Control a steppermotor with a raspberry Pi

This post describe how to control a steppermotor with a Raspberry Pi.

Supplies needed:

  • Easydriver (i use the 4.4 version)
  • jumper wires (female – male)
  • A raspberry Pi offcourse
  • A breadboard
  • Restistors 4.4k Ohm
  • A steppermotor NEMA 17
  • Installed with Raspbian

EasydriverRaspberry
VCCPIN4 – 5V
STEPPIN16 (GPIO23)
DIRPIN18 (GPIO24)
ENABLEPIN22 (GPIO25)

To connect the Raspberry Pi to the easydriver i use the mapping in this table.

I use a breadboard. This make it easier to connect different wires to the Pi and Easydriver.

Circuit with Raspberry & Easydriver

I used a breadboard to connect the Raspberry Pi with the easydriver and motor. You can also connect the Pi directly to the easydriver.

Secondly i used pulldown resistors on the switching wires. For testing purpose this is not necessary and from Easydriver v4.5 the pulldown resistors are on the easydriver board.
For testingpurpose the motor is powered by the Raspberry Pi. In a real live situation you need an external power supply.

Control the steppermotor with Python

Create the following file: ‘~/motorcontrol.py’.

vim ~/motorcontrol.py

In the file paste the following text

#!/usr/bin/python
import RPi.GPIO as GPIO, time

GPIO.setmode(GPIO.BOARD)
GPIO.setup(16, GPIO.OUT)
GPIO.setup(18, GPIO.OUT)
GPIO.setwarnings(False)
GPIO.output(16, True)

#iemand anders 500
p = GPIO.PWM(16, 5000)

def SpinMotor(direction, num_steps):
    p.ChangeFrequency(5000)
    GPIO.output(18, direction)
    while num_steps > 0:
        p.start(1)
        time.sleep(0.01)
        num_steps -= 1
    p.stop()
    GPIO.cleanup()
    return True

direction_input = raw_input('Please enter O or C fro Open or Close:')
num_steps = input('Please enter the number of steps: ')
if direction_input == 'C':
    SpinMotor(False, num_steps)
else:
    SpinMotor(True, num_steps)

As you can see we use PIN16 and PIN18 as output. Those pins control the directorion and the amount of steps to run. Make the file executable.

chmod +x ~/motorcontrol.py
#and play arround
~/motorcontrol.py

Pin22 is connected to ENABLED. By default the easydriver chip is enabled and it consumes power. When you connect an external power supply with a higher voltage, the chip becomes very hot. Disable the chip by turning this pin on (or off).

About the author