Dependencies

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
This library is a fork of the Adafruit Motor HAT library. We fixed some issues to make the library installation compatible with Python 3.x.

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:

  1. As always, the first step is importing the MotorHAT module:
       from Adafruit_MotorHAT import Adafruit_MotorHAT, 
Adafruit_DCMotor
  1. 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).
  2. 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)
  1. 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)
  1. Let's rotate the motors in the forward direction:
       left_motor.run(Adafruit_MotorHAT.FORWARD) 
right_motor.run(Adafruit_MotorHAT.FORWARD)
  1. 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)
  1. Now, let's rotate the motors in the reverse direction:
       left_motor.run(Adafruit_MotorHAT.BACKWARD) 
right_motor.run(Adafruit_MotorHAT.BACKWARD)
  1. 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.

If your Raspberry Pi Zero seems to be resetting itself while running or the motors aren't running at the rated speed, it is an indicator that the motors are not being driven with the sufficient current. Switch over to a power source that meets the requirement (this may involve switching from the GPIO's power supply to the battery pack or switching to a battery pack of higher capacity).

Now that the motors are tested, let us set up a camera for the robot.