My last project had me digging through old robotics parts to find an H-Bridge Motor Controller. I found one that had several TTL chips on the circuit as well and it reminded me of a trick some of you may find useful.
First, lets define pulse width modulation (PWM.) It refers to the duty cycle (how long the line is high vs how long it is low) of a pulse stream.
What use is this for controlling motors? If you have ever tried controlling a motor by simply turning it on and off, you quickly realize how little control you have. Your robot goes from stopped to zooming at full speed with nothing in between. If, however, you use PWM to power your motor, then it will be on at full power very briefly and then off, repeatedly. With PWM you can provide much finer motor control and have your robot move at whatever speed you like. The higher the duty cycle of the pulse stream, the more power your motor will output.
There are several ways to produce PWM signals from the Raspberry Pi. The simplest is to turn a GPIO pin on and off in a loop with delays controlling the pulse width. This is very CPU intensive and not always consistent. You can use libraries or device drivers to produce PWM from the GPIO that will be consistent and will use virtually no CPU. Or, you can use external hardware to produce PWM.
Here is how I have done it in the past. (I haven’t built a robot using a Pi yet, but did it with simpler microcontrollers years ago.) Use six GPIO pins:
For more detail: Pulse Width Motor Control