The dependencies for the motor driver libraries may be installed from the command-line terminal of your Raspberry Pi Zero as follows (you may skip this step if you installed these tools while working on Chapter 4, Communication Interfaces):
sudo apt-get update
sudo apt-get install python3-dev python3-smbus
The next step is cloning the motor driver library:
git clone https://github.com/sai-y/Adafruit-Motor-HAT-Python-
Library.git
The library can be installed as follows:
cd Adafruit-Motor-HAT-Python-Library
sudo python3 setup.py install
Now that the libraries are installed, let's write a program that rotates the motors continuously:
- As always, the first step is importing the MotorHAT module:
from Adafruit_MotorHAT import Adafruit_MotorHAT,
Adafruit_DCMotor
- The next step is to create an instance of the MotorHAT class and establish the interface with the motor driver (as discussed in the previous section, the motor driver's 7-bit address is 0x60).
- The motors of the robot are connected to channels 1 and 2. Hence, we need to initialize two instances of the Adafruit_DCMotor class that represent the left and right motors of the robot:
left_motor = motor_driver.getMotor(1)
right_motor = motor_driver.getMotor(2)
- The next step is setting the motor speed and motor direction. The motor speed can be set using an integer between 0 and 255 (which corresponds to 0% and 100% of the motor's rated rpm). Let's set the motor speed at 100%:
left_motor.setSpeed(255)
right_motor.setSpeed(255)
- Let's rotate the motors in the forward direction:
left_motor.run(Adafruit_MotorHAT.FORWARD)
right_motor.run(Adafruit_MotorHAT.FORWARD)
- Let's rotate both the motors in the forward direction for 5 seconds and then reduce the speed:
left_motor.setSpeed(200)
right_motor.setSpeed(200)
- Now, let's rotate the motors in the reverse direction:
left_motor.run(Adafruit_MotorHAT.BACKWARD)
right_motor.run(Adafruit_MotorHAT.BACKWARD)
- Let's turn off the motors once we are done rotating the motors in the reverse direction for 5 seconds:
left_motor.run(Adafruit_MotorHAT.RELEASE)
right_motor.run(Adafruit_MotorHAT.RELEASE)
Putting it altogether:
from Adafruit_MotorHAT import Adafruit_MotorHAT, Adafruit_DCMotor
from time import sleep
if __name__ == "__main__":
motor_driver = Adafruit_MotorHAT(addr=0x60)
left_motor = motor_driver.getMotor(1)
right_motor = motor_driver.getMotor(2)
left_motor.setSpeed(255)
right_motor.setSpeed(255)
left_motor.run(Adafruit_MotorHAT.FORWARD)
right_motor.run(Adafruit_MotorHAT.FORWARD)
sleep(5)
left_motor.setSpeed(200)
right_motor.setSpeed(200)
left_motor.run(Adafruit_MotorHAT.BACKWARD)
right_motor.run(Adafruit_MotorHAT.BACKWARD)
sleep(5)
left_motor.run(Adafruit_MotorHAT.RELEASE)
right_motor.run(Adafruit_MotorHAT.RELEASE)
The preceding code sample is available for download along with this chapter as motor_test.py. Recharge the USB battery pack before you test the motors. We picked the test duration long enough to verify the motor direction, performance, and so on.
Now that the motors are tested, let us set up a camera for the robot.