FAQ of Raspberry Pi Full Function Motor HAT Robot Expansion Board

by Cindy Ding on January 03, 2019

This Raspberry Pi Motor HAT Full function Robot Expansion Board has been update to the latest V0.2 version on October 31st 2018. We accept the customer advice and make some adjust of this motor hat board. It is more convenient to use now.

What can you do with this motor HAT robot expansion board?

  • Let your robotic dreams come true with the new DC+Stepper Motor HAT. This Raspberry Pi add-on board is perfect for any motion project as it can drive up to 4 DC or 2 Stepper motors with full PWM speed control, It also adds the capability to control 4 Servos with perfect timing. 
  • Since the Raspberry Pi does not have a lot of PWM pins, we use a fully-dedicated PWM driver chip onboard to both control motor direction and speed. This chip handles all the motor and speed controls over I2C. Only two pins (SDA & SCL) are required to drive the multiple motors, and since it's I2C you can also connect any other I2C devices or HATs to the same pins.
  • In fact, you can even stack multiple Motor HATS, up to 32 of them, for controlling up to 64 stepper motors or 128 DC motors (or a mix of the two) - just remember to purchase and solder in a stacking header instead of the one we include.
  • Motors are controlled by TB6612 MOSFET drivers with 1.2A per channel and 3A peak current capability, a big improvement over L293D drivers and there are built-in flyback diodes as well.We even had a little space so we added a polarity protection FET on the power pins and a bit of prototyping area. And the HAT is assembled and tested so all you have to do is solder on the included 2x20 plain header and the terminal blocks.
  • Stepper motors are great for (semi-)precise control, perfect for many robot and CNC projects. This HAT supports up to 2 stepper motors. The python library works identically for bi-polar and uni-polar motors.
  • Running a stepper is a little more intricate than running a DC motor but its still very easy.

Key Features:

  • 4 H-Bridges: TB6612 chipset provides 1.2A per bridge (3A peak) with thermal shutdown protection, internal kickback protection diodes. Can run motors on 5VDC to 12VDC.
  • Up to 4 bi-directional DC motors with individual 8-bit speed selection (so, about 0.5% resolution)
  • Up to 2 stepper motors (unipolar or bipolar) with single coil, double coil, interleaved or micro-stepping.
  • Up to 4 Channel 12-bit PWM/Servo(5V)
  • One Infrared Receiver(GPIO26)
  • Big terminal block connectors to easily hook up wires (18-26AWG) and power
  • Polarity protected 2-pin terminal block and jumper to connect external 5-12VDC power
  • Works best with Raspberry Pi model A+, B+, Pi 2, or Pi 3;
  • Install the easy-to-use Python library, check out the examples and you're ready to go!
  • Pi HAT 2x20P Conector with Extra Long pin
  • You can get the download information and document here.

User Manual of Stepper Motor HAT for Raspberry Pi

Powering Motors

Function:
• 4 H-Bridges: provides 1.2A per bridge (3A peak) with thermal shutdown protection, internal kickback protection diodes. Can run motors on 5VDC to 12VDC.
• Up to 4 bi-directional DC motors with individual 8-bit speed selection (so, about 0.5% resolution)
• Up to 2 stepper motors (unipolar or bipolar) with single coil, double coil, interleaved or micro-stepping.
• Up to 4 Channel 12-bit PWM/Servo(5V)
• One Infrared Receiver(GPIO26)
• Big terminal block connectors to easily hook up wires (18-26AWG) and power
• Polarity protected 2-pin terminal block and jumper to connect external 5-12VDC power
• Works best with Raspberry Pi model A+, B+, or Pi 2,Pi 3;

Motors need a lot of energy, especially cheap motors since they're less efficient.

Note: the motor voltage is 5V, the current should be around 200mA. It is suggest to use the 996, 995 series motor.

Voltage requirements:
The first important thing to figure out what voltage the motor is going to use. If you're lucky your motor came with some sort of specifications. Some small hobby motors are only intended to run at 1.5V, but its just as common to have 6-12V motors. The motor controllers on this HAT are designed to run from 5V to 12V.

MOST 1.5-3V MOTORS WILL NOT WORK or will be damaged by 5V power.


