The code below shows, how to use an unipolar Stepper Motor (28-BYJ48) with a Motor Controller (ULN2003).
(-> the L298N cannot be used because it is designed for bipolar Stepper Motors)
The picture shows how to connect the cables properly (you can ignore the second motor).
There are three versions of the code:
- using the already implemented Stepper.h library with one motor (a second one is commented out)
- using the Stepper.h library as a foundation for simple object orientated programming (OOP)
- using the AccelStepper.h library with one motor (a second one is commented out)
Important Annotation: It is not necessary to connect a separate battery to the Stepper Motor Controller (if you use the 5V version of the Stepper Motor), but it is recommended to use one (to protect the electronics). The Arduino is able to output 5V but it may not work flawlessly.
The Stepper.h library is already implemented in the Arduino IDE. To implement the library, go to Sketch
-> Include Library
and search for Stepper
or just write #include <Stepper.h>
to include the library.
The provided program will move the Stepper(s) as they are might be intended. The program is pretty self-explanatory (there are a lot of comments).
The program is using the Stepper.h library as a foundation for object orientated programming. OOP is an important programming concept which is widely spread (many of the most widly used programming languages support it).
The provided program is much more space efficient because some funtions of the Stepper.h library, which are for other Stepper Motors, are missing (because they are not needed for the 28-BYJ48 Stepper Motor).
At first, a class called StepperMotor
is being created. After that, some private variables for later usage are being initialised. Those variables are accessable inside the class, so you can't call them (it is kind of code protection). Only the class itself can call them. After that. some public funtions are initialised. The first function is a constructer
because it has the same name as the class. The constructor is called every time when a new object is being created. This explicit constructor declares the previous initialised variables. The other functions just apply logic for moving a Stepper Motor. The rest of the program is almost the same as in the first program. The only difference is that Stepper
is replaced by StepperMotor
, because we changed the class name.
Understanding this code (the Stepper.h lib) leads to more control over the Stepper Motor.
Work in prgress!
Since the Arduino NANO has a different chip built-in, you have to compile the program in another way which is pretty straight forward. Nevertheless it won't work if you use the standard bootloader.
To change the board, go to Tools
-> Board
and change it to Arduino Nano
.
After that, go to Tools
-> Processor
and change it to ATmega328P (Old Bootloader)
.
It is recommended to change the bootloader to the standard one if you are working on other projects because the change of the booloader could lead to problems with other programs.
In the scripts folder are some scripts explaining in detail how to use the Stepper library. Those scripts are only in German.
Work in progress