top of page

Stepper Motor Interfacing with Raspberry Pi

Updated: Feb 18



Overview of Stepper Motor



Stepper Motor


  • Stepper motor is a brushless DC motor that divides the full rotation angle of 360° into number of equal steps.


  • The motor is rotated by applying certain sequence of control signals. The speed of rotation can be changed by changing the rate at which the control signals are applied.


  • For more information about Stepper Motor, its control sequence and how to use it, refer the topic Stepper Motor in the sensors and modules section.


  • Raspberry Pi’s GPIOs can be used to control stepper motor rotation. We can generate sequence of control signals on the GPIO pins of Raspberry Pi. To know more about Raspberry Pi GPIO refer Raspberry Pi GPIO Access.


 

Connection Diagram of Stepper Motor with Raspberry Pi


Stepper Motor Interfacing with Raspberry Pi



Rotate Stepper Motor using Raspberry Pi


Let’s rotate a Stepper Motor in clockwise and counter-clockwise directions alternately.


  • Here, we are using six wire unipolar stepper motor. Only four wires are required to control this stepper motor. The two center tap wires of the stepper motor are connected to the 5V supply.


  • ULN2003 driver is used to drive unipolar stepper motor.


  • We will interface Stepper Motor with Raspberry Pi using Python language. In this program, we have used the keyboard key as input for selecting motor rotation direction (i.e. clockwise or anti-clockwise).


Note: To find winding coils and their center tap leads, measure resistance in between the leads. From center leads we will get half the resistance value as compared to resistance between winding ends.


Stepper Motor Python Program for Raspberry Pi

'''
    Stepper Motor interfacing with Raspberry Pi
    http:///www.electronicwings.com
'''
import RPi.GPIO as GPIO
from time import sleep
import sys

#assign GPIO pins for motor
motor_channel = (29,31,33,35)  
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
#for defining more than 1 GPIO channel as input/output use
GPIO.setup(motor_channel, GPIO.OUT)

motor_direction = input('select motor direction a=anticlockwise, c=clockwise: ')
while True:
    try:
        if(motor_direction == 'c'):
            print('motor running clockwise\n')
            GPIO.output(motor_channel, (GPIO.HIGH,GPIO.LOW,GPIO.LOW,GPIO.HIGH))
            sleep(0.02)
            GPIO.output(motor_channel, (GPIO.HIGH,GPIO.HIGH,GPIO.LOW,GPIO.LOW))
            sleep(0.02)
            GPIO.output(motor_channel, (GPIO.LOW,GPIO.HIGH,GPIO.HIGH,GPIO.LOW))
            sleep(0.02)
            GPIO.output(motor_channel, (GPIO.LOW,GPIO.LOW,GPIO.HIGH,GPIO.HIGH))
            sleep(0.02)

        elif(motor_direction == 'a'):
            print('motor running anti-clockwise\n')
            GPIO.output(motor_channel, (GPIO.HIGH,GPIO.LOW,GPIO.LOW,GPIO.HIGH))
            sleep(0.02)
            GPIO.output(motor_channel, (GPIO.LOW,GPIO.LOW,GPIO.HIGH,GPIO.HIGH))
            sleep(0.02)
            GPIO.output(motor_channel, (GPIO.LOW,GPIO.HIGH,GPIO.HIGH,GPIO.LOW))
            sleep(0.02)
            GPIO.output(motor_channel, (GPIO.HIGH,GPIO.HIGH,GPIO.LOW,GPIO.LOW))
            sleep(0.02)

            
    #press ctrl+c for keyboard interrupt
    except KeyboardInterrupt:
        #query for setting motor direction or exit
        motor_direction = input('select motor direction a=anticlockwise, c=clockwise or q=exit: ')
        #check for exit
        if(motor_direction == 'q'):
            print('motor stopped')
            sys.exit(0)

3 views0 comments

Comments


bottom of page