Raspberry Pi Stepper Motor 28BYJ-48

Too set up your stepper motor you first need to make the circuit. You can connect the motor straight to the raspberry pi if you would like. I used this link. Make sure you know what GPIO you use for the four inputs.

Next, you need to set up the code. I used the link below for the initial code and then modified it to specific uses I wanted the motor to do. You will need to put parentheses around all the print statements.

I wanted to control two motors, direction and how many rotations the motor spins for. For that use the code below.

from time import sleep
import RPi.GPIO as GPIO
import os
import random

GPIO.setmode(GPIO.BCM)

class stepmotor:

    def __init__(self, IN1, IN2, IN3, IN4):

        self.IN1 = IN1 # IN1
        self.IN2 = IN2 # IN2
        self.IN3 = IN3 # IN3
        self.IN4 = IN4 # IN4

        self.time = 0.001

        GPIO.setup(IN1,GPIO.OUT)
        GPIO.setup(IN2,GPIO.OUT)
        GPIO.setup(IN3,GPIO.OUT)
        GPIO.setup(IN4,GPIO.OUT)

        GPIO.output(IN1, False)
        GPIO.output(IN2, False)
        GPIO.output(IN3, False)
        GPIO.output(IN4, False)

    def Step1(self):
        GPIO.output(self.IN4, True)
        sleep (self.time)
        GPIO.output(self.IN4, False)

    def Step2(self):
        GPIO.output(self.IN4, True)
        GPIO.output(self.IN3, True)
        sleep (self.time)
        GPIO.output(self.IN4, False)
        GPIO.output(self.IN3, False)

    def Step3(self):
        GPIO.output(self.IN3, True)
        sleep (self.time)
        GPIO.output(self.IN3, False)

    def Step4(self):
        GPIO.output(self.IN2, True)
        GPIO.output(self.IN3, True)
        sleep (self.time)
        GPIO.output(self.IN2, False)
        GPIO.output(self.IN3, False)

    def Step5(self):
        GPIO.output(self.IN2, True)
        sleep (self.time)
        GPIO.output(self.IN2, False)

    def Step6(self):
        GPIO.output(self.IN1, True)
        GPIO.output(self.IN2, True)
        sleep (self.time)
        GPIO.output(self.IN1, False)
        GPIO.output(self.IN2, False)

    def Step7(self):
        GPIO.output(self.IN1, True)
        sleep (self.time)
        GPIO.output(self.IN1, False)

    def Step8(self):
        GPIO.output(self.IN4, True)
        GPIO.output(self.IN1, True)
        sleep (self.time)
        GPIO.output(self.IN4, False)
        GPIO.output(self.IN1, False)

    def left(self, step):
        for i in range (step):  

            self.Step1()
            self.Step2()
            self.Step3()
            self.Step4()
            self.Step5()
            self.Step6()
            self.Step7()
            self.Step8()  
            #print (“Step left: “),i

    def right(self, step):
        for i in range (step):

            self.Step8()
            self.Step7()
            self.Step6()
            self.Step5()
            self.Step4()
            self.Step3()
            self.Step2()
            self.Step1()  
            #print (“Step right: “),i  

#def move2right(steps):
#    for i in range(half):
#        motor1.right(1)
#        motor2.right(1)
#        sleep(0.05)

half = int(512/2)
quarter = int(512/4)
full = int(512)
twice = int(512*2)

motor1 = stepmotor(2,3,4,10)
motor2 = stepmotor(17,27,22,18)

for i in range(twice*10):
    motor1.right(1)
    motor2.left(1)

GPIO.cleanup()

Leave a Reply