Current requirements:
The second thing to figure out is how much current your motor will need. The motor driver chips that come with the kit are designed to provide up to 1.2 A per motor, with 3A peak current. Note that once you head towards 2A you'll probably want to put a heat-sink on the motor driver, otherwise you will get thermal failure, possibly burning out the chip.

If you don't have to take your project on the go, a 9V 1A, 12V 1A, or 12V 5A will work nicely.

99% of 'weird motor problems' are due to having a voltage mismatch (too low a voltage, too high a voltage) or not having a powerful enough supply! Even small DC motors can draw up to 3 Amps when they stall.

Power it up
Wire up your battery pack to the Power terminal block on the right side of the HAT. It is polarity protected but still its a good idea to check your wire polarity. Once the HAT has the correct polarity, you'll see the LED light up.

Please note the HAT does not power the Raspberry Pi, and we strongly recommend having two seperate power supplies - one for the Pi and one for the motors, as motors can put a lot of noise onto a power supply and it could cause stability problems!

Installing Software
We have a Python library you can use to control DC and stepper motors, its probably the easiest way to get started, and python has support for multithreading which can be really handy when running multiple stepper motors at onces!

Enable I2C
You will have to make I2C support work on your Pi before you begin, visit our tutorial to enable I2C in the kernel!
Before you start, you'll need to have the python smbus library installed as well as 'git', run apt-get install python-smbus i2c-tools

Downloading the Code
The easiest way to get the code onto your Pi is to hook up an Ethernet cable or with a WiFi setup, and clone it directly using 'git', which is installed by default on most distros.
Simply run the following commands from an appropriate location (ex. "/home/pi"):

wget tar xvzf Raspi_MotorHAT.tar

cd Raspi_MotorHAT

Install python-dev if you haven't already:

sudo apt-get install python-dev

From within the Motor HAT library folder, we have a couple examples to demonstrate the different types of motors and configurations. The next few pages will explain them.

Using DC Motors
DC motors are used for all sort of robotic projects.

The Motor HAT can drive up to 4 DC motors bi-directionally. That means they can be driven forwards and backwards. The speed can also be varied at 0.5% increments using the high-quality built in PWM. This means the speed is very smooth and won't vary!

Note that the H-bridge chip is not meant for driving continuous loads over 1.2A or motors that peak over 3A, so this is for small motors. Check the datasheet for information about the motor to verify its OK!

Connecting DC Motors
To connect a motor, simply solder two wires to the terminals and then connect them to either theM1, M2, M3, or M4. If your motor is running 'backwards' from the way you like, just swap the wires in the terminal block
For this demo, please connect it to M3
Now go into the Raspi_MotorHAT / folder and run sudo python DCTest.py to watch your motor spin back and forth.
raspberry pi motor hat
Note: need to external power supply to the motor power supply


DC motor control
Here's the code which shows you everything the MotorHAT library can do and how to do it.
The Motor HAT library contains a few different classes, one is the MotorHAT class itself which is the main PWM controller. You'll always need to create an object, and set the address. By default the address is 0x6F(see the stacking HAT page on why you may want to change the address)

# create a default object, no changes to I2C address or frequency
mh =  Raspi_MotorHAT(addr=0x6F)


The PWM driver is 'free running' - that means that even if the python code or Pi linux kernel crashes, the PWM driver will still continue to work. This is good because it lets the Pi focus on linuxy things while the PWM driver does its PWMy things. But it means that the motors DO NOT STOP when the python code quits.
For that reason, we strongly recommend this 'at exit' code when using DC motors, it will do its best to shut down all the motors.

# recommended for auto-disabling motors on shutdown!
def turnOffMotors():
    mh.getMotor(1).run(Raspi_MotorHAT.RELEASE)
    mh.getMotor(2).run(Raspi_MotorHAT.RELEASE)
    mh.getMotor(3).run(Raspi_MotorHAT.RELEASE)
    mh.getMotor(4).run(Raspi_MotorHAT.RELEASE)

 atexit.register(turnOffMotors)


