How to run a stepper motor with Raspberry PLC
Code in Python and make the most out of the Raspberry PLC for industrial automation
11 October, 2021 by
How to run a stepper motor with Raspberry PLC
Boot & Work Corp. S.L., Fernandez Queralt Martinez

INTRODUCTION

As we mentioned in the previous blog >, stepper motors in the world of automation are commonly used to run conveyor belts or 3D printers, among others.

In this blog, we are going to show you how to execute a Python code in order to make your stepper motor run!

Latest Posts

RPI.GPIO PYTHON LIBRARY

Once the Microstep driver and the stepper motor are right connected to the Raspberry PLC, we are going to proceed to make the stepper motor run.

So first of all, we are going to configure the pins as the stepper motor requires a way of trasmitting a PWM signal to the controller.

import RPi.GPIO as GPIO
from time import sleep

# Pin 10 from Raspberry Pi: Uart RX
DIR = 10

# Pin 8 from Raspberry Pi: Uart TX
STEP = 8

# 0 or 1 to set the sense of rotation: clockwise or counterclockwise.
CW = 1
CCW = 0

# Setup pin layout on PI
GPIO.setmode(GPIO.BOARD)

# Setup the GPIO mode of each pin
GPIO.setup(DIR, GPIO.OUT)
GPIO.setup(STEP, GPIO.OUT)

PWM SIGNAL

In order to better setup the stepper motor, you must know what your motor amp rating is, since if your setting is on 1 microstep, then 200 steps will be needed to do one full revolution.


So, in the code below, we will do the following:

1. Set the first direction to spin

2. Set a time sleep to leave a time to switch the direction.

3. Run for the steps needed by your microcontroller. And set the TX pin to LOW. Sleep with the speed for the motor to run. Set the TX pin to LOW.  Sleep with the speed for the motor to run. 

4. Set a time sleep to leave a time to switch the direction again.

5. Finally, repeat the step 3 with the counterclockwise sense.

GPIO.output(DIR, CW)

try:
     while True:
                sleep(1.0)
                GPIO.output(DIR,CW)  for x in range(200):
GPIO.output(STEP,GPIO.HIGH) sleep(.005)
                        GPIO.output(STEP,GPIO.LOW) sleep(.005)
                sleep(1.0) GPIO.output(DIR,CCW)
for x in range(200): GPIO.output(STEP,GPIO.HIGH) sleep(.005) GPIO.output(STEP,GPIO.LOW) sleep(.005) except KeyboardInterrupt: print("cleanup") GPIO.cleanup(

FULL CODE

Now, copy all the code below, paste it in a file, execute it and make your stepper motor run!


import RPi.GPIO as GPIO
from time import sleep

DIR = 10
STEP = 8

CW = 1
CCW = 0

GPIO.setmode(GPIO.BOARD)

GPIO.setup(DIR, GPIO.OUT)
GPIO.setup(STEP, GPIO.OUT)

GPIO.output(DIR, CW)

try:
while True:
sleep(1.0)
GPIO.output(DIR,CW)

for x in range(200):
GPIO.output(STEP,GPIO.HIGH)
sleep(.005)
GPIO.output(STEP,GPIO.LOW)
sleep(.005)

  sleep(1.0)
GPIO.output(DIR,CCW)

for x in range(200):
GPIO.output(STEP,GPIO.HIGH)
sleep(.005)
GPIO.output(STEP,GPIO.LOW)
sleep(.005)

except KeyboardInterrupt:
print("cleanup")
GPIO.cleanup()

 
 

 Now, would you like to make a Node-RED Dashboard to better start and stop the stepper motor? 

Read more >>

How to run a stepper motor with Raspberry PLC
Boot & Work Corp. S.L., Fernandez Queralt Martinez
11 October, 2021
Share this post
Archive

Looking for your ideal Programmable Logic Controller?

Take a look at this product comparison with other industrial controllers Arduino-based. 

We are comparing inputs, outputs, communications and other features with the ones of the relevant brands.


Industrial PLC comparison >>>

Do you want more information?

Just fill the form!

Tell me more!