How to run a stepper motor with industrial Raspberry PLC

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

Introduction

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

In this post, we are going to show you how to execute Python code in order to make your stepper motor run with Raspberry Pi  automation!

Your Dynamic Snippet will be displayed here... This message is displayed because you did not provided both a filter and a template to use.

Requirements

RPI.GPIO Python Library

Once the Microstep driver and the stepper motor are right connected to the Raspberry Pi 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 transmitting a PWM signal to the industrial Raspberry Pi PLC.

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 make one complete revolution.

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

1. Set the first direction to spin.

2. Set a time to 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 to sleep to leave a time to switch the direction again.

5. Finally, repeat 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 on your Raspberry Pi industrial PLC 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 for your open source PLC Raspberry Pi to better start and stop the stepper motor? 

Read more >>

​Search in our Blog

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

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 >>>