• sales

    +86-0755-88291180

Raspberry Pi Stepper Motor HAT User Guide

一、Overview

This is a perfect solution for making motion robots based on Raspberry Pi. This motor driver is powerful enough to drive 4 DC motors or 2 stepper motors, and additionally provides 4 channels of full-speed PWM control, which can control 4 channels of servos . With its own voltage regulator circuit, it directly provides a stable power supply for the Raspberry Pi.

Raspberry Pi does not have enough PWM pins, using a dedicated PWM driver chip (PCA9685) based on I2C communication to control the speed and direction of the motor can save more Raspberry Pi resources. Only 2 GPIO pins (SDA & SCL) can be used to drive multiple motors, or multiple drivers can be stacked and used (cascadable) without interfering with the use of other I2C devices.

Features

  • Each 2.5A (3.6A peak) output can run 4.5-13.5V DC motors;
  • Can drive 4 DC motors or 2 stepper motors;
  • Provide 8 additional PWM outputs, can control 8 servos
  • I2C address can be customized, and multiple drivers can be stacked and cascaded;
  • Integrates 5V regulator, allows providing power to Raspberry Pi
  • Comes with development resources and manual (python)

二、Power description

  • Because the driver needs to provide power for both the motor and the Raspberry Pi, it is recommended to use a power battery as the power source. The power supply voltage is 6-12V;
  • The motor is directly powered by the power supply, so the input voltage of the motor is equal to the external power supply voltage;
  • The driver has a built-in voltage regulator module, and outputs 5V2A to supply power to the Raspberry Pi and 4-channel PWM (servo);
  • Different power supply modes can be selected by jumpers.

三、Software Installation

The driver is based on Raspberry Pi and runs in a linux environment, and the driver provides a Python library and related sample codes.

First of all, you need to make the I2C of the Raspberry Pi in Enable (for detailed configuration, please refer to the online information)

*Run the apt-get install python-smbus git installation smbus

Upload the code to the Raspberry Pi (eg "/home/pi") and try running the examples in the examples directory.

*If it prompts that the class library is not found, please configure the relevant library files, or directly copy the column library files to the current directory.

1、Control DC motor (DC motor)

a)Connect the motor

There are 4 sets of motor terminals on the driver board, which are M1, M2, M3, and M4. If you run the DCTest.py example, connect the motor to the M3 interface. Then run the following command:

sudo python DCTest.py
#!/usr/bin/python
from Adafruit_MotorHAT import Adafruit_MotorHAT, Adafruit_DCMotor
import time
import atexit
# create a default object, no changes to I2C address or frequency
mh = Adafruit_MotorHAT(addr=0x60)
# recommended for auto-disabling motors on shutdown!
def turnOffMotors():
mh.getMotor(1).run(Adafruit_MotorHAT.RELEASE)
mh.getMotor(2).run(Adafruit_MotorHAT.RELEASE)
mh.getMotor(3).run(Adafruit_MotorHAT.RELEASE)
mh.getMotor(4).run(Adafruit_MotorHAT.RELEASE)
atexit.register(turnOffMotors)
################################# DC motor test!
myMotor = mh.getMotor(3)
# set the speed to start, from 0 (off) to 255 (max speed)
myMotor.setSpeed(150)
myMotor.run(Adafruit_MotorHAT.FORWARD);
# turn on motor
myMotor.run(Adafruit_MotorHAT.RELEASE);
while (True):
print "Forward! "
myMotor.run(Adafruit_MotorHAT.FORWARD)
print "\tSpeed up..."
for i in range(255):
myMotor.setSpeed(i)
time.sleep(0.01)
print "\tSlow down..."
for i in reversed(range(255)):
myMotor.setSpeed(i)
time.sleep(0.01)
print "Backward! "
myMotor.run(Adafruit_MotorHAT.BACKWARD)
print "\tSpeed up..."
for i in range(255):
myMotor.setSpeed(i)
time.sleep(0.01)
print "\tSlow down..."
for i in reversed(range(255)):
myMotor.setSpeed(i)
time.sleep(0.01)
print "Release"
myMotor.run(Adafruit_MotorHAT.RELEASE)
time.sleep(1.0)

b)DC motor Detailed example

Create a MotorHAT object and set the I2C address, the default address is 0x60 (for detailed address settings, refer to the following content)

mh = Adafruit_MotorHAT(addr=0x60)

Create a DC motor object

MotorHAT has a getMotor(num) method, num is 1~4.

myMotor = mh.getMotor(3)

Set the motor rotation speed

myMotor.setSpeed(150)

setSpeed(speed)

speed :0 (off)~ 255 (maximum!).

*The value of *speed is 0 to 255, and the speed is a relative value, which is related to the power supply voltage.

Set DC Motor direction

myMotor.run(Adafruit_MotorHAT.FORWARD)

 run(direction)

direction Defined as follows:

  • Adafruit_MotorHAT.FORWARD – DC motor spins forward
  • Adafruit_MotorHAT.BACKWARD – DC motor spins forward
  • Adafruit_MotorHAT.RELEASE – DC motor is ‘off’, not spinning but will also not hold its place.

2、Use Stepper Motors (V5 version has not been updated yet, and does not support stepper motors)

The driver board can be connected to 2 stepper motors at the same time, both unipolar and bipolar are applicable.

a)Connect the motor Stepper Motors

In the above picture, the left is the bipolar drive Bipolar stepper motors (4 lines), the right is the unipolar drive Unipolar Stepper Motor (6 lines)

  • Unipolar Stepper Motor (6 wires): First, you need to know which wire is the center wire, then connect the central wire to the GND of the binding post (white and yellow in the right picture), and the remaining two sets of end wires (black and green) , red and blue) are connected to M1, M2 or M3, M4 respectively.
  • Bipolar stepper motors (4 wires): Similar to unipolar drive, just leave GND blank.

