Welcome to Make: Sensors. Soon you’ll be making gadgets that can sense it all—from dangerous gases to acceleration. In this book, you’ll use sensors to measure the physical world, represent the result as a numeric value, and take some action based on that value.
For example, a sensor could measure heat, pressure, light, or acceleration and report a value such as 22 C, 1015 millibars, light is detected, or 2.3 g acceleration (in the case of light, notice that we represented it as a Boolean or yes/no value rather than a numeric quantity; you’ll see examples of this later).
A microcontroller board is the brain of the robot, system, or gadget you’re building. You’ll write your own software to run on the microcontroller. In this book, you’ll work with two very popular boards: Arduino and Raspberry Pi. Either of these makes it easy to write software code to work with electronics.
If your interest in electronics started with a desire to quickly learn some basics and then design your own robots, gadgets, or projects, you’re in the right place. This book will show you how to go from idea to reality quickly.
Theory, skills, and basics are useful—as long as they serve your creativity. Feel free to experiment with your ideas, and have the courage to publish your results on the Web.
Each chapter presents a mini project to show how you can combine different technologies. For example, you’ll build a wooden box that you open with a fingerprint and a color-changing chameleon dome. These are fun projects, but also good starting points for things you invent later yourself.
The skills you learn with Arduino are easily applicable to real-life projects. For example, we used Arduino to build the sun sensor prototype for Finland’s first satellite (Figure P-1).
When you get an idea, you can quickly build your first prototype with the help of this book. Instead of spending hours with component data sheets, you can simply pick a sensor and use ready-made breadboard diagrams and code. You can use sensors as building blocks for your project, but unlike construction kits such as Meccano or Lego, the possibilities with Arduino and Raspberry Pi are nearly endless.
If you know what you want to measure, you can easily find a sensor for it. The book is arranged by the real-life phenomena you can measure:
You can also use Make: Sensors as a maker’s coffee-table book: browse it to get ideas of what’s available, and look for inspiration for new projects.
If you want to understand how sensors are connected to Arduino and Raspberry Pi, you’ll enjoy the in-depth explanations. All the sensor code examples are fully self-contained, completely showing the interaction with the sensor. Understanding the sensors in the book helps you apply your skills to new sensors, even ones that aren’t on the market yet.
When we chose the sensors for you, we picked a variety of useful and interesting sensors. We didn’t just pick easy or difficult ones. This means you’ll get to see solutions to the wide variety of challenges involved in connecting sensors to Arduino and Raspberry Pi.
In each chapter you’ll find experiments, environmental experiments, and a test project:
Any robot or gadget you build must handle three things: input, processing, and output.
A protocol defines how a sensor talks to the microcontroller board, such as Arduino or Raspberry Pi. The protocol defines how the wires should be connected and how your code should ask for measurements.
Even though there is a staggering amount of different sensors, there is a limited number of popular protocols. You’ll learn each of the protocols as you work through experiments and projects, but here’s an overview of what you’ll be seeing.
You can get an overview of common sensor protocols in Table P-1.
pulseIn()
or gpio.pulseInHigh()
to read the length of the pulse. Because this is handled by a function, you don’t have to get into low-level microcontroller operations such as interrupts; it is all handled by a library.
As you play with the sensors, you’ll get much more familiar with these protocols. Or, if you’re in a hurry to put new sensors in your robots and innovative devices, you can just use the code in this book and look at the details later.
Protocol | Example value | Arduino | Raspberry Pi Python | Example sensors |
Digital resistance | 1 or 0 | digitalRead() | botbook_gpio.read() | Button, IR sensor switch, tilt sensor, passive infrared movement |
Analog resistance | 5%, 10%, 23 C | analogRead() | botbook_mcp3002.readAnalog(), chip | Potentiometer, light-dependent resistor, MQ-3 alcohol, MQ X gas family (smoke, hydrocarbon, CO…), FlexiForce pressure, KY-026 flame, HDJD-S822-QR999 color, LM35 temperature, soil humidity |
Pulse length | 20 milliseconds | pulseIn() | gpio.pulseInHigh() | Ping and HC-SR04 ultrasonic distance, MX2125 acceleration |
Serial port | A9B3C5B3C5 | Serial.read() | pySerial.read() | GT-511C3 Fingerprint scanner, ELB149C5M RFID identity |
I2C | (2.11 g, 0.0 g, 0.1g), very precise values | Wire.h | smbus | Wii Nunchuk, MPU 6050 accelerometer and gyro combination, GY65 atmospheric pressure |
SPI | 57 deg, very precise values | Bit-banging | spidev | MCP3002 analog-to-digital converter |
Bits encoded to very short pulses | 53% | Bit-banging | Bit-banging | DHT11 humidity |
Most users won’t find raw circuit boards and components compelling to play with. Making an attractive package for your gadget or robot makes a huge difference.
This book gives you one example for each project, but there’s no need to follow our instructions blindly. Try different materials and use different tools.
How about using cardboard (Figure P-2), fabric (Figure P-3) or 3D printing (Figure P-4)?
Trying out and learning new techniques makes the process of work more interesting, such as welding or making something out of clay between all the soldering (see Figure P-5).
We also use a lot of recycled materials in our own projects. Obviously they are cheap (free!) but they also give a unique look to a project.
If you need high quality components without fuss, pick a well-known shop, preferably in the Western world. If you want cheap components, look to Asia.
Quality shops mainly selling to makers include Maker Shed, SparkFun, Parallax, and Adafruit. Maker Shed is the shop from the publisher of this book. SparkFun sells a lot of breakout boards, which require you to solder in headers. Parallax created Basic Stamp, the previous generation of microcontroller boards for makers. Adafruit has a lot of parts, many designed by them. The SparkFun and Adafruit websites have a lot of information about their components, including tutorials.
These days, even big-name distributors like Element14 and RS electronics have broken into the Maker market. Finding parts from their huge catalogs is becoming easier, as they’ve started providing clear areas for Arduino and Raspberry Pi.
For some special parts and sometimes very cheap prices, Asia is the continent to go to. DealExtreme is very popular at the moment. Its shipping is slow and quality varies, but the prices are low and the assortment is wide. AliExpress is another Asian shop worth checking out.
The following typographical conventions are used in this book:
Constant width
Constant width bold
Constant width italic
This icon signifies a tip, warning, or general note.
You can download all the source code for this book from http://makesensors.botbook.com.
You can extract the ZIP package by double-clicking it, or by right-clicking and selecting “Extract” from the pop-up menu.
This book is here to help you get your job done. In general, you may use the code in this book in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from MAKE books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission.
We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: “Make: Sensors by Tero Karvinen, Kimmo Karvinen, and Ville Valtokari. Copyright 2014 Tero Karvinen, Kimmo Karvinen, and Ville Valtokari, 978-1-449-36810-4.”
If you feel your use of code examples falls outside fair use or the permission given here, feel free to contact us at [email protected].
Safari Books Online (formerly Safari Books Online) is membership-based training and reference platform for enterprise, government, educators, and individuals.
Members have access to thousands of books, training videos, Learning Paths, interactive tutorials, and curated playlists from over 250 publishers, including O’Reilly Media, Harvard Business Review, Prentice Hall Professional, Addison-Wesley Professional, Microsoft Press, Sams, Que, Peachpit Press, Adobe, Focal Press, Cisco Press, John Wiley & Sons, Syngress, Morgan Kaufmann, IBM Redbooks, Packt, Adobe Press, FT Press, Apress, Manning, New Riders, McGraw-Hill, Jones & Bartlett, and Course Technology, among others.
Maker Media has uploaded this book to the Safari Books Online service. To have full digital access to this book and others on similar topics from MAKE and other publishers, sign up for free at http://oreilly.com/safari.
Please address comments and questions concerning this book to the publisher:
MAKE |
1005 Gravenstein Highway North |
Sebastopol, CA 95472 |
800-998-9938 (in the United States or Canada) |
707-829-0515 (international or local) |
707-829-0104 (fax) |
MAKE unites, inspires, informs, and entertains a growing community of resourceful people who undertake amazing projects in their backyards, basements, and garages. MAKE celebrates your right to tweak, hack, and bend any technology to your will. The MAKE audience continues to be a growing culture and community that believes in bettering ourselves, our environment, our educational system—our entire world. This is much more than an audience, it’s a worldwide movement that Make is leading—we call it the Maker Movement.
For more information about MAKE, visit us online:
MAKE magazine: http://makezine.com/magazine/ |
Maker Faire: http://makerfaire.com |
Makezine.com: http://makezine.com |
Maker Shed: http://makershed.com/ |
We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at:
http://bit.ly/make-sensors |
To comment or ask technical questions about this book, send email to:
[email protected] |
The authors would like to thank Hipsu, Marianna, Nina, Paavo Leinonen, and Valtteri.