A few things to know about the L298N
This is a rudimentary way to drive a stepper motor. You are activating the motor’s coils in sequence yourself to have it take steps. There exist cheap stepper drivers which only require 1 signal from the Pi to be instructed to execute a full step sequence.
There are 2 coils on a bipolar stepper motor, each with a + and a – side. As such, this method requires 4 pins from your Pi to drive the Motor Vs only 1 pin with a more advanced stepper driver. Equally true is that a more advanced stepper driver will require at least another pin for direction and they’ll usually have other pins for micro-stepping.
This method does not allow for micro-stepping, only full steps. This is fine enough for most applications.
Lastly, while the L298N method might appear less appealing because of the above points, I find it to be extremely robust (advanced stepper drivers can be very finicky). You can power your Pi or Arduino with it thanks to a 5V port (warning, it can only provide so many amps and a Pi running heavy processing will crash).
It can be used to drive 2 DC motors instead of 2 stepper coils of a bipolar stepper motor, and has 2 PWM pins to adjust power given to the motors. It’s a very versatile and enabling device to master. You’ll also learn a lot about how stepper motors work.
Finding your stepper motor coils
Documentation for electronics is often inaccurate, disparate, when it’s even available. It’s good to find or verify what wires you think your coils are on. To do so you can put an LED on 2 motor pins and spin it until it lights up. When it does, induction powered your LED so you know you are holding the 2 wires of a coil. The other 2 wires are obviously the other coil.
There is no good way I could find to verify which of a coil’s wire is positive or negative. In my trials, I found that it doesn’t matter for a bipolar stepper motor on an L298N. The worst that will happen is that your motor may turn in the other direction which is easy enough to fix by flipping wires.
#!/usr/bin/python3 import RPi.GPIO as GPIO import time out1 = 17 out2 = 18 out3 = 27 out4 = 22 # careful lowering this, at some point you run into the mechanical limitation of how quick your motor can move step_sleep = 0.002 step_count = 200 # setting up GPIO.setmode( GPIO.BCM ) GPIO.setup( out1, GPIO.OUT ) GPIO.setup( out2, GPIO.OUT ) GPIO.setup( out3, GPIO.OUT ) GPIO.setup( out4, GPIO.OUT ) # initializing GPIO.output( out1, GPIO.LOW ) GPIO.output( out2, GPIO.LOW ) GPIO.output( out3, GPIO.LOW ) GPIO.output( out4, GPIO.LOW ) def cleanup(): GPIO.output( out1, GPIO.LOW ) GPIO.output( out2, GPIO.LOW ) GPIO.output( out3, GPIO.LOW ) GPIO.output( out4, GPIO.LOW ) GPIO.cleanup() # the meat try: i = 0 for i in range(step_count): if i%4==0: GPIO.output( out4, GPIO.HIGH ) GPIO.output( out3, GPIO.LOW ) GPIO.output( out2, GPIO.LOW ) GPIO.output( out1, GPIO.LOW ) elif i%4==1: GPIO.output( out4, GPIO.LOW ) GPIO.output( out3, GPIO.LOW ) GPIO.output( out2, GPIO.HIGH ) GPIO.output( out1, GPIO.LOW ) elif i%4==2: GPIO.output( out4, GPIO.LOW ) GPIO.output( out3, GPIO.HIGH ) GPIO.output( out2, GPIO.LOW ) GPIO.output( out1, GPIO.LOW ) elif i%4==3: GPIO.output( out4, GPIO.LOW ) GPIO.output( out3, GPIO.LOW ) GPIO.output( out2, GPIO.LOW ) GPIO.output( out1, GPIO.HIGH ) time.sleep( step_sleep ) except KeyboardInterrupt: cleanup() exit( 1 ) cleanup() exit( 0 )
Stuff you might need for this to run:
sudo apt-get update --fix-missing && sudo apt-get install python3-rpi.gpio
This stepper motor is rated for rotating by 1.8° per step. This means that it takes 100 steps to rotate 180 degrees:
Power the Pi from the L298N?
As I alluded to earlier, the L298N comes with a 5V power output that is most convenient to power Pis and Arduinos as it allows you to eliminate one of the 2 power supplies to your system. Be advised that while it provides the right voltage, it does not provide enough amps to power a standard Raspberry Pi, and while it can power a Pi Zero, the later will not get enough power and crash if it works hard enough (having a camera and a live streaming server pushed it over the edge in my testing). It will power a Pi Zero on Wifi running Python which is enough for a lot of applications.
Here’s the circuit if you want to do this:
10 Replies to “Driving a Bipolar Stepper Motor with an L298N and a Raspberry Pi”
I’m very much a beginner and have just bought a RP to play with . I managed to connect this all up and make it work first time. Thanks Ben.
Next stage – Is to amalgamate a drive system all into a BBQ rotisserie and a Star tracker for my camera.
I have connected a stepper motor, which needs only 3.1v rated voltage and 2.5 amps rated current. in this case, can i proceed with 5v power supply? if i use 12v power supply, is it ok to stepper motor ?
42BYGHW811 wantai mini stepper motor
Raspberry pi 3 b+
power supply L298N – 5v
power supply to raspberry – 5v
kindly resolve the issue.
Thank you in advance
I definitely wouldn’t be using 12v supply but you might be able to get away with 5v. Overall this might be kind of a mismatch and you might want to try and find a better suited stepper driver.
May i know which stepper motor you used in above article?
and can you suggest me any stepper driver for 42BYGHW811 wantai mini stepper motor.
The motor on this post is a nema 17. I don’t know of a driver for your motor off the top of my head, it’s your homework to figure one out that matches the specs :).
Thank you so much.. 🙂
Can you please give me the stepper motor model number ?
because even my stepper is also a nema 17.
thank you.. 🙂
Thankyou so much, its really helpfull to me.. 🙂