O'Reilly logo

Getting Started with Raspberry Pi Zero by Richard Grimmett

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Controlling the speed of your motors with PWM

The previous example either turned the motors on to full speed or turned them off. You may want to configure your motors to run at different speeds. This can be done by using Pulse Width Modulation (PWM) to adjust the speed. PWM simply defines a way of changing the voltage of the signal by sending a series of pulses of equal value and changing the width of each pulse. The wider the pulse, the higher the average voltage delivered to the receiver. The DC motors that you are using respond to this higher average voltage by spinning faster.

The Raspberry Pi Zero GPIO can create PWM signals. The code snippet to do this is shown in the following screenshot:

The following is an explanation of the lines of code ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required