Adafruit DC and Stepper Motor HAT for Raspberry Pi Created by lady ada Last updated on 2016-10-05 05:14:49 PM UTC
Guide Contents Guide Contents Overview Assembly Solder on Headers and Terminal Block And Solder! 10 Powering Motors Voltage requirements: Current requirements: Power it up Installing Software Enable I2C Downloading the Code from Github Using DC Motors Connecting DC Motors DC motor control walkthru Creating the DC motor object Setting DC Motor Speed Setting DC Motor Direction Using Stepper Motors Connecting Stepper Motors Stepper motor control walkthru Creating the Stepper motor object Stepping step() - bl
Schematic Fabrication Print © Adafruit Industries https://learn.adafruit.
Overview Let your robotic dreams come true with the new DC+Stepper Motor HAT from Adafruit. This Raspberry Pi add-on is perfect for any motion project as it can drive up to 4 DC or 2 Stepper motors with full PWM speed control. Raspberry Pi and motors are not included © Adafruit Industries https://learn.adafruit.
Since the Raspberry Pi does not have a lot of PWM pins, we use afully-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 GPIO 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.
Motors are controlled TB6612 MOSFET driver: with 1.2A per channel current capability (20ms long bursts of 3A peak), a big improvement over L293D drivers and there are built-in flyback diodes as well. © Adafruit Industries https://learn.adafruit.
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 here at Adafruit so all you have to do is solder on the included 2x20 plain header and the terminal blocks. Lets check out these specs again: 4 H-Bridges: TB6612 chipset provides 1.2A per bridge (3A brief peak) with thermal shutdown protection, internal kickback protection diodes. Can run motors on 4.5VDC to 13.5VDC.
Install the easy-to-use Python library, check out the examples and you're ready to go! Comes with an assembled & tested HAT, terminal blocks, and 2x20 plain header. Some soldering is required to assemble the headers on. Stacking header not included, but we sell them in the shop so if you want to stack HATs, please pick one up at the same time. (http://adafru.it/2223) Raspberry Pi and motors are not included but we have lots of motors in the shop and all our DC motors, and stepper motors work great.
Assembly Solder on Headers and Terminal Block Before we can motorin' there's a little soldering to be done. This step will attach the 2x20 socket header so that we can plug this HAT into a Raspberry Pi, and the terminal blocks so you can attach external power and motors. Start by plugging the 2x20 header into a Raspberry Pi, this will keep the header stable while you solder.
And Solder! Heat up your iron and solder in one header connection on the right. Once it is soldered, put down the solder and reheat the solder point with your iron while straightening the HAT so it isn't leaning down (For tips on soldering, be sure to check out our Guide to Excellent Soldering (http://adafru.it/aTk)). Solder one point on the opposite side of the connector © Adafruit Industries https://learn.adafruit.
Solder each of the connections for the top row © Adafruit Industries https://learn.adafruit.
Flip the board around and solder all the connections for the other half of the 2x20 header © Adafruit Industries https://learn.adafruit.
Check over your work so far, make sure each solder point is shiny, and isn't bridged or dull or cracked Now grab the 3.5mm-spaced terminal blocks. These will let you quickly connect up your motor and power supply using only a screw driver You will have 3 x 2-pin terminal blocks and 2 x 3-pin terminal blocks Slide each of the 3-pin terminal blocks into a 2-pin to create two 5-pin blocks © Adafruit Industries https://learn.adafruit.
Slide the terminal blocks along the edge of the HAT, so that the 'mouth' of each block is facing out. You can use scotch or other plain tape to keep the terminal blocks flat against the PCB while you solder © Adafruit Industries https://learn.adafruit.
Flip over the board and solder in all of the terminal block pins © Adafruit Industries https://learn.adafruit.
Check over your work so far, make sure each solder point is shiny, and isn't bridged or dull or cracked You're done! You can now move onto the software side © Adafruit Industries https://learn.adafruit.
Powering Motors Motors need a lot of energy, especially cheap motors since they're less efficient. 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.
If you don't have to take your project on the go, a9V 1A (http://adafru.it/dO6), 12V 1A (http://adafru.it/eny), or 12V 5A w (http://adafru.it/enz)ill 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. © Adafruit Industries https://learn.adafruit.
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.
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! (http://adafru.
That's it! Now you can get started with testing. Run cd examples 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 © Adafruit Industries https://learn.adafruit.
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 highquality 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.
DC motor control walkthru Here's a walkthru of the code which shows you everything the MotorHAT library can do and how to do it. Start with importing at least these libraries: #!/usr/bin/python from Adafruit_MotorHAT import Adafruit_MotorHAT, Adafruit_DCMotor import time import atexit 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.
mh = Adafruit_MotorHAT(addr=0x60) 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' codewhen using DC motors, it will do its best to shut down all the motors.
following: 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. 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.
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 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.
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 M ( 2 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 Adafruit-Motor-HAT-Python/examples folder and run sudo python StepperTest.py to watch your stepper motor spin back and forth.
mh.getMotor(4).run(Adafruit_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.
"StepperMotor" by Wapcaplet; Teravolt. (http://adafru.it/enC) There are four essential types of steps you can use with your Motor HAT. All four kinds will work with any unipolar or bipolar stepper motor 1. Single Steps - this is the simplest type of stepping, and uses the least power. It uses a single coil to 'hold' the motor in place, as seen in the animated GIF above 2. Double Steps - this is also fairly simple, except instead of a single coil, it has two coils on at once.
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.
#print("Steppin!") stepper.step(numsteps, direction, style) #print("Done") We have some commented-out print statements in case you want to do some debugging. Then, whenever you want to make the first stepper move, you can call: st1 = threading.Thread(target=stepper_worker, args=(myStepper1, numsteps, direction, stepping_style)) st1.start() Which will spin up a background thread to move Stepper1 and will return immediately. You can then do the same with the second stepper: st2 = threading.
Stacking HATs One of the cool things about this HAT design is that it is possible to stack them. Every HAT you stack can control another 2 steppers or 4 DC motors (or a mix of the two) You can stack up to 32 HAT for a total of 64 steppers or 128 DC motors! Most people will probably just stack two or maybe three but hey, you never know.
The only thing to watch for when stacking HATs is every HAT must have a unique I2C address. The default address is 0x60. You can adjust the address of the motor HATs to range from 0x60 to 0x80 for a total of 32 unique addresses. Addressing the HATs Each board in the stack must be assigned a unique address. This is done with the address jumpers on the left side of the board. The I2C base address for each board is 0x60.
Board 0: Address = 0x60 Offset = binary 0000 (no jumpers required) Board 1: Address = 0x61 Offset = binary 0001 (bridge A0) Board 2: Address = 0x62 Offset = binary 0010 (bridge A1, the one above A0) Board 3: Address = 0x63 Offset = binary 0011 (bridge A0 & A1, two bottom jumpers) Board 4: Address = 0x64 Offset = binary 0100 (bridge A2, middle jumper) etc. Note that address 0x70 is the "all call" address for the controller chip on the HAT.
# 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) then use other of those for getting motors. We recommend using threading for control of the steppers to make them turn together © Adafruit Industries https://learn.adafruit.
Downloads Motor ideas and tutorials Wikipedia has tons of information (http://adafru.it/aOF) on steppers Jones on stepper motor types (http://adafru.it/aOH) Jason on reverse engineering the stepper wire pinouts (http://adafru.it/aOI) Files PCA9685 PWM driver (http://adafru.it/emJ) TB6612 Motor driver (http://adafru.it/emK) PCB files on GitHub (http://adafru.it/rEN) Fritzing object in the Adafruit Fritzing Library (http://adafru.it/aP3) Schematic Click to embiggen © Adafruit Industries https://learn.
Fabrication Print This is a 'HAT mechanical standard' compatible! © Adafruit Industries https://learn.adafruit.
© Adafruit Industries Last Updated: 2016-10-05 05:14:48 PM UTC Page 38 of 38