make_sensors.pdf

(401622 KB) Pobierz
Make:
Sensors
Projects and Experiments to
Measure the World with
Arduino and Raspberry Pi
Tero Karvinen, Kimmo Karvinen
& Ville Valtokari
Electronics/Microcontrollers
Make:
Sensors
Sensors allow you to interact with the physical world in a way most of us never
could before—measure something, interpret the result, and take action based on
its value. Combined with inexpensive little computers like Arduino and Raspberry
Pi, you can make the physical world programmable. With this book, you’ll make
gadgets that can sense it all—from various gases to infrared light to acceleration.
Recommended for makers who are looking for powerful Arduino or Raspberry Pi
applications,
Make: Sensors
takes you from idea to reality quickly. You’ll create
polished projects using RGB LEDs, e-paper displays, and servo motors—and
finish them off with enclosures.
Learn about touch, light, temperature, humidity, gas, and magnetic sensors as
well as accelerometers and gyroscopes. Each chapter offers a new mini-project
and an environmental experiment that demonstrates how to combine different
technologies for a unique result.
In Make: Sensors, you’ll learn to build these projects:
»
»
»
»
»
»
Personal Breathalyzer with Arduino and an alcohol sensor
Email Smoke Alarm powered by Raspberry Pi
Haunted Ringing Bell with a hidden servo motor
Pong video game with Python and pyGame
Color-changing Chameleon Dome with an RGB LED and color sensor
Graphical sound visualizer that analyzes and display ambient sound
Make: Sensors teaches you to experience and understand your world more deeply by
enabling you to track, record, and interact with it in a fresh way.
More than 50 sensor-based experiments and projects
US $34.99 CAN $36.99
ISBN: 978-1-449-36810-4
Make: Sensors
Tero Karvinen, Kimmo Karvinen, and
Ville Valtokari
Make: Sensors
by Tero Karvinen, Kimmo Karvinen, and Ville Valtokari
Copyright © 2014 Tero Karvinen, Kimmo Karvinen, and Ville Valtokari. All rights reserved.
Printed in Canada.
Published by Maker Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
Maker Media books may be purchased for educational, business, or sales promotional use. Online editions
are also available for most titles (http:/
/oreilly.com/safari).
For more information, contact O’Reilly Media’s
corporate/institutional sales department: 800-998-9938 or
corporate@oreilly.com.
Editors:
Brian Jepson and Emma Dvorak
Production Editor:
Kristen Brown
Technical Editor:
Jason Tavares
Proofreader:
Julie Van Keuren
Indexer:
Judith McConville
Cover Designers:
Juliann Brown and Brian Jepson
May 2014:
2014-05-05:
2015-01-23:
2017-07-07:
First Edition
First release
Second release
Third release
Interior Designer:
Nellie McKesson
Illustrators:
Tero Karvinen, Kimmo Karvinen, and
Ville Valtokari
Photographer:
Kimmo Karvinen
Cover Photo:
Kimmo Karvinen
Revision History for the First Edition:
See
http:/
/oreilly.com/catalog/errata.csp?isbn=9781449368104
for release details.
Make:, Maker Shed, and Maker Faire are registered trademarks of Maker Media, Inc. The Maker Media logo
is a trademark of Maker Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book, and Maker Media, Inc. was aware of a trademark
claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and authors assume
no responsibility for errors or omissions, or for damages resulting from the use of the information contained
herein.
ISBN: 978-1-449-36810-4
[TI]
Table of Contents
Preface
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi
1.
Raspberry Pi
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Raspberry Pi from Zero to First Boot
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Extract NOOBS*.zip
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Connect Cables
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Boot and Install Raspbian
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Troubleshooting Your Raspberry Pi Installation
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Feeling at Home in Linux
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Command-Line Interface is Everywhere, Forever
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Looking Around
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Text Files for Configuration
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
sudo Make Me a Sandwich
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Connecting Electronics to Raspberry Pi Pins
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Hello GPIO, Blink an LED
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Building the Circuit
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Two Numbering Systems: Purpose and Location
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Controlling GPIO Pins from the CLI
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Writing to Files Without an Editor
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Light Up the LED
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Troubleshooting
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
GPIO Without Root
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Troubleshooting GPIO
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
GPIO in Python
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Hello Python
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
What’s Next?
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
iii
Zgłoś jeśli naruszono regulamin