Raspberry Pi Pico With Stepper Motor (MicroPython Guide)

In this tutorial, you will learn how to interface a 28BYJ-48 stepper motor with Raspberry Pi Pico using ULN2003 motor driver IC. MicroPython code has been used in this tutorial to control a stepper motor. We shall discuss interfacing with the ULN2003 IC on a breadboard and also learn to use the commonly used ULN2003 breakout board module.

Components Required

  • Raspberry Pi Pico or Pico W.
  • 5V Stepper Motor (28BYJ-48).
  • ULN2003 stepper motor driver.
  • Breadboard and jumpers/Dupont cables.
Raspberry Pi Pico stepper motor control using ULN2003

ULN2003 Pinout & Specifications

ULN2003 is an integrated circuit that consists of an array of seven Darlington transistors. Below is the pinout of ULN2003.

Each output pin of ULN2003 is capable of handling peak currents up to 600mA and voltage up to 50V. When we send a HIGH signal to a pin on one side, the corresponding pin on the output side gets LOW (Read: What are HIGH and LOW logic levels?). A small current in the input can switch a high current in the output. This property can be used to control a stepper motor. Stepper motors generally require higher current and voltage than that provided by microcontroller pins.

For more details on ULN2003, you can refer to its datasheet.

ULN2003 Stepper Driver Module

The ULN2003 is commonly available as a module board for easy interfacing. The pinout of such a ULN2003 module is provided below.

28BYJ-48 Stepper Motor Overview

The 28BYJ-48 is a unipolar stepper motor that requires 64 half-steps to complete a full 360-degree rotation, with each step having a step angle of 5.625 degrees. To produce the rotating motion, the motor’s four coils are energized in a specific sequence, which is determined by the motor driver circuit. The motor requires a 5V DC voltage to operate. Additionally, the motor has a gearbox attached to it, which reduces its speed and increases its torque. The gearbox has a gear ratio of 1:64, which indicates that for every 64 rotations of the motor shaft, the gearbox output shaft rotates once. This gearbox makes it a powerful stepper motor in a small form factor.

A 12V version of the motor is also available, but this guide will only demonstrate the 5V variant. For more details on the 28BYJ-48 stepper motor, you can view its datasheet.

Stepper Motor Pinout

The image below describes the pinout of the 28BYJ-48 stepper motor.

On the right side of the image above, you can see that internally it consists of 4 coils that can be powered in a sequence to turn the motor. One side of each coil is connected to a common point.

Also read: Arduino vs MicroPython vs CircuitPython: Which One Will You Choose?

Schematic – Raspberry Pi Pico W With Stepper Motor & ULN2003

If you own the ULN2003 driver module, then its schematic with Pico is given below.

For a standalone ULN2003 IC, connect your Raspberry Pi Pico and 28BYJ-48 as shown in the schematic below.

Connection details:

Sl.NoULN2003 PinRaspberry Pi Pico PinStepper Motor Wire Color
1INPUT 1GPIO 12
2INPUT 2GPIO13
3INPUT 3GPIO 14
4INPUT 4GPIO 15
5OUTPUT 1BLUE
6OUTPUT 2PINK
7OUTPUT 3YELLOW
8OUTPUT 4ORANGE
9GNDGND
10COMMONRED
Table: Connection between Raspberry Pi Pico, ULN2003 & Stepper Motor (Pins shown on the same row are connected using wires).

The COM pin of ULN2003 should be connected to an external 5V power supply, such as a power bank. The ground pins of ULN2003, Raspberry Pi Pico, and the external power supply must be connected to each other for the circuit to work. You can also refer to our Raspberry Pi Pico pinout guide.

Working Of The Circuit

When a pin(GPIO) of Pico is set to HIGH, an input pin of ULN2003 gets the HIGH signal. This switches the corresponding output pin of ULN2003 to LOW i.e. the output pins of ULN2003 act as current sink. By switching the GPIOs, the coils in the stepper motor can be energized. For example, if GPIO12 is switched HIGH, OUTPUT 1 will act as a current sink, and current will flow through the coil connected to the BLUE wire. In this way, we can switch the GPIOs in a sequence to turn the motor.

MicroPython Code For Raspberry Pi Pico Stepper Motor Control

To program Raspberry Pi Pico with MicroPython, we need to load it with a MicroPython UF2 file first. You can read our guide on getting started with Raspberry Pi Pico to learn all the steps required to program it in MicroPython. The guide has details on uploading code using Thonny IDE and uPyCraft IDE.

