In this project, we will learn about Stepper Motors, how the Raspberry Pi Stepper Motor Interface works and how to control a Stepper Motor using Raspberry Pi and L298N Motor Driver Module.
Outline
ToggleOverview
If you remember the previous Raspberry Pi Tutorials, we have seen HOW TO CONTROL A DC MOTOR USING RASPBERRY PI and also HOW TO CONTROL A SERVO MOTOR USING RASPBERRY PI.
Stepper Motors can be categorized between Servo Motors and DC Motors as you can control the angular position of the shaft as well as rotate the shaft freely in either direction.
I have already discussed about stepper motors, types of stepper motors, internal structure of a stepper motor and also how to design a stepper motor control circuit in STEPPER MOTOR CONTROL USING ARDUINO project.
So, I suggest you to refer to that project once before continuing with the Raspberry Pi Stepper Motor Interface.
Driving a Stepper Motor
It is important for you to identify your stepper motor i.e. whether it is a Unipolar Stepper Motor or a Bipolar Stepper Motor. Because, the driving techniques for each of these motor is different.
In this project, I’ll be using a simple 12V Bipolar Stepper Motor. To drive this Stepper Motor, we need a motor driver module like L293D or L298N. Both these module work great with the 12V Stepper Motor. So, choosing the driver module is up to you. For this project, I have chosen the L298N Motor Driver Module.
The main advantage of a Stepper Motor over a Servo Motor is that complete rotation of the motor’s shaft is divided into steps and you can precisely position the Stepper Motor by carefully energizing the Motor Coils.
Even though there is no feedback mechanism like in a Servo Motor, a Stepper Motor with smaller step angles can provide very accurate angular positioning.
Typically, almost all commercially available Stepper Motors comes with a step angle of 1.80. So, for a complete 3600 degree rotation, it will have a step count of 200.
In order to increase the accuracy of the Stepper Motor i.e. to achieve a bit more precise angular positioning of the shaft, you can use a technique called “Half Stepping”.
Half Stepping is a technique where instead of energizing a single phase at a time, we will be use a combination of one phase and two phase energizing. With this technique, you can achieve even smaller resolution of 400 steps and a smoother operation.
Raspberry Pi Stepper Motor Interface
Almost all advanced CNC Machines and Industrial Pick-n-Place Robots use Stepper Motors in them. Raspberry Pi on the other hand is a tiny computer that is being used in a wide range of applications like Robotics, Home Automation, Industrial Automation an many other.
So, interfacing a Stepper Motor with Raspberry Pi will be interesting as you can implement this interface in all the above mentioned applications.
A 12V Bipolar Stepper Motor draws about 350mA to 500mA of current. Hence, we need a dedicated driver circuit while working on the Raspberry Pi Stepper Motor Interface.
Since I have used a Bipolar Stepper Motor, I only need four control wires. This can be done with the help of many different modules or drivers like L293D, ULN2003 or L298N.
In case of a Unipolar Stepper Motor, there are multiple ways you can wire it to the Driver Module. Typically, a Unipolar Stepper Motor will have either 5 or 6 wires. So, you need to use ULN2003 or wire it a Bipolar Stepper Motor.
Circuit Diagram
The following image shows the connection diagram of the Raspberry Pi Stepper Motor Control using L298N. It is implemented using Fritzing Application.
Components Required
- Raspberry Pi 3 Model B
- 12V Bipolar Stepper Motor
- L298N Motor Driver Module
- 12V Motor Power Supply
- Power Supply for Raspberry Pi
- Connecting Wires
- Computer
Circuit Design
The inputs to the Motor Driver Module i.e. IN1, IN2, IN3 and IN4 are connected to the Physical Pins 11, 12, 13 and 15 i.e. GPIO17, GPIO18, GPIO27 and GPIO22 of the Raspberry Pi.
One set of the Motor Coils are connected to OUT1 and OUT2 of the Motor Driver and the other set is connected to OUT3 and OUT4.
A 12V external power supply is given to the Motor Driver Module and the ground terminals of the L298N Motor Driver Module and the Raspberry Pi are made common.
A separate 5V supply is given to the Raspberry Pi.
Code
The programming part of the project is implemented using Python. The Python script for Controlling a Stepper Motor using Raspberry Pi and L298N Motor Driver Module is given below.
Working of the Project
This purpose of this simple project is just to understand how to Interface a Stepper Motor with Raspberry Pi and L298N. The working of the project is very simple and is explained here.
Initially, you need to calibrate the Stepper Motor by giving some random values so that the steps will be locked. Here, a positive value (like 30, for example) will make the Stepper Motor to rotate in clockwise direction and a negative value (like -30) will make it to rotate in the anti-clockwise direction.
After this initial calibration, you can enter any value and the Stepper Motor will move to that position. Since we are implementing a Half Stepping Technique, the total number of steps is 400. So, if you enter 100, the Stepper Motor will rotate 900 in clock wise direction.
Similarly, by entering other values (either positive or negative) you can accurately control the position of your Stepper Motor.
Applications
Controlling a Stepper Motor with Raspberry Pi will enable us to use this interface in a variety of projects and applications like:
- Robotics
- CNC Plotters
- 3D Printing
- Home Automation
- Industrial Automation
11 Responses
Hi
Thanks for the tutorial. Any idea on the speed controls using an L298N
thanks for your kind asist but whem i drive stepper motor l298n ger very hot do you have any solution?
Thanks for assist but stepper motor is rotating it just vibrating and if i give big value it will simply vibrating more times ..
please help me out i am new in this field..
Hello sir can I use uln2003a for driver circuit.?
why here EN pins are not used? i tried with this code and the procedure but not working
Let me address a few concerns:
Q: Why is my stepper hot?
A: Steppers usually get quite hot in operation. If you’re worried about it damaging something then you can always dissipate it. You can also go the route of either turning down the driving voltage or decreasing the PWM value on the EN pins.
Q: My stepper isn’t rotating. What’s wrong?
A: It can be one of two things. If your motor is just sitting in place, moving slowly, or is making a small noise instead of the normal one, it may be an issue with your driving voltage. Steppers are normally operated above their recommended voltage but using pwm on the EN pins. The other problem that I’ve had happen is that the wires going from the driver to the stepper are in the wrong order. This can resort in the motor vibrating at full power but not really spinning.
Q: How can I control speed?
A: You can do it either by decreasing the pwm on the EN pins or by adding a delay in between each time you move the motor.
What I find interesting is that the L298N is rated for a minimum of 4.5v on the logic. This post was an interesting example of using the 3.3v gpio pins of the pi on the driver inputs but then using 5v on the actual logic line for the driver. Interesting.
Do i need some library to run it?
Same case here. The motor is just static and vibrating. It is not rotating in either of the directions. Please help me out with this.
Your wires are probably wrong. I had the same problem. It’s because each of the two wires/phases are turning the motor in opposite directions, so it turns left then right, therefore looking like it just vibrates and stays in the same position. I changed my wire positions and things are working now.
Good luck
Regarding the vibration / not rotating motor, I noticed the following (Raspberry Pi 4):
In my situation, the vibration was caused by the stepper motor quickly reversing rotation direction instead of step by step in one rotation direction. Using a sleep time of 1 second makes it easier to see/feel what the motor is doing.
I solved this vibration by switching “out1 = 13” to “out1 = 11” and “out2 = 11” to “out2 = 13”
Some L298N based control boards have LED’s indicating which output is “high”/on or “low”/off.
This might help in understanding if the logic order of switching on/off is correct.
During the vibration, I noticed an irregular pattern
Hello,
is it possible to increase the rotation speed of the motor?