Essentials_GPIOZero_v1.pdf

(6166 KB) Pobierz
ESSENTIALS
SIMPLE
GPIO
ELECTRONICS
Raspberry Pi
Phil King
ZERO
TAKE CONTROL OF THE REAL WORLD
WITH
YOUR
Written by
THE OFFICIAL
RASPBERRY PI
MAGAZINE
25
%
SAV
UP T E
O
2
FREE
PI ZERO!
Subscribe in print for six or 12 months to receive this stunning free gift
Subscribe today & receive:
A free Pi Zero v1.3 (the latest model)
A free Camera Module connector
A free USB & HDMI cable bundle
Delivered with your first issue!
Pricing
Get six issues:
£30
(UK)
£45
(EU)
$69
(USA)
£50
(Rest of World)
Subscribe for a year:
£55
(UK)
£80
(EU)
$129
(USA)
£90
(Rest of World)
Direct Debit: £12.99
(UK)
(quarterly)
Other benefits:
Save up to 25% on the price
Free delivery to your door
Exclusive Pi offers & discounts
Get every issue first (before stores)
How to subscribe:
magpi.cc/Subs1
(UK / ROW)
Call +44(0)1202 586848 (UK/ROW)
imsnews.com/magpi
(USA)
Call 800 428 3003 (USA)
Search ‘The MagPi’
on your app store:
3
WELCOME
TO
SIMPLE
ELECTRONICS
WITH GPIO ZERO
Pi’s continued popularity is its 40-
pin GPIO header, which enables users
to connect electronic components and control
them with a program. While other languages
may be used, the code for physical computing
projects is usually written in Python, something
that’s become a whole lot easier with the recent
introduction of the GPIO Zero library. Before its
arrival, connecting electronics required numerous
lines of code just to get everything set up. GPIO
Zero does all this boilerplate code for you, so you
can focus on controlling the physical devices. As
well as resulting in far fewer lines of code, it makes
it a lot easier for newcomers to understand.
In this book, we’ll help you start coding with
GPIO Zero, guiding you step by step through
all sorts of projects, from basic LED and button
circuits to using various sensors and building
robots. It’s time to dust off that breadboard!
Phil King
Contributing Editor, The MagPi magazine
O
ne of the main reasons for the Raspberry
FIND US ONLINE
raspberrypi.org/magpi
EDITORIAL
GET IN TOUCH
magpi@raspberrypi.org
DESIGN
Managing Editor:
Russell Barnes
russell@raspberrypi.org
Contributing Editor:
Phil King
Sub Editor:
Laura Clay
Contributors:
Mike Cook, Richard Hayler & family, Ben Nuttall
Critical Media:
criticalmedia.co.uk
Head of Design:
Dougal Matthews
Designers:
Lee Allen, Mike Kay
DISTRIBUTION
THE MAGPI SUBSCRIPTIONS
Select Publisher Services Ltd
PO Box 6337, Bournemouth
BH1 9EH |
+44 (0)1202 586 848
magpi.cc/Subs1
Seymour Distribution Ltd
2 East Poultry Ave, London
EC1A 9PT |
+44 (0)207 429 4000
In print, this product is made using paper
sourced from sustainable forests and
the printer operates an environmental
[
management system which has been
Chapter One
]
assessed as conforming to ISO 14001.
4
This book is published by Raspberry Pi (Trading) Ltd., 30 Station Road, Cambridge, CB1 2JH. The
publisher, editor and contributors accept no responsibility in respect of any omissions or errors
relating to goods, products or services referred to or advertised in this product. Except where
otherwise noted, content in this magazine is licensed under a Creative Commons Attribution-
NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0).
[ SIMPLE ELECTRONICS WITH GPIO ZERO ]
ESSENTIALS
CONTENTS
06
[
CHAPTER ONE
]
GET STARTED WITH
GPIO ZERO
Discover what it’s all about
[
PHIL KING
]
38
[
CHAPTER SIX
]
MOTION-SENSING ALARM
Use a PIR to create an intruder alert
13
[
CHAPTER TWO
]
CONTROL LEDS
Make them blink and more
42
[
CHAPTER SEVEN
]
BUILD A RANGEFINDER
Connect and read a distance sensor
18
[
CHAPTER THREE
]
ADD A PUSH BUTTON
Create a fun reaction game
47
[
CHAPTER EIGHT
]
MAKE A LASER TRIPWIRE
Use an LDR to detect light/dark
24
[
CHAPTER FOUR
]
MAKE A MUSIC BOX
Link buttons to sounds
53
[
CHAPTER NINE
]
BUILD AN INTERNET RADIO
Adjust the station with potentiometers
32
[
CHAPTER FIVE
]
LIGHT AN RGB LED
All the colours of the rainbow
61
[
CHAPTER TEN
]
CREATE AN LED
THERMOMETER
Read an analogue temperature sensor
68
[
CHAPTER ELEVEN
]
BUILD A ROBOT
Control DC motors with GPIO Zero
76
[
CHAPTER TWELVE
]
QUICK REFERENCE
A handy guide to GPIO Zero’s many
useful classes
Phil King is
a Raspberry
Pi enthusiast
and regular
contributor to
The
MagPi
magazine.
Growing up in the
‘golden era’ of
8-bit computers in
the 1980s, he leapt
at the chance to
write about them
in magazines such
as
CRASH
and
ZZAP!64.
When
consoles took over
the video games
world, he missed
the opportunity
to program… until
the Raspberry Pi
came along. Phil is
now an avid coder
and electronics
dabbler, who
loves to work on
projects with his
six-year-old son.
[
Don’t Panic
]
[
Contents
5
Zgłoś jeśli naruszono regulamin