Daily Archives: August 6, 2018

Voice HAT for Raspberry Pi for Controlling a Motor

If you were one of the unlucky ones to miss out on the issue 57 of the MagPi, then the only option is to buy the Voice HAT from the AIY projects. The issue 57 had offered a free AIY projects Voice Kit, which Google had developed to make a Voice Assistant, and you could control a speaker with the voice HAT that attached on top of a Raspberry Pi Zero (RBPiZ).

Other tutorials in the MagPi show how to connect the Voice HAT hardware to simple circuits.  So far, the tutorials have dealt with LED lights and servomotors, but this project is somewhat more complex—using the Voice HAT to control a DC motor. Therefore, you will need a DC motor, four AA size batteries, breadboard, and jumper wires.

Usually, the RBPiZ draws its power from the power supply on the Voice HAT board. For this project, this connection has to be broken, else the motor may draw too much power from the RBPiZ and short it. On the Voice HAT board, locate the external power jumper marked JP1, and use a sharp knife to cut the track. If you later wish the power to be shared again between the board and the RBPiZ, re-solder the cut joint.

Power off the RBPiZ and the Voice HAT, and connect the positive terminal of the DC motor to Driver 0, middle pin, which is marked with a “+” symbol. Same way, the negative terminal of the DC motor connects to the “–“ pin of the Driver 0. As this pin connects to the GPIO4 pin, it allows the motor to be turned on and off.

The four AA battery pack connects to the +V and GND pins on the Voice HAT. This ensures the motor is supplied adequate power from the battery pack and the Voice HAT and does not crash the RBPiZ when it draws power. Now turn on the power to the Voice HAT, and then turn on the battery pack.

At this point, you are ready to turn on power to the RBPiZ. Boot into the AIY Projects software and enter the code from motor.py for testing the circuit. The control to the motor comes from the PWMOutputDevice from GPIO Zero, and this allows managing the speed of the motor.

The motor is controlled via a Pulse Width Modulation (PWM) method. The RBPiZ controls the power to the motor by controlling the on and off periods. If the on period is more than the off period, the motor receives more power and therefore, rotates faster.

To manage the speed of the motor, you control the variables .on() and .off() in the software.  Alternately, you may set the value of the instance variable to a value between 0.0 and 1.0 for controlling the speed. Here, 0.0 means the motor is a dead stop, while 1.0 sets the motor to a maximum speed. The motor.py uses both techniques and you can also use pwm.pulse() for pulsing the motor on or off. To integrate this with the Voice Assistant, enter the code from add_to_action.py to the relevant sections. You can now control the motor using voice commands.