Chapter 10. Controlling Hardware
10.0 Introduction
In this chapter, you come to grips with the control of electronics through the Raspberry Pi’s general-purpose input/output (GPIO) connector.
Most of the recipes require the use of a solderless breadboard and male-to-female and male-to-male jumper wires (see Recipe 9.8). To maintain compatibility with older 26-pin Raspberry Pi models, all the breadboard examples here use only the top 26 pins common to both GPIO layouts (see Recipe 9.1).
10.1 Connecting an LED
Problem
You want to know how to connect an LED to the Raspberry Pi.
Solution
Connect an LED to one of the GPIO pins using a 470Ω or 1kΩ series resistor to limit the current. To make this recipe, you will need the following:
-
Breadboard and jumper wires (see “Prototyping Equipment and Kits”)
-
470Ω resistor (see “Resistors and Capacitors”)
-
LED (see “Opto-Electronics”)
Figure 10-1 shows how you can wire this LED using a solderless breadboard and male-to-female jumper leads.
Having connected the LED, we need to be able to turn it on and off using commands from Python.
Start a Python console from the Terminal and enter these commands:
$ sudo python3 >>> from gpiozero import LED >>> led = LED(18) >>> led.on() >>> led.off() >>>
This will turn your LED on after the led.on()
command, and off again after the led.off()
command. ...
Get Raspberry Pi Cookbook, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.