Datasheet

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' 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(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)
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
© Adafruit Industries
https://learn.adafruit.com/adafruit-dc-and-stepper-motor-hat-for-
raspberry-pi
Page 24 of 38