Introduction: Driving a 12V 28BYJ-48 Stepper With the A4988 Stepstick (in Full-step Mode).
Table of Contents:
Step 1: Parts
Step 2: Setting Up the Motor, CNC Shield, and UNO
Step 3: Writing Code and Running the Motor
Full Disclosure: One A4988 stepper driver was destroyed with reverse polarity during the making of this instructable.
Warning! 12VDC being back fed into the UNO from the CNC shield and then from the UNO to your computer could potentially destroy your personal computer. This is why I am unplugging the USB cord from your PC before providing 12VDC to the CNC shield.
Facts:
- The 28BYJ-48 is not broken out on it's header connector correctly (ignoring the red wire entirely) to be connected one for one to an A4988 stepstick driver. In short, the pink and yellow wires need to be crossed over.
- The spec. sheet states that an A4988 needs > 8VDC power supply for your motor, i.e. you may not be able to run a 5VDC power supply for driving 5V 28BYJ-48 motors.
- You do not need to disconnect the 28BYJ-48 red wire to run it in bipolar stepper mode. I have done it both ways and have seen no difference, although I have no quantitative data to back up this assertion. However, visually, stepwise, and to the touch (heatwise) I have noticed no difference in my limited trial.
- You need to dial the potentiometer back on the a4988 to restrict it's current flow. I have mine set to 0.10V, this value could be slightly increased to get your motor turning more strongly (0.15V). I have accidentally had the 'pot' set at 0.50-0.60V, although it didn't blow the A4988 or 12V stepper, it is too high. Too high of pot reading could prove dangerous if run for too long at this setting, burning your 28BYJ-48 motor, A4988 stepstick, or place of residence up.
Disclaimer: I am not an electrical engineer and my 'facts' should be taken as such.
- Not needing to disconnect the 28BYJ-48 red wire to run it in bipolar stepper mode, is my opinion (based on my limited trial), and thus should be taken with a grain of salt. Many, claim that you need to both internally cut the circuit track that connects both poles' common line together, and then to disconnect the red wire entirely.
- Needing to dial the potentiometer back on the A4988 to restrict it's current flow is definitely a fact. However, having mine set to 0.10V, is merely my opinion. You should consult the datasheets and use their values and formulas to set this value properly. By slowly increasing your potentiometer's voltage, your motor will turn more strongly and truely. I have had to tune my driver to about 0.15V in order to achieve the performance I knew the motor was capable of.
Step 1: The Parts
- Jumper Wires
- CNC shield, or similar breakout board or a breadboard plus resistors, capacitor, etc.
- Arduino Uno, although other microcontrollers/microcomputers could be used
- A4988 stepstick driver
- Plastic potentiometer screwdriver, or normal #1 phillips' head srewdriver or similar
- 3mm flathead or silimar to tighten screw terminals snuggly.
- Wire strippers
- Multimeter
- 12VDC 28BYJ-48 stepper motor
- 12VDC power supply (at least 1Amp for 2 motors) that plugs into household AC or a 12VDC battery with a disconnect.
- 16 gauge wires for power supply
- 5VDC USB power supply
- PC or microcomputer (to run the Arduino IDE and to flash the program via USB to the UNO).
The difference between a microcontroller and a microcomputer could be easily compared the difference between an Arduino (microcontroller) and a Raspberry Pi (microcomputer). They are both capable of manipulating GPIO pins, and executing a code or program. The difference is that a microcontroller needs it's program/code and/or commands sent to it from an outside source, in order to operate; and in it's operation only executes within it's code base and can at most receive and send basic messages to other devices. The Pi can do many of the things an Arduino can, however the Pi utilises an operating system called Linux; which allows the user to basically have a miniature PC that has it's GPIO pins exposed, like an Arduino does.
We can use either a microcontroller or microcomputer for driving a 12V 28BYJ-48 stepper motor with the A4988 stepstick driver. In the case of the microcomputer (RPi), we will both compose and execute the code within the Linux OS. In the case of the microcontoller (Arduino), we will write the code in an IDE on a pc and then flash the code to the microcontroller; the microcontroller then executes that code.
Step 2: Setting Up the Motor, CNC Shield, and UNO
Above is a somewhat fuzzy picture of the crossover of the pink and yellow lines, notice my orange and red jumper wires cross over each other. Also notice the red is not connected.
- Prepare motor to plug into A4988 outputs.
- Put CNC shield on top of UNO.
- Power the UNO and set potentiometer/s.
- Unplug UNO's USB from power supply or computer. Hook 12VDC to the CNC shield to 'proof it'.
- Unplug 12VDC power supply from wall. Or if using a 12V battery disconnect it from the CNC shield. Your project is now entirely disconnect from all power source. With niether 5V nor 12V connected to the UNO-CNC sandwich, it can be safely manhandled and stored.
1) Correct Wiring for a 28BYJ-48 stepper for A4988 bipolar mode:
- Ignore the red wire. You do not need to disconnect it internally of from the connector.
- The pink and yellow wires need to be crossed over.
- The correct order of 28BYJ-48 wires pinned for stepstick use is blue, yellow, pink, and then orange; or vice versa.
28BYJ-48 to a4988 connections:
- Red wire= not connected
- Orange = 1B
- Yellow= 2A
- Pink= 1A
- Blue= 2B
Basically, the pink and yellow wires need to be crossed over. I have done this by re-wiring with jumpers, by simply crossing the pink and yellow jumper positions with each other when plugging them into their stepstick outputs, I did this because I can still pull these jumpers back out and plug the motor's connector back into the stock ULN2003 unipolar stepper controller. However, the 28BYJ-48's white connector could be rewired instead by popping out and switching the locations of the yellow and pink wires within the motor's connector, permanently mapping it for compatibility with the A4988 driver. This is a matter of opinion, ultimately I needed extra length in my wires so I went for the jumper crossover option.
Remember red is unconnected and yellow and pink are swapped.
2) Correct Assembly of the a CNC shield:
Never step the A4988 without having a stepper hooked to it's outputs, it will likely fry it. For that matter, it may be advisable to not connect to your 12V power supply until your stepper motor are connected securely to each of the four outputs, on each of your A4988s.
- I recommend that you don't microstep the 28BYJ-48 stepper, it already contains an internal gear train so microstepping it will be rather silly. Do not install jumpers on the microstepping 2x3 header to achieve full-stepping. Install or don't install your black squarish jumpers correctly to select a microstep setting.
- Insert A4988 into the CNC shield, the direction matters. On my version, the side with the potentiometer(pot) gets inserted toward the bottom (side with the motor power supply screw terminals) of the breakout board. Consult the internet or a photo to double check.
- Plug a properly pinned out (via crossover cables or re-populating the stepper's header) 28BYJ-48 stepper motor into the 4 outputs on each A4988 you have plugged in. While, being careful to not connect the red wire.
- Insert CNC shield onto the UNO making sure that all your pins are aligned properly and that they are going to slide easily into the UNO. CNC shields might have bend pins need to straighten first, you could also inspect for solder bridges while checking pins.
3) Adjusting the a4988 potentiometer:
- Plug the UNO's USB cord into the UNO. Then plug the other end into a 5VDC power supply or a computers USB port. It would be safer to use a power supply, than a computer.
- Set multi-meter to a mode suitable for reading DC voltages between 0.0 & 1.0 VDC.
- Read the potentiometer voltage, by putting the positive red lead on the top the pot (where the screw driver goes), and putting the negative black lead on a negative point, a good large one is the CNC shield's power supply negative (-) screw terminal. You could even clamp your black multimeter lead carefully into the negative screw terminal.
- Set this value by turning the pot counterclockwise a little at a time, taking intermittent voltage readings. Turning clockwise to raise the voltage back up if needed. Mine is set to 0.10V, I believe this to be a safe rating, however this is merely my opinion. You should consult the datasheets and use their values and formulas to set this value properly.
- If your motor seems to be a little lack lustre performance or power wise, you can slowly increase your potentiometer's voltage. As you this value increases, your motor will turn more strongly and truly. I have tuned my driver to about 0.15V in order to achieve the performance I knew the motor was capable of. The motor's power will no longer increase past a point. I wouldn't recommend values in excess of 0.15V. Excess power = excess heat.
4) Connect 12VDC to the CNC shield:
- Disconnect the UNO from 5V. This protects your power supply or computer from being fried if there is a dead short in a poorly/cheaply manufactured CNC shield.
- Hook up your 12V power supply to the CNC shield with 16 gauge wire (18 gauge could be used for such small motors), making sure positive wire is going to positive screw terminal and negative to negative. Make sure you don't strip the wire too far back (only about 4-5mm or 1/8" -3/16" for the CNC screw terminal wire ends), if using multi-stranded wires-- twist each end of each wire round and round so they are tightly together (so the ends are less like multi-strand wire and more like a 'solid' core one), and then tighten the correct wires into the correct screw posts. It is important get these ends are tightened and fastened really well within the screw posts, we can give each wire a slight tug to ensure it's solid. Also 16 gauge wire works better than 18 because it has a larger diameter to clamp.
- Turn the power on and let it sit for like 30 seconds to a minute. This is 'proofing'/proving whether or not the shield is operable, 'bricked on arrival' or perhaps bricked because our shortcomings. If during this time, if you hear, see or smell something funny-> unplug the shield's 12VDC power, as quickly and safely as possible.
I applied a 12V reverse polarity to to my shield during this step, fried an A4988 and thankfully nothing else. I then simply added a new A4988 and I re-powered it correctly (+ to +, - to -), it seems fine. But, a possibly damaged shield like this should be marked as such (Sharpied note to self), so it doesn't wind up accidentally getting put into service in an end project someday.
Warning! 12VDC being back fed into the UNO from the shield and then from the UNO to your computer could potentially destroy your personal computer. This is why I am unplugging the USB cord before proofing. Accidents can and do happen.
5) Disconnect 12VDC from the CNC shield:
- To be 100% safe the 12V should be turned off from the CNC shield at this point. Unplug the 12VDC power supply from the AC wall outlet. Or if using a 12V battery disconnect it from the CNC shield; this is done with a positive-side switch that can handle the current or by putting a barrel jack inline or another type of quick connector. It is important that the disconnection occurs in a minimum the positive wire. I am using a battery as my power supply and have an inline barrel jack as a shut off.
- 12VDC being back fed into the UNO from the shield and then from the UNO to your computer could potentially destroy your personal computer.
- I am unplugging 12V power now, because we are going to proceed to flash a sketch to the UNO in the next step.
- With niether 5V nor 12V connected to the UNO-CNC sandwich, it can be safely manhandled and stored.
Step 3: Writing Code and Running the Motor
To be 100% safe the 12V should be turned off from the CNC shield at this point. 12VDC being back fed into the UNO from the shield and then from the UNO to your computer could potentially destroy your personal computer. It could happen... be warned.
I personally use a Raspberry Pi to write my code and then flash it to the UNO. Because of the non-valuable nature of my PC I am willing to have both 12VDC connected and the USB hooked to the computer all at once. I personally have never fried my Pi, but it's cheap nature allows me to gamble a little. I recommend disconnecting 12V before putting the UNO USB into the computer. Better yet, I'd recommend getting a Pi because things are much easier and pleasant if you don't have to worry about destroying an expensive PC.
Note: I'm assuming that you already have basic competency within the Arduino IDE.
- Hook UNO to computer via USB.
- Start the Arduino IDE.
- Write program. I'm assuming your stepper is hooked to the x-axis on the CNC shield. (see below for other UNO to CNC shield pin assignments)
- Flash program to UNO.
- Check the serial terminal at 9600 baud to ensure the program flashed properly... 'stepping' will be printed very rapidly once per line.
- Unplug UNO from computer.
- Plug UNO into 5V USB power supply. (Or if using a Pi you could leave it plugged in to the Pi, do this at own risk)
- Connect 12VDC to CNC shield.
- Watch motor spin.
- Unplug the 12VDC when done, because it will still be powering your one pole of your motor even when idle. Plus, you could write good code that disables the A4988 when you are done with it, thus powering your motor down.
void setup() { Serial.begin(9600); pinMode(2, OUTPUT);//step pin pinMode(5, OUTPUT);//direction pin pinMode(8, OUTPUT);//enable pin //enables a4988 stepper drivers, pin 8, 0=enable, 1=disable digitalWrite(8, 0); //sets x axis direction digitalWrite(5, 0); } void loop() { Serial.println("stepping"); digitalWrite(2, 1);//step signaL ON digitalWrite(2, 0);//OFF //delay between steps-- 5ms is a common value that works well in full step mode delay(5); }
UNO pins to CNC shield:
- pin 8= a4988 stepper drivers enable pin
- pin 2= x-axis step pin
- pin 3= y-axis step pin
- pin 4= z-axis step pin
- pin 5= x-axis direction pin
- pin 6= y-axis direction pin
- pin 7= z-axis direction pin
Step 4: Going Further... A4988 on a Breadboard Operated by a Raspberry Pi
First, I'd recommend looking at some other pictures online about the A4988's pinout and/or breadboarding it to supplement my poor pictures
- Put A4988 on breadboard.
- Write code for Pi.
- Watch motor spin.
1) Breadboard an A4988:
- Insert the A4899 on a breadboard, ensuring that one of it's 1x8 headers is on one side of the breadboard and the other 1x8 header is on the opposite side.
- If left unhooked, the enable pin is floating or pulled to ground-- in either case the A4988 is ready to operate a motor without connecting this pin.
- If left unhooked, the ms# pins are floating or pulled to ground-- in either case the A4988 is ready to operate a motor in full-step mode with these pins not connected. You can hook each ms# pin to 5v with a resistor (1k-10k), so you can set the different step modes.
- The reset and sleep pins need to be connected with each other. This can be accomplished via a jumper wire.
- The motor gets connected to 2B, 2A, 1A, 1B. See Step 2: 1) Correct Wiring for a 28BYJ-48 stepper for A4988 bipolar mode.
- Put a capacitor (approximately 100uF) between Vmotor (top right, assuming your A4988 is aligned the same as my diagram). Positive (top right pin) to the positive capacitor leg and the negative or ground (GND) capacitor leg to the negative or ground (GND) pin, it's the next pin down.
- Hook the motor's VDC 12V power supply to the Vmotor. Positive (top right pin) to the positive (power supply) and negative to negative. Negative or ground (GND) is the next pin down.
- Unhook the 12V power supply, only the positive line really needs disconnected.
- Hook the microcontroller to the A4988. I hooked the step pin to gpio18 on the Pi. And I hooked the dir/direction pin to gpio17 on the Pi. Then, hook the 5VDC from the Pi to the A4988, we could use an external 5v power supply to make this step safer. Piositive to positive-- Vmicro (5V) pin (second from the bottom) to microcontroller's/supply's 5v pin, and negative to negative-- GND on micro and supply(if used) to GND (bottom right pin) on the stepper driver.
- Set the A4988 potentiometer to limit the current. See Step 2: 3) Adjusting the a4988 potentiometer.
- A common ground needs to be present in order for electronics to work; all the negatives must all be connected with one another. i.e. The Pi and motor power supplies are connected by grounds through the A4988 stepstick. If you are using an external 5v power supply obviously connect it's negative to the GND (bottom right pin); however, we must not forget to also tie the Pi's/micro's GND here too.
- Finally, we can write some code and run the example. We could achieve this on an Arduino with their IDE. However, I am going to use a Pi to control the A4988 by running a python script.
2) Code for your Pi...
import RPi.GPIO as GPIO
from time import sleepGPIO.setwarnings(False) #sets gpio to numerical pin assignments... 1-40 GPIO.setmode(GPIO.BOARD) #sets pins as outputs GPIO.setup(11, GPIO.OUT)#(gpio17) GPIO.setup(12, GPIO.OUT)#(gpio18)
#sets direction pin GPIO.output(11, 1)
while True: #step pin GPIO.output(12, 1) GPIO.output(12, 0) sleep(0.004) print("test")
- Put code in text document or favourite IDE and save it to test.py, or similar. Let's save the file directly on the desktop.
- Open a fresh terminal and type:cd /home/pi/Desktop/(enter key)
- Then type:python test.py(enter key)
- The program will start and wildly print test.
- Hit Ctrl+C to exit the program, it loops forever
- Leave terminal open.
3) Run motor
- Recconnect 12VDC to A4988.
- Start python script again in terminal:python test.py
- If all is well, your motor is spinning. If not double check your connections.
- Hit Ctrl+C to exit the program and stop the motor.
- Unplug the 12VDC, because it will still be powering your one pole of your motor even when idle.
Warning! 12VDC being back fed into the Pi from the A4899 could potentially destroy your Raspberry Pi. Breadboard this circuit at your own risk. Reverse polarity is the utmost enemy. If you value your Pi use an external 5V supply and be very careful not to feed 12VDC accidentally in to the common ground line. This can fry your Pi. Reverse polarity (connecting positive to negative accidentally) fried a A4988 while hooking up my CNC shield. Beware, of damaging your electronics by proceeding with caution and double check your connects before applying power to a circuit.