Make: Sensors
Tero Karvinen
Kimmo Karvinen
Ville Valtokari
Preface
Welcome to Make: Sensors . Soon youll be making gadgets that can sense it allfrom dangerous gases to acceleration. In this book, youll 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; youll see examples of this later).
A microcontroller board is the brain of the robot, system, or gadget youre building. Youll write your own software to run on the microcontroller. In this book, youll work with two very popular boards: Arduino and Raspberry Pi. Either of these makes it easy to write software code to work with electronics.
Its About Your Ideas
If your interest in electronics started with a desire to quickly learn some basics and then design your own robots, gadgets, or projects, youre in the right place. This book will show you how to go from idea to reality quickly.
Theory, skills, and basics are usefulas 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, youll 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-lifeprojects. For example, we used Arduino to build the sun sensor prototype for Finlands first satellite ().
Figure 1. Finland launches its first satellite in 2014. We designed and built the sun sensor prototype with Arduino.
How to Read This Book
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:
- Distance ()
- Smoke and gas ()
- Touch ()
- Movement ()
- Light ()
- Acceleration and angular momentum ()
- Identity ()
- Electricity ()
- Sound ()
- Weather and climate ()
You can also use Make: Sensors as a makers coffee-table book: browse it to get ideas of whats available, and look for inspiration for new projects.
If you want to understand how sensors are connected to Arduino and Raspberry Pi, youll 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 arent on the market yet.
When we chose the sensors for you, we picked a variety of useful and interesting sensors. We didnt just pick easy or difficult ones. This means youll get to see solutions to the wide variety of challenges involved in connecting sensors to Arduino and Raspberry Pi.
In each chapter youll find experiments, environmental experiments, and a test project:
- Experiments give you quick instructions on how to use a single sensor with Arduino and Raspberry Pi. You can easily use these as building blocks for your own projects or just to see how the sensor works.
- Environmental experiments let you play with sensors and monitor changes in the surrounding environment. This gives you insight into how sensors see the world and how they really work.
- Sensors are more fun when you actually do something with the readings they give you. In test projects youll build a device or gadget around one sensor. Youll learn how to use different outputs such as RGB LEDs, e-paper, and servo motors. Test projects also work as quick starting points for your own innovations.
Input, Processing, Output
Any robot or gadget you build must handle three things: input , processing , and output .
- Because most of the devices you build wont have a keyboard or a mouse, sensors are your inputs. Take a quick look at the table of contents, and keep in mind that this is just a fraction of whats out there. There are countless sensors to measure everything you could imagine.
- Processing happens in your program, running in Arduino or Raspberry Pi. In your program, you get to decide what happens next.
- Outputs affect the world around the device. You could light an LED, turn on a servo motor, or play a sound. Those are three of the most common types of output, but there are others (for example, haptic feedback such as vibration, displaying something on an e-paper screen, or turning on a household appliance).
Protocols
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 are a staggering amount of different sensors, there are a limited number of popular protocols. Youll learn each of the protocols as you work through experiments and projects, but heres an overview of what youll be seeing.
You can get an overview of common sensor protocols in
Digital resistance Some sensors work like a button and have two states, on or off. These sensors are easy to read. The on state is represented when a voltage referred to as HIGH is applied to the microcontroller input pin. This is usually either 3.3 volts or 5 volts depending on the microcontroller board youre using. Analog resistance Analog resistance sensors change their resistance in response to a physical change (such as turning the knob of a dial). Arduino and Raspberry Pi measure the changes in resistance by measuring the voltage level that passes through the sensor. For example, you can turn a potentiometer to make its resistance larger or smaller. These analog resistance sensors are very easy to make with Arduino. Raspberry Pi needs an external chip for measuring analog values. Youll learn to use the MCP3002 analog-to-digital converter to measure resistance with Raspberry Pi in . Most analog input sensors report their value using resistance, so they are analog resistance sensors. Pulse width Some sensors report their value with a pulse width, or the period of time in which the pin is held HIGH. You use functions like pulseIn()
or gpio.pulseInHigh()
to read the length of the pulse. Because this is handled by a function, you dont have to get into low-level microcontroller operations such as interrupts ; it is all handled by a library. Serial port A serial port sends text characters between two devices. Its the same technique your computer uses when talking to Arduino over USB. Youll become quite familiar with the serial port when you print some messages to the Arduino serial monitor in various projects. I2C I2C is a popular industry standard protocol. It is commonly found inside computers and well known from Wii Nunchuk joysticks. I2C allows 128 devices to be connected to the same wires. In