Skip to Content
Raspberry Pi Cookbook, 3rd Edition
book

Raspberry Pi Cookbook, 3rd Edition

by Simon Monk
October 2019
Beginner to intermediate
608 pages
11h 41m
English
O'Reilly Media, Inc.
Content preview from Raspberry Pi Cookbook, 3rd Edition

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:

Figure 10-1 shows how you can wire this LED using a solderless breadboard and male-to-female jumper leads.

F0901
Figure 10-1. Connecting an LED to a Raspberry Pi

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. ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Raspberry Pi Cookbook, 4th Edition

Raspberry Pi Cookbook, 4th Edition

Simon Monk
Arduino Cookbook, 3rd Edition

Arduino Cookbook, 3rd Edition

Michael Margolis, Brian Jepson, Nicholas Robert Weldin

Publisher Resources

ISBN: 9781492043218Errata PageSupplemental Content