Book Image

Getting Started with Raspberry Pi Zero

By : Richard Grimmett
Book Image

Getting Started with Raspberry Pi Zero

By: Richard Grimmett

Overview of this book

Raspberry Pi Zero is half the size of Raspberry Pi A, only with twice the utility. At just three centimeters wide, it packs in every utility required for full-fledged computing tasks. This practical tutorial will help you quickly get up and running with Raspberry Pi Zero to control hardware and software and write simple programs and games. You will learn to build creative programs and exciting games with little or no programming experience. We cover all the features of Raspberry Pi Zero as you discover how to configure software and hardware, and control external devices. You will find out how to navigate your way in Raspbian, write simple Python scripts, and create simple DIY programs.
Table of Contents (16 chapters)
Getting Started with Raspberry Pi Zero
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

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 that you just added:

  • io.setup(in2_pin1, io.OUT): This sets GPIO 27 to an output.

  • p1 = io.PWM(in1_pin1, 50): Instead of just on or off settings, this PWM setting allows the programmer to set the relative width of the pulse. This initializes this functionality...