Creating the DC motor object
OK now that you have the motor HAT object, note that each HAT can control up to 4 motors. And you can have multiple HATs!
To create the actual DC motor object, you can request it from the MotorHAT object you created above with getMotor(num) with a value between 1 and 4, for the terminal number that the motor is attached to

myMotor = mh.getMotor(3)

DC motors are simple beasts, you can basically only set the speed and direction.

Setting DC Motor Speed
To set the speed, call setSpeed(speed) where speed varies from 0 (off) to 255 (maximum!). This is the PWM duty cycle of the motor

# set the speed to start, from 0 (off) to 255 (max speed)
myMotor.setSpeed(150)


Setting DC Motor Direction
To set the direction, call run(direction) where direction is a constant from one of the following:
• Raspi_MotorHAT.FORWARD - DC motor spins forward
• Raspi _MotorHAT.BACKWARD - DC motor spins forward
• Raspi _MotorHAT.RELEASE - DC motor is 'off', not spinning but will also not hold its place.

Using Stepper Motors

Stepper motors are great for (semi-)precise control, perfect for many robot and CNC projects. This HAT supports up to 2 stepper motors. The python library works identically for bi-polar and uni-polar motors.
Running a stepper is a little more intricate than running a DC motor but its still very easy.
Raspberry Pi motor HAT robot expansion board
Note: need to external power supply to the motor power supply


Connecting Stepper Motors
For unipolar motors: to connect up the stepper, first figure out which pins connected to which coil, and which pins are the center taps. If its a 5-wire motor then there will be 1 that is the center tap for both coils. There's plenty of tutorials online on how to reverse engineer the coils pinout. The center taps should both be connected together to the GND terminal on the Motor HAT output block. then coil 1 should connect to one motor port (say M1 or M3) and coil 2 should connect to the other motor port (M2 or M4).


For bipolar motors: its just like unipolar motors except theres no 5th wire to connect to ground. The code is exactly the same.
For this demo, please connect it to M1 and M2
Now go into the Raspi_MotorHAT / folder and run sudo python StepperTest.py to watch your stepper motor spin back and forth.

Stepper motor control
Here's the code which shows you everything the MotorHAT library can do and how to do it.
The MotorHAT library contains a few different classes, one is the MotorHAT class itself which is the main PWM controller. You'll always need to create an object, and set the address. By default the address is 0x6F (see the stacking HAT page on why you may want to change the address)

 # create a default object, no changes to I2C address or frequency
 mh = Raspi_MotorHAT(addr = 0x6F)


Even though this example code does not use DC motors, it's still important to note that the PWM driver is 'free running' - that means that even if the python code or Pi linux kernel crashes, the PWM driver will still continue to work. This is good because it lets the Pi focus on linuxy things while the PWM driver does its PWMy things.

Stepper motors will not continue to move when the Python script quits, but it's still strongly recommend that you keep this 'at exit' code, it will do its best to shut down all the motors:

# recommended for auto-disabling motors on shutdown!
def turnOffMotors():
    mh.getMotor(1).run(Raspi_MotorHAT.RELEASE)
    mh.getMotor(2).run(Raspi_MotorHAT.RELEASE)
    mh.getMotor(3).run(Raspi_MotorHAT.RELEASE)
    mh.getMotor(4).run(Raspi_MotorHAT.RELEASE)

 atexit.register(turnOffMotors)


Creating the Stepper motor object
OK now that you have the motor HAT object, note that each HAT can control up to 2 steppers. And you can have multiple HATs!
To create the actual Stepper motor object, you can request it from the MotorHAT object you created above with getStepper(steps, portnum) where steps is how many steps per rotation for the stepper motor (usually some number between 35 - 200) ith a value between 1 and 2. Port #1 isM1 and M2, port #2 is M3 and M4

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


Next, if you are planning to use the 'blocking' step() function to take multiple steps at once you can set the speed in RPM. If you end up using oneStep() then this step isn't necessary. Also, the speed is approximate as the Raspberry Pi can't do precision delays the way an Arduino would. Anyways, we wanted to keep the Arduino and Pi versions of this library similar so we keptsetSpeed() in:

 myStepper.setSpeed(30)           # 30 RPM