StepperTest.py Example

Connect the stepper motor to M1, M2 and run it in the examples directory:

#!/usr/bin/python
#import Adafruit_MotorHAT, Adafruit_DCMotor, Adafruit_Stepper
from Adafruit_MotorHAT import Adafruit_MotorHAT, Adafruit_DCMotor, Adafruit_StepperMotor
import time
import atexit
# create a default object, no changes to I2C address or frequency
mh = Adafruit_MotorHAT()
# recommended for auto-disabling motors on shutdown!
def turnOffMotors():
mh.getMotor(1).run(Adafruit_MotorHAT.RELEASE)
mh.getMotor(2).run(Adafruit_MotorHAT.RELEASE)
mh.getMotor(3).run(Adafruit_MotorHAT.RELEASE)
mh.getMotor(4).run(Adafruit_MotorHAT.RELEASE)
atexit.register(turnOffMotors)
myStepper = mh.getStepper(200, 1) # 200 steps/rev, motor port #1
myStepper.setSpeed(30) # 30 RPM
while (True):
print("Single coil steps")
myStepper.step(100, Adafruit_MotorHAT.FORWARD, Adafruit_MotorHAT.SINGLE)
myStepper.step(100, Adafruit_MotorHAT.BACKWARD, Adafruit_MotorHAT.SINGLE)
print("Double coil steps")
myStepper.step(100, Adafruit_MotorHAT.FORWARD, Adafruit_MotorHAT.DOUBLE)
myStepper.step(100, Adafruit_MotorHAT.BACKWARD, Adafruit_MotorHAT.DOUBLE)
print("Interleaved coil steps")
myStepper.step(100, Adafruit_MotorHAT.FORWARD, Adafruit_MotorHAT.INTERLEAVE)
myStepper.step(100, Adafruit_MotorHAT.BACKWARD, Adafruit_MotorHAT.INTERLEAVE)
print("Microsteps")
myStepper.step(100, Adafruit_MotorHAT.FORWARD, Adafruit_MotorHAT.MICROSTEP)
myStepper.step(100, Adafruit_MotorHAT.BACKWARD, Adafruit_MotorHAT.MICROSTEP)

b)Detailed example

Create a MotorHAT object and set the I2C address, the default address is 0x60 (for detailed address settings, refer to the following content)

mh = Adafruit_MotorHAT(addr=0x60)

create Stepper motor object

myStepper = mh.getStepper(200, 1) # 200 steps/rev, motor port #1

getStepper(steps, portnum)

steps :How many steps per revolution (usually between 35 and 200)

portnum :1~2,Port #1 is M1 and M2, port #2 is M3 and M4

Set Speed, RPM(RPM)

myStepper.setSpeed(30) # 30 RPM

setSpeed() Set Speed, RPM(RPM)

step() – blocking steps

stepper1.step(100, Adafruit_MotorHAT.FORWARD, Adafruit_MotorHAT.SINGLE)

step(numberofsteps, direction, type)

numbersteps:Step count

direction:FORWARD or BACKWARD

type:SINGLE, DOUBLE, INTERLEAVE or MICROSTEP

*Please refer to the online information for the working characteristics of the stepper motor.

Using “Non-blocking” oneStep()

oneStep(direction, stepstyle) single step

3、Servo control

The servo control of the driver board is realized by the PWM special chip PCA9685, and the corresponding numbers are 0-7 respectively. Since ordinary servos on the market usually only support 50Hz signals, the frequency of pwm.setPWMFreq(50) is controlled at around 50.

from Adafruit_PWM_Servo_Driver import PWM
import time
# Initialise the PWM device using the default address
pwm = PWM(0x60,debug = False)
servoMin = 150 # Min pulse length out of 4096
servoMax = 600 # Max pulse length out of 4096
def setServoPulse(channel, pulse):
pulseLength = 1000000.0 # 1,000,000 us per second
pulseLength /= 50.0 # 50 Hz
print "%d us per period" % pulseLength
pulseLength /= 4096.0 # 12 bits of resolution
print "%d us per bit" % pulseLength
pulse *= 1000.0
pulse /= (pulseLength*1.0)
# pwmV=int(pluse)
print "pluse: %f " % (pulse)
pwm.setPWM(channel, 0, int(pulse))
# Angle to PWM
def write(servonum,x):
y=x/90.0+0.5
y=max(y,0.5)
y=min(y,2.5)
setServoPulse(servonum,y)
pwm.setPWMFreq(50) # Set frequency to 60 Hz
write(0, 90)
write(14, 120)

四、Stacking cascade

When you feel that the motor interface is not enough, you can use multiple driver boards in cascade. We support stacking up to 4 drives.

Cascading multiple drives requires a unique I2C address for each drive, the default address is 0x60. The address range is from 0x60 to 0x63, a total of 4 addresses.

There are A0~A4 on the driver board in total

Board 0: Address = 0x60 Offset = binary 0000 (All are empty by default)
Board 1: Address = 0x61 Offset = binary 0001 (Only solder A0)
Board 2: Address = 0x62 Offset = binary 0010 (Solder only A1)
Board 3: Address = 0x63 Offset = binary 0011 (Solder A0 & A1 at the same time)

StackingTest.py is a code example of cascading overlay, the key part is creating 2 MotorHAT objects:

# bottom hat is default address 0x60
bottomhat = Adafruit_MotorHAT(addr=0x60)
# top hat has A0 jumper closed, so its address 0x61
tophat = Adafruit_MotorHAT(addr=0x61)

五、Schematic