Stepper Motor with Raspberry PI Pico: 28BYJ-48 and ULN2003 wiring and MicroPython code

Stepper motors can trasform your code logic into transfer in robotic initiatives. The 28BYJ-48 stepper motor (with ULN2003 motor driver) is the most cost effective mannequin and works with a easy code on Rapsberry PI Pico

On this tutorial, I’m going to indicate you the right way to wire and use the 28BYJ-48 and ULN2003 with Raspberry PI Pico, with MicroPython code.

Please notice that, when you want the code for Raspberry PI pc boards, you'll be able to confer with my Controlling a stepper motor with Raspberry Pi Zero W tutorial.

28BYJ-48 stepper motor and ULN2003

A stepper motor is a brushless DC electrical motor. Their {hardware} makes it attainable to divide a full rotation into plenty of equal steps. In comparison with frequent DC motors, they're much more exact however they've much less rotation velocity. They normally match properly into purposes to manage cameras, robotic arms, or at any time when you should carry out exact rotations. When selecting the stepper motor to make use of on your challenge, please test fastidiously the required torque drive and velocity (normally out there from the producer datasheet), as it could affect your remaining consequence.

As stepper motors are present hungry gadgets, a motor driver is normally included to handle the facility provide and maintain it remoted from microcontroller circuits.

The 28BYJ-48 stepper motor works by powering a particular sequence into the ULN2003 driver Pins. The states for every sequence step are the next:


To maneuver the stepper it's important to produce sequence 1 first on the pins, then sequence 2, then sequence 3, and so forth. Shifting the stepper in other way may be achieved simply by working within the reverse sequence (sequence 0, sequence 7, sequence 6, and so forth).

On this tutorial, I’m going to make use of the 28BYJ-48 stepper motor and the ULN2003 motor driver, that are normally offered collectively.

What We Want

As regular, I recommend including from now to your favourite e-commerce buying cart all wanted {hardware}, in order that on the finish it is possible for you to to judge total prices and resolve if proceed with the challenge or take away them from the buying cart. So, {hardware} will likely be solely:

Examine {hardware} costs with the next hyperlinks:

amazon raspberry pi pico box
Amazon stepper motor box
Amazon Dupont Wiring box

Step-by-Step Process

Put together Wiring

Please prepare cabling in keeping with the next diagram, in keeping with Raspberry PI Pico pinout:

raspberry pi pico stepper motor wiring diagram

Please discover under some element photos from my lab:

raspberry pi pico stepper motor details 01
raspberry pi pico stepper motor details 02
raspberry pi pico stepper motor details 03
raspberry pi pico stepper motor details 04

Get and Perceive my Code

Now obtain the next file to make use of your 28BYJ-48 stepper motor with ULN2003:

You'll be able to each load it in your Raspberry PI Pico storage or run it out of your pc.

I’ll clarify all code strains within the following paragraphs.

Initially, required modules are imported:

from machine import Pin
from time import sleep

Then we outline some variables. The primary one identifies what are the GP quantity from Raspberry PI Pico related with ULN2003, so as with IN1, IN2, IN3, and IN4. In response to my cabling, I take advantage of GP0, GP1, GP2, and GP3, so:

motor_GP = [0,1,2,3]

Then I take advantage of a sequence pointer. That is going to be a world variable in order that the operate which strikes the stepper motor is ready to replace it at every step:


I additionally use a “stepper_obj” array. This may embody the 4 Pin objects of Raspberry PI Pico, with the intention to get the next code simplified:

stepper_obj = []

The final variable lists all of the sequence steps as an array of array. Please notice that arrSeq[0] is the same as Seq0 from the earlier desk:

arrSeq = [[0,0,0,1],

Earlier than going into the primary operate/loop, I initialize all of the Raspberry PI Pico pins to output and append these objects (the Pin() operate creates an object) to my stepper_obj. As you'll be able to see, the array construction permits setting all of the 4 Raspberry PI Pico Pins with a single-line loop. The person is warned that Pins are going to be initialized with a shell message:

print("Setup pins...")
for gp in motor_GP: stepper_obj.append(Pin(gp, Pin.OUT))

The stepper_move() operate is the place all of the stuff is managed. It requires an enter variable: route. It have to be “+1” or “-1” with the intention to have ahead or backward rotation.

The “world” assertion makes this operate in a position to edit the seq_pointer variable:

def stepper_move(route): # route have to be +1 or -1
    world seq_pointer

At first of this script, the seq_pointer equals to [0,1,2,3,4,5,6,7]. Executing “seq_pointer[1:]+seq_pointer[:1]” we’ll get a brand new worth for seq_pointer equal to [1,2,3,4,5,6,7,0]. The next execution will implement the identical rotation.

On the opposite aspect, from seq_pointer = [0,1,2,3,4,5,6,7], executing “seq_pointer[-1:]+seq_pointer[:-1]” we’ll get a brand new worth for seq_pointer equal to [7,0,1,2,3,4,5,6].

At every iteration the primary worth of seq_pointer array would be the actual pointer for use to pick out the right way to energy GP pins in keeping with arrSeq map:


So, seq_pointer[0] will likely be our pointer at each step. Used as index for arraySeq, we’ll get the ordered checklist of values to set into our Pins. For instance:

  • when seq_pointer = [7,0,1,2,3,4,5,6]
  • -> seq_pointer[0] = 7
  • -> arrSeq[seq_pointer[0]] = arrSeq[7] = [1,0,0,1]

So, the next row calculates the facility to set at every Raspberry PI Pico Pin and assign that worth inside a single for loop:

    for a in vary(4): stepper_obj[a].worth(arrSeq[seq_pointer[0]][a])

Earlier than closing the stepper_move() operate, we add a delay as a result of a too quick execution will end in no transfer…:


That carried out, transferring the 28BYJ-48 stepper ahead will likely be achieved by calling the stepper_move() operate with route parameter set to 1, as within the following loop:

whereas True:

If you should transfer backward, you simply want to alter “1” into “-1”:

whereas True:

Operating the Script

Run this script in your Thonny IDE (F5) and your stepper motor will begin rotating.

Take pleasure in!

Source link

Leave a Reply

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