Using "Non-blocking" oneStep()
OK lets say you want a lot of control over your steppers, you can use the one oneStep(direction, stepstyle) which will make a single step in the style you request, with no delay. This will let you step exactly when you like, for the most control

Connecting Servos
raspberry pi motor hat expansion board
Note: need to external power supply to the motor power supply

Connecting a Servo
Most servos come with a standard 3-pin female connector that will plug directly into the headers on the Motor HAT headers. Be sure to align the plug with the ground wire (usually black or brown) with the bottom row and the signal wire (usually yellow or white) on the top.

Adding More Servos
Up to 4 servos can be attached to one board. If you need to control more than 4 servos, additional boards can be stacked as described on the next page.
Note:MotoHAT’s servo channel: 0,1,14,15

Stepper Servo control
For this demo, please connect it to Channel 0
Now go into the Raspi_MotorHAT / folder and run sudo python ServoTest.py to watch your servo spin back and forth.

Customer Feedback

Feeback 1: Great first board for experimenting with all kinds of motors - DC, stepper, and servo

Great little board, the only HAT I've found for Raspberry Pi that supports all three major kinds of motors - DC, stepper, and servo. Wasn't sure which kind I wanted for my project so this helped me narrow it down. Regarding stacking/addressing, it works like a charm (although the documentation is missing). There are four solder drops on the board labelled A0-A3 that control the address. The solder bridges two small contacts. They come all soldered so the address is 0x6f. Use an iron and a bit of solder wick to remove any of the drops and you can get any address between 0x60 and 0x6f. Use the command "sudo i2cdetect -y 1" (no quotes) to show you the current address. For some reason you will always see address 0x70 active with this HAT but ignore that one, the address from 0x60 to 0x6f is the one you want.

Few other points/warnings:
1) Always disconnect the power to the motor HAT when mucking with motor wiring, I burnt out one of the motor channels by briefly touching a wire to the HDMI connector casing.
2) The DC/stepper drivers can handle variable voltage but servos always use ONLY 5V. Use more and you'll get a little fireworks display on both the board and your servos.
3) The stepper driver is limited by the bandwidth of the I2C bus (it has to send commands for every micro-step) so I haven't been able to get them to move as fast as I know they are capable of. I was able to up the baud rate of the I2C bus somewhat but started getting missed steps. A more intelligent protocol could alleviate this but for an entry-level experimenters board I won't mark it down.

raspberry pi motor hat

Frequently Asked Questions

Question 1.Can you run a servo and a dc motor at the same time? or do the servo pwm commands change the signal on all outputs?
Answer: You can run a servo and a dc motor at the same time via python script.If you ignore the time that the python program runs in the while loop, then you can think of it as simultaneous.

pwm.setPWMFreq(60)                        # Set frequency to 60 Hz
while (True):
  pwm.setPWM(0, 0, servoMin)
  pwm.setPWM(0, 0, servoMax)

Question 2.Does the servo pwm commands change the signal on all outputs?
Answer: You can run a servo and a dc motor at the same time via python script.If you ignore the time that the python program runs in the while loop, then you can think of it as simultaneous.

pwm.setPWMFreq(60)                        # Set frequency to 60 Hz
while (True):
  pwm.setPWM(0, 0, servoMin)
  pwm.setPWM(0, 0, servoMax)

Question 3. Does the motot hat board power the attached Pi or will the Pi require a separate power supply?
Answer: The robot expansion board does not require additional power, and it is powered by the Raspberry Pi; However, external devices controlled by the expansion board, such as the steering gear, require additional power supply. please refer to the following pictures:


Question 4. What is the I2C address?
Answer: I2C address is 0x6F, and you can found these information from WIKI:
www.raspberrypiwiki.com/index.php/Robot_Expansion_Board

2 comments
by S A on March 12, 2022

How do I connect a relay to stepper motor hat to run a 5v – 12v buzzer?

by RTTST on March 12, 2022

What exactly is the purpose of “prescaleval” in
def setPWMFreq(self, freq)
of
Raspi_PWM_Servo_Driver.py

Perhaps you can point me to the documentation for that library?

Thanks

LEAVE A COMMENT

Please note, comments must be approved before they are published


BACK TO TOP