Introduction: 28BYJ-48 5V Stepper Motor and A4988 Driver
Ever wanted to get a robot to turn at a precise angle, using only a few outputs of your Arduino or micro:bit ? All this for cheap ? That's the instructable for you ! In this instructable we will see how to drive a very cheap stepper motor using only 2 outputs of our controller and requiring only a 5V power supply !
I made this instructable after struggling a bit to gather the information, sometimes stumbling on misinformation, and I wanted to save others from going through the same process.
But before starting, why such constraint ?
- Why 5V: because I want to integrate this on a mobile robot which will run only with a 3.7 lithium battery which I can get 5V out with a booster.
- Why using the A4988 and not the ULN2003 which often comes with the 28BYJ motor ? Because, first, it requires 4 inputs. Therefore using the A4988 makes us save 2 of our precious controller outputs (and if you like working with the micro:bit as I do then those outputs are precious...) ! But there is more ! Being able to drive the motor by just giving the steps as high impulses, gives us the possibility to drive the motor with a simple PWM. By fixing the duty cycle to 50%, changing the frequency of the PWM will change the rotation speed of the motor. Why is that great ? Because if you want to set my motor speed and then carry on controlling other things with my Arduino or micro:bit, then you can simply set my PWM and forget about it, which will make your code so much more readable and your life so much easier (for instance if you want to build a robot like this).
So let's get started !
Here is what you'll need for this instructable:
- 1x 28BYJ stepper motor
- 1x A4988 driver
- 1x breadboard or prototyping board, a capacitor and some wires
- Micro:bit and extension board or Arduino
- 5V power supply (+3.3V if you use Micro:bit). For this I used a 18650 lithium battery and a battery shield.
- 1x multimeter
Step 1: Getting to Know Our System
The first thing that I'd recommend to start with, would be to learn more about stepper motors and the A4988 driver. Hey, but why do we need this driver ? Could we control a stepper motor without a driver ? The answer is no. Boards like Micro:bit and Arduino are good at processing information but not at giving out much current, and you need current to make a stepper motor move. To learn more about how both the motor and driver work this is the reference I would recommend. It is synthetic but also contains most of the information you'll need for the wiring.
But wait before trying to wire anything! Is the 28BYJ adapted to the A4988? If you do a quick search, you'll see that this motor comes rarely with the A4988 as a driver. If you read thoroughly the previous reference you might see why: our stepper is a unipolar motor while the A4988 is designed to drive bipolar motors, so we'll have to hack a bit our motor !
Step 2: Hacking the Motor
To make your motors compatible with the motor driver simply take the red wire out of the white connector. To do cut the connector to remove the red wire and cut the red wire of the motor. Then swap the yellow and pink cable on the connector. Keep the red wire and connector for the next step !
To get a cable out of the connector push the wire you want to remove in the connector and then push the visible metallic bit on the connector in with a sharp tool (above is a picture where I do this with my favorite knife, the opinel !), and finally pull and eventually the whole thing should come out as on the picture above. The last picture shows what the connector should look like at the end of those modifications: the order of the cable on the connector should be orange/pink/yellow/blue.
(PS: online you'll find some tutorials indicating that you have to desolder the red wire from the motor and then scratch the PCB, forget about that, this is not necessary. Useless ?)
Step 3: Setting the Driver
Now... time to drive this motor with the driver ? Not yet sorry ! You see the screw on the A4988 board ? Well we'll have to fiddle with it. This screw is basically allowing you to set how much current will go through the coils of your motor. In our case, while our power supply gives 5V and our coils in the motor have a resistance of 50 Ohms, our current will not be more than 100mA, which should be supported by the motor so you could eventually skip this step. However if you are like me and that you'd like you motor to only take as much current as it needs then follow along.
So to set the driver, follow the Method 2 of this article with those adaptations (as the picture above shows)
- Use the 5V from the battery shield for both the logic and the motor power input (VMOT is said to need more than 8V but 5V works !). The 2 GND pins on the board are connected, so no need to connect them both to the battery ground.
- Connect STEP and DIR pins to the 5V too (not to Arduino as shown in the referenced article)
- When setting the multimeter, I set the current to 50mA which was enough to drive my motors using half-steps (more on that in the next step). To connect my multimeter to measure the current in the coil of the motor, as you can see ono the image above, I unplugged the yellow wire from the connector and put the red wire in, so that I could put my multimeter from the red to the yellow wire to measure the current.
Step 4: Controlling the Motor
That's it, we are almost ready to make our motor turn. The only things to do is:
- to remove our multimeter from our system if not already done,
- connect MS1 to 5V which will make the driver use half-steps (I had trouble getting the robot to turn with full steps on 5V. But as a part of my objective was to make everything run on 5V I accepted to sacrifice a bit of speed and to gain some precision),
- provide the STEP and DIR pins with what we want from our controller.
Then: if you want to control the motor using Arduino, simply follow the article here where you'll find a sample code. If you want to control it with the micro:bit then stay with me a little longer.
Micro:bit, as Arduino, comes with GPIOs. Therefore, once we power it (with 3.3V !), then we can program it to output STEP and DIR. While there seems to be plenty inputs and outputs, be warned that actually many of them are already reserved for some other purposes. You can learn more about that in this article. You will see in this article that actually many of the inputs/outputs are shared with the display, and therefore, if you want to use those ones, then you'll have to turn off the display. But let's not turn off the display ! So which pins can we use ? I'll use the pins 2 and 8 as I won't use the pads (pin 2).
Plug pin 2 of the micro:bit to STEP, pin 8 to DIR, upload the program attached using your favorite micro:python editor (I used mu-editor). This program basically sets a PWM on pin 2 with a period of 1 millisecond (and a 50% duty cycle), and your motor should be rotating. Set pin 8 to 0 or 1 to make it turn one way or another and change the period to make it turn the speed you want (as long as you don't want it to go too fast... for me a pulse every millisecond was close to the maximum speed I could reach).
To make things a bit more compact and embark it easily on a mobile robot I made a little board. The board is shown in the picture above. In the image there is a purple wire going from VMOT to VDD that is hiding in the shadow. Also, the yellow wire going from SLP to RST is actually not soldered, I just put it there to represent the solder I put on the back of the board to connect those 2 pins. Remark: the heat sink is normally not required with such a system, as we draw much, much less than 1A.
That's it, I hope this instructable will help many of you enjoy the power of stepper motor in your projects.