If you are using macOS, follow our guide to program Raspberry Pi Pico on macOS using Thonny IDE.

The following steps show how we can upload MicroPython code to control the stepper motor using the Thonny IDE.

  • With all connections done as shown in the schematic above, connect the Pico to your computer using a USB cable. Open Thonny IDE, and paste the following code into a new project.
#Source: electrocredible.com, Language: MicroPython
import utime
from machine import Pin

# Define the pins for the stepper motor
stepper_pins = [Pin(12, Pin.OUT), Pin(13, Pin.OUT), Pin(14, Pin.OUT), Pin(15, Pin.OUT)]

# Define the sequence of steps for the motor to take
step_sequence = [
    [1, 0, 0, 1],
    [1, 1, 0, 0],
    [0, 1, 1, 0],
    [0, 0, 1, 1],
]

def step(direction, steps, delay):
    # Use the global step_index variable so that it can be modified by this function
    global step_index
    # Loop through the specified number of steps in the specified direction
    for i in range(steps):
        # Add the specified direction to the current step index to get the new step index
        step_index = (step_index + direction) % len(step_sequence)
        # Loop through each pin in the motor
        for pin_index in range(len(stepper_pins)):
            # Get the value for this pin from the step sequence using the current step index
            pin_value = step_sequence[step_index][pin_index] 
            # Set the pin to this value
            stepper_pins[pin_index].value(pin_value)
        # Delay for the specified amount of time before taking the next step
        utime.sleep(delay)
# Set the initial step index to 0
step_index = 0
# Take the specified number of steps in the anti-clockwise direction with a delay of 0.01 seconds between steps
step(1, 500, 0.01)
# Take the specified number of steps in the clockwise direction with a delay of 0.01 seconds between steps
step(-1, 500, 0.01)

Code language: Python (python)
  • Run the code by clicking the Run icon or by pressing the F5 key.
run-button-Thonny-1
  • Save the script to your Raspberry Pi Pico.
Thonny Save to
  • Save the script as main.py or with any other name with a “.py” filename extension.
save as main.py in Thonny

Result

When you run the code, the stepper motor must run for a few seconds in the anti-clockwise direction and then for a few seconds in the clockwise direction. You can change the direction, speed, and duration of rotation by making changes in the code.

Stepper Control MicroPython Code Explained

We first import the necessary modules. The utime module is required to introduce delays in our code and the Pin module helps us to set GPIOs of Pico as output.

import utime
from machine import PinCode language: JavaScript (javascript)

GPIOs 12, 13, 14, and 15 are set as control pins for the stepper motor.

stepper_pins = [Pin(12, Pin.OUT), Pin(13, Pin.OUT), Pin(14, Pin.OUT), Pin(15, Pin.OUT)]

We define a 2-D array called step_sequence that holds the sequence in which the GPIOs must switch. In our code, coils 1 & 4 will first energize, then coils 1 & 2, and so on. The sequence repeats once the stepper motor completes a full revolution.

step_sequence = [
    [1, 0, 0, 1],
    [1, 1, 0, 0],
    [0, 1, 1, 0],
    [0, 0, 1, 1],
]

Note that we are using a full-step sequence to turn the stepper. In a full-step sequence, two coils of a stepper are energized at the same time. Alternatively, you can also use a half-step sequence to rotate the stepper. In half-step switching, the step angle reduces to half that of the full-step sequence and the number of steps gets doubled. This allows fine control of a stepper motor. Modify the array as shown below to implement half-step sequence switching of the stepper.

 step_sequence = [
    {1, 0, 0, 0},
    {1, 1, 0, 0},
    {0, 1, 0, 0},
    {0, 1, 1, 0},
    {0, 0, 1, 0},
    {0, 0, 1, 1},
    {0, 0, 0, 1},
    {1, 0, 0, 1}
]

The step() function takes three arguments – the direction the motor will turn(clockwise/anti-clockwise), the steps to turn, and the delay between each step.

def step(direction, steps, delay):

The code below will turn the stepper motor. The function call step(1, 500, 0.01) will turn the motor in the clockwise direction for 500 steps with a delay of 0.01 seconds between the steps.

step(1, 500, 0.01)
step(-1, 500, 0.01)Code language: Python (python)

Applications

Here are some project ideas which you can make using Raspberry Pi Pico and stepper motor:

  • CNC Machine.
  • 3D printer.
  • Robotic Arm.
  • Camera slider.
  • Pet Feeder.

Also Read: Logic Level Converter – How It Works? Circuit, Uses Explained.


Posted

in

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *