In this tutorial, we will learn about the 28BYJ-48 stepper motor and how to interface it with Raspberry Pi Pico using a UNL2003 driver. The 28BYJ-48 stepper motor is inexpensive and one of the most commonly used stepper motors out there. It comes with a UL2003 motor driver attached to it that is responsible for driving a stepper motor. The reason for using a driver is that the Raspberry Pi Pico GPIO pins can not provide enough driving current to a 28BYJ-48 stepper motor. The second important reason is that it protects the board’s pins from getting damaged due to the high current requirement of a stepper motor than a maximum current that Raspberry Pi Pico pins can provide.
We have a similar guide using MicroPython for ESP32:
Before we start this lesson, make sure you are familiar with and have the latest version of Python3 installed in your system and set up MicroPython in your Raspberry Pi Pico. Additionally, you should have a running Integrated Development Environment(IDE) to do the programming. We will be using the same Thonny IDE as we have done previously when we learned how to blink and chase LEDs in MicroPython here:
If you are using uPyCraft IDE, you can check this getting started guide:
We will require the following components for this user guide:
- Raspberry Pi Pico board
- One 28BYJ-48 Stepper Motor with ULN2003 motor driver
- External 5V power supply
- Connecting Wires
Stepper Motors Introduction
Stepper motors are DC brushless and synchronous motors. They rotate in discrete steps of predefined values and are able to rotate both clockwise and anticlockwise. Unlike other DC motors, they provide a precise position control according to the number of steps per revolution for which the motor is designed. That means a complete one revolution of a stepper motor is divided into a discrete number of steps. They are commonly used in CNC machines, Robotics, 2D and 3D printers.
For this guide, we will use a 28BYJ-48 stepper motor and control it through ULN2003 motor driver.
28BYJ-48 Stepper Motor
This is the most commonly used stepper motor in low power industrial and most famously in hobbyist projects.
28BYJ-48 is a uni-polar 5V stepper motor that takes electrical signals as input and rotates by converting these input signals into mechanical rotation. It consists of 4 stationary coils rated at +5V. These coils are known as a stator and make a ring around the rotor. Because of 5 volts operating voltage, we can easily drive this motor from any microcontroller such as Raspberry Pi Pico, ESP32, ESP8266, Arduino or TM4C123 Tiva Launchpad, etc. It has a 1/64 reduction gear set and therefore moves in precise 512 steps per revolution. These motors are silent in comparison to other DC motors and servo motors. You can achieve positional control easily without needing extra circuitry and components.
This stepper motor has a stride angle of 5.625 degrees. That means 28BYJ-48 will complete one revolution in (360/5.625) 64 steps by taking one step at a time and in one step it covers a 5.625-degree distance. However, the stepper motor can also be used in full-step mode. In full-step mode, the angle of each step is 11.25 degrees. That means the motor completes its one revolution in 32 steps instead (360/11.25).
Therefore, in order to move one step forward or backward, the coils of the motor energize with a particular sequence.
Steps per Revolution & Step Angle
The output shaft of this particular stepper motor is driven through a gear ratio of 64:1 that is also known as the speed variation ratio. This suggests that after the inside motor rotates 64 times then the shaft will complete one rotation.
Therefore we can conclude that:
- In order to complete one full rotation of the shaft a total of 2048 steps will be required. This is known as the steps per revolution calculated by multiplying 32 and 64 (32×64=2048).
- Moreover, this will enable the motor to have a step angle of 360º/2048 steps = 0.18º/step.
- It is a unipolar 5 pin coil with a rated DC voltage of 5V.
- Has 4 phases with a stride angle of 5.625°/64.
- Speed variation ratio is 1/64
- The frequency of this stepper motor is 100Hz and insulated power is 600VAC/1mA/1s.
- The half-step method is recommended for driving this stepper motor.
- The value of pull in torque for a stepper motor is 300 gf-cm.
The following figure shows the pinout diagram of 28BYJ-48 stepper motor. It consists of 5 pins. Out of these 5 pins, four pins are used to provide sequence logic to the coils and one pin is a +5 volts supply pin.
Pin Configuration Details
|Pin Number||Coil Number||Colour|
Coil 1-Coil 4: These are coils used to control the step sequence of the stepper motor. One end of each coil is connected with +5V and the other end will be connected with ULN2003 driver output.
Vcc: Used to apply +5 volt supply to the stepper motor. This voltage appears across the coils when a specific coil is ground through a control sequence.
ULN2003 Stepper Motor Driver Module
To use a 28BYJ-28 stepper motor with Raspberry Pi Pico, we will be required to attach it with the ULN2003 motor driver. This is necessary because the current consumption of 28BYJ-48 is around 240mA. That means the current required to drive coils by applying a sequence of control signals is also almost 200mA. The pins of Raspberry Pi Pico can not provide current of this magnitude. Therefore, we need a ULN2003 driver which translates low current output of Raspberry Pi Pico pins into higher current that meets the requirement of stepper motor control signals.
The ULN2003 breakout board has high current and voltage than a single transistor and therefore it can drive a stepper motor easily by enabling our Raspberry Pi Pico.
Recommneded reading: ULN2003 introduction, pinout, example and features
Stepper Motor Driver Module Pinout
ULN2003 driver IC consists of 7 Darlington pair transistor outputs. Each output can drive 500mA and 50V load. The input to each 7 Darlington pair transistor will be a signal from our microcontroller. To drive a stepper motor, this driver board uses only four input pins (IN1, IN2, IN3, and IN4).
The following diagram shows the ULN2003 motor driver board:
Motor Connector Header is used to connect the stepper motor. It provides output from four Darlington pair transistors.
|1N1 to IN4||These are input pins used to provide control signals to the stepper motor such as control sequences. We will connect these pins with the GPIO pins of Raspberry Pi Pico.|
|Vcc and GND||Vcc is a power supply pin and it is used to provide 5 volts power to the stepper motor from an external power source.|
The 28BYJ-48 stepper motor requires 240mA current to operate and it also consumes power at an idle condition. Therefore, it is recommended not to power the 28BYJ-48 stepper motor directly from any microcontroller. Instead use an external 5 volts power supply.
Interfacing Raspberry Pi Pico with 28BYJ-48 Stepper Motor and ULN2003 motor driver
To connect Raspberry Pi Pico with the stepper motor and driver we will use the input pins IN1-IN4, the power supply pins, and the motor connection header. The 28BYJ-48 stepper motor already comes attached with the motor driver via the motor connector header. Now we will connect four GPIO pins of our Raspberry Pi Pico board with the input pins (IN1-IN4) of the driver. We have used the following GPIO pins to connect with each input pin.
|Raspberry Pi Pico||Motor Driver|
You can use any other suitable Pi Pico GPIO pins as well.
Additionally, we will power the motor driver with a 5V external power supply. Both the grounds of the power supply and the microcontroller will be in common.
The connection diagram is shown in the picture below.
MicroPython Script: 28BYJ-48 Stepper Motor with Raspberry Pi Pico
This MicroPython code will help us rotate the stepper motor in clockwise direction. In order to do that we have to provide a sequence of values (1 (high state), 0 (low state)) to the stepper motor pins.
from machine import Pin from time import sleep IN1 = Pin(2,Pin.OUT) IN2 = Pin(3,Pin.OUT) IN3 = Pin(4,Pin.OUT) IN4 = Pin(5,Pin.OUT) pins = [IN1, IN2, IN3, IN4] sequence = [[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]] while True: for step in sequence: for i in range(len(pins)): pins[i].value(step[i]) sleep(0.001)
How the Code Works?
We will start by importing the Pin class from the machine module and the sleep class from the time module.
from machine import Pin from time import sleep
Configure the GPIO pins connected with IN1, IN2, IN3, IN4 pins as output pins. This is done by using the Pin() method and passing the GPIO number as the first parameter and Pin.OUT as the second parameter.
Next, create an array called ‘pins’ consisting of the four output pins IN1, IN2, IN3 and IN4.
IN1 = Pin(2,Pin.OUT) IN2 = Pin(3,Pin.OUT) IN3 = Pin(4,Pin.OUT) IN4 = Pin(5,Pin.OUT) pins = [IN1, IN2, IN3, IN4]
Then create a 2d array called ‘sequence’ to hold the sequence of values that we will provide to the stepper motor pins. These values are 1 and 0 indicating a high state and a low state.
Note: Reversing this sequence of numbers will move the stepper motor in the anti-clockwise direction.
sequence = [[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]]
Inside the infinite loop, we will step through the sequence and set each pin the value of the sequence in order. This will be done after a delay of 0.001 seconds to ensure proper movement of the stepper motor.
while True: for step in sequence: for i in range(len(pins)): pins[i].value(step[i]) sleep(0.001)
To see the demo of above code, copy this code to Thonny IDE and upload it to Raspberry Pi Pico. Also, attach the stepper motor and ULN2003 with Raspberry Pi Pico according to the connection diagram above.
The stepper motor will move in a clockwise direction as shown in the video below:
You may like to read dc motor and servo motor interfacing with Raspberry Pi Pico:
- Servo Motor with Raspberry Pi Pico using MicroPython
- Control DC Motor using L298N Driver with Raspberry Pi Pico and MicroPython
Related tutorials and projects:
- Stepper Motor Interfacing with TM4C123 Tiva Launchpad
- Stepper Motor Control with L293D Motor Driver IC and Arduino
- 28BYJ-48 5 volt Stepper Motor Guide
- NEMA 23 Stepper Motor Pinout, features and example with Arduino
- STEPPER MOTOR INTERFACING WITH 8051 MICROCONTROLLER
- TMC2209 Stepper Motor Driver module – SilentStepStick
- NEMA 34 Stepper Motor
- STEPPER MOTOR INTERFACING WITH PIC16F877A MICROCONTROLLER