Last Updated on 2016-11-14.
[:en]This tutorial gives you a quick and simple idea how to control a stepper motor via Raspberry Pi and a Python script.
Parts
- Raspberry Pi 3
- Deek-Robot Stepper Motor Shield v1 (includes an L293D motor driver)
- Nema 17 stepper motor, Type 42HS34-1334-04LA (4-pin, bipolar)
Connection between motor and shield
- Red: A-
- Green: A+
- Yellow: B-
- Blue: B+
Red+Green and Yellow+Blue are one phase each, in this case (4-pin bipolar motor) it does not matter if you swap Red with Green or Yellow with Blue.
Connection between shield and RPi
- 5V+ (VCC): Pin 2
- GND: Pin 6
- IN1: Pin 12
- IN2: Pin 16
- IN3: Pin 18
- IN4: Pin 22
- VIN:
Pin 4(not needed; or separate power source) - GND (near VIN):
Pin 10(not needed; or separate power source)
For INx, you can of course choose other GPIO ports on your RPi, but you have to fit the script.
Python Script
import RPi.GPIO as GPIO import time # Variables delay = 0.0055 steps = 500 GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) # Enable pins for IN1-4 to control step sequence coil_A_1_pin = 18 coil_A_2_pin = 23 coil_B_1_pin = 24 coil_B_2_pin = 25 # Set pin states GPIO.setup(coil_A_1_pin, GPIO.OUT) GPIO.setup(coil_A_2_pin, GPIO.OUT) GPIO.setup(coil_B_1_pin, GPIO.OUT) GPIO.setup(coil_B_2_pin, GPIO.OUT) # Function for step sequence def setStep(w1, w2, w3, w4): GPIO.output(coil_A_1_pin, w1) GPIO.output(coil_A_2_pin, w2) GPIO.output(coil_B_1_pin, w3) GPIO.output(coil_B_2_pin, w4) # loop through step sequence based on number of steps for i in range(0, steps): setStep(1,0,1,0) time.sleep(delay) setStep(0,1,1,0) time.sleep(delay) setStep(0,1,0,1) time.sleep(delay) setStep(1,0,0,1) time.sleep(delay) # Reverse previous step sequence to reverse motor direction for i in range(0, steps): setStep(1,0,0,1) time.sleep(delay) setStep(0,1,0,1) time.sleep(delay) setStep(0,1,1,0) time.sleep(delay) setStep(1,0,1,0) time.sleep(delay)
Pictures
Important
In general, I highly recommend to only power the shield (especially the motor) when it is needed, not all the time. The motor takes about 0,5A even when idle, and the L293D chip gets quite hot.
So use a switch etc. for the power line, or maybe use another driver chip.
References:
https://www.raspberrypi.org/forums/viewtopic.php?f=49&t=55580
http://www.elektronx.de/tutorials/schrittmotorsteuerung-mit-dem-raspberry-pi/
[:]
thanks for this..
i want to control bipolar stepper motor with l293d shield (i.e, arduino motor driver) using raspberry pi3.
So, please help me how can i connect and programmed in python that l293d shield can be control the stepper motor using raspberry pi.
Thanks…