Make: Sensors Tero Karvinen, Kimmo Karvinen, and Ville Valtokari (Hi MAKER MEDIA SEBASTOPOL. CA
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 2. Arduino 25 Basic Arduino Setup 26 Ubuntu Linux 26 Windows 7 and Windows 8 27 OSX 27 Hello World 28 Anatomy of an Arduino Program 29 Shields Make It Easy and Robust 29 3. Distance 31 Experiment: Measure Distance with Ultrasonic Sound (PING) 32 Ping Code and Connections for Arduino 33 Ping Code and Connections for Raspberry Pi 35 HC-SR04 Ultrasonic Sensor 38 HC-SR04 Code and Connection for Arduino 38 HC-SR04 Code and Connections for Raspberry Pi 40 Echo Calculations Explained 42 Environment Experiment: invisible Objects 43 Experiment: Detect Obstacles With Infrared (IR Distance Sensor).. 44 IR Switch Code and Connections for Arduino 45 IR Switch Code and Connections for Raspberry Pi 47 Environment Experiment: How to See Infrared 48 Experiment: Follow Movement with Infrared (IR Compound Eye). 50 Compound Eye Code and Connection for Arduino 51 Compound Eye Code and Connections for Raspberry Pi 54 Installing SpiDev 56 Alternative Circuits for Raspberry Pi 57 Test Project: Posture Alarm (Arduino) 58 What You'll Learn 58 Piezo Beeper 59 Alarm, Alarm! 61 Combining Piezo and IR Sensor 62 Putting Everything in a Neat Package 64 4. Smoke and Gas 67 Experiment: Detect Smoke (Analog Gas Sensor) 68 MQ-2 Code and Connection for Arduino 69 MQ-2 Code and Connection for Raspberry Pi 71 Environment Experiment: Smoke Goes Up 72 Experiment: Breathalyzer (Alcohol Sensor MQ-303A) 74 Environment Experiment: Try It Without Drinking 77 Test Project: Emailing Smoke Alarm 78 What You'll Learn 78 iv Make: Sensors
Python for Email and Social Media 79 Building It 79 How Does Email Work? 79 Could Arduino Send Email? Not Easily 80 Code for Raspberry Pi 80 Packaging 83 5. Touch 89 Experiment: Button 89 Pull-Up Resistor 90 Code and Connection for Arduino 91 Code and Connection for Raspberry Pi 93 Experiment: Microswitch 94 Microswitch Code and Connection for Arduino 95 Microswitch Code and Connection for Raspberry Pi 97 Experiment: Potentiometer (Variable Resistor, Pot) 98 Potentiometer Code and Connection for Arduino 99 Potentiometer Code and Connection for Raspberry Pi 101 Experiment: Sense Touch Without Touch (Capacitive Touch Sensor QT113) 103 QT113 Code and Connection for Arduino 104 QT113 Code and Connection for Raspberry Pi 105 Environment Experiment: Sensing Touch Through Wood 106 Experiment: Feel the Pressure (FlexiForce) 108 FlexiForce Code and Connection for Arduino 108 FlexiForce Code and Connection for Raspberry Pi 109 Experiment: Build Your Own Touch Sensor 111 Capsense Code and Connection for Raspberry Pi 113 Test Project: Haunted Ringing Bell 114 What You'll Learn 115 Servo Motors 115 Haunted Bell Code and Connection for Arduino 119 Attaching Servo to Ringing Bell 122 8. Movement 123 Experiment: Which Way Is Up? (Tilt Ball Switch) 123 Tilt Sensor Code and Connection for Arduino 124 Tilt Sensor Code and Connection for Raspberry Pi 125 Experiment: Good Vibes with Interrupt (Digital Vibration Sensor) 126 Vibration Code and Connection for Arduino 127 Vibration Code and Connection for Raspberry Pi 128 Experiment: Turn the Knob 130 Rotary Encoder Code and Connection for Arduino 130 Rotary Encoder Code and Connection for Raspberry Pi 132 Experiment: Thumb Joystick (Analog Two-Axis Thumb Joystick) 134 Table of Contents v
Joystick Code and Connection for Arduino 135 Joystick Code and Connection for Raspberry Pi 136 Environment Experiment: Salvage Parts from an Xbox Controller 138 Experiment: Burglar Alarm! {Passive Infrared Sensor) 140 Burglar Alarm Code and Connection for Arduino 140 Burglar Alarm Code and Connection for Raspberry Pi 142 Environment Experiment: Cheating an Alarm 144 Test Project: Pong 147 What You'll Learn 148 Pong Packaging Tips 152 Automatically Start Your Game When Raspberry Pi Boots 156 Run Game on Login 156 7. Light Automatic Login 157 161 Experiment: Detecting Flame (Flame Sensor) 161 Flame Sensor Code and Connection for Arduino 162 Flame Sensor Code and Connection for Raspberry Pi 164 Environment Experiment: Flame Precision 165 Experiment: See the Light (Photoresistor, LDR) 166 LDR Code and Connection for Arduino 168 LDR Code and Connection for Raspberry Pi 169 Environment Experiment: One Direction 170 Experiment: Follow the Line 172 Line Sensor Code and Connection for Arduino 172 Line Sensor Code and Connection for Raspberry Pi 174 Environment Experiment: Black is White 175 Experiment: All the Colors of the 'Bow 177 Color Sensor Code and Connection for Arduino 178 Color Sensor Code and Connection for Raspberry Pi 180 Test Project: Chameleon Dome 182 What You'll Learn RGB LED 183 183 Easing Input to Output 189 Combining Codes 190 Dome Building Tips 8. Acceleration Acceleration vs. Angular Velocity 195 201 Experiment: Accelerate with MX2125 202 201 Decoding MX2125 Pulse Length 203 Accelerometer Code and Connection for Arduino 205 Accelerometer Code and Connection for Raspberry Pi 206 Experiment: Accelerometer and Gyro Together 208 MPU 6050 Code and Connection for Arduino 209 vi Make: Sensors
MPU 6050 Code and Connection for Raspberry Pi 215 Hexadecimal, Binary, and Other Numbering Systems 219 Bitwise Operations 221 Experiment: Hacking Wii Nunchuk (with I2C) 225 Nunchuk Code and Connection for Arduino 226 Nunchuk Code and Connection for Raspberry Pi 229 Test Project: Robot Hand Controlled by Wii Nunchuk 232 What You'll Learn 233 Adding Hand Mechanics 237 3 Identity 239 Keypad 240 Keypad Code and Connection for Arduino 241 Keypad Code and Connection for Raspberry Pi 243 Environment Experiment: Revealing Fingerprints 246 Fingerprint Scanner GT-511C3 247 Fingerprint Sensor Code and Connection for Arduino Mega 249 Fingerprint Sensor Code and Connection for Raspberry Pi 255 RFID with ELB149C5M Electronic Brick 261 RFID Code and Connection for Arduino Mega 263 RFID Code and Connection for Raspberry Pi 265 Test Project: Ancient Chest from the Future 268 What You'll Learn 268 Operating the Chest 268 The Box 269 Ancient Chest Code and Connection for Arduino 271 Who or What Is It? 277 10. Electricity and Magnetism 279 Experiment: Voltage and Current 279 AttoPilot Code and Connection for Arduino 281 AttoPilot Code and Connection for Raspberry Pi 282 Experiment: Is It Magnetic? 284 Hall Effect Sensor Code and Connection for Arduino 285 Hall Effect Sensor Code and Connection for Raspberry Pi 286 Experiment: Magnetic North with LSM303 Compass-Accelerometer.. 288 Calibrate Your Module 289 LSM303 Code and Connection for Arduino 290 LSM303 Code and Connection for Raspberry Pi 295 LSM330 Protocol 299 Compass Heading Calculation 299 Experiment: Hall Switch 301 Hall Switch Code and Connection for Arduino 302 Hall Switch Code and Connection for Raspberry Pi 303 Test Project: Solar Cell Web Monitor 304 Table of Contents vii
What You'll Learn 305 Connecting Solar Cells 305 Turn Raspberry Pi into Web Server 308 Finding Your IP Address 309 Making Your Home Page on Raspberry Pi 309 Solar Panel Monitor Code and Connection for Raspberry Pi... 310 Timed Tasks with Cron 312 What's Next? 313 11. Sound 315 Experiment: Hearing Voices/Volume Level 315 Microphone Breakout Code and Connection for Arduino 316 Microphone Breakout Code and Connection for Raspberry Pi. 317 Environment Experiment: Can You Hear a Pin Drop? 319 Test Project: Visualize Sound over HDMI 320 What You'll Learn 320 Enabling the Serial Port in Raspberry Pi 320 Visualizer Code and Connection for Raspberry Pi 321 Fast Fourier Transformation 324 What Next? 326 12. Weather and Climate 327 Experiment: Is It Hot in Here? 327 LM35 Code and Connection for Arduino 328 LM35 Code and Connection for Raspberry Pi 329 Environment Experiment: Changing Temperature 331 Experiment: Is It Humid in Here? 332 How Humid Is Your Breath? 333 DHT11 Code and Connection for Arduino 334 DHT11 Code and Connection for Raspberry Pi 336 Talking to Arduino from Raspberry Pi 337 Atmospheric Pressure GY65 339 GY65 Code and Connection for Arduino 340 Using Arduino Libraries 341 GY65 Arduino Library Explained 342 GY65 Code and Connection for Raspberry Pi 346 Experiment: Does Your Plant Need Watering? (Build a Soil Humidity Sensor) 350 Soil Sensor Code and Connection for Arduino 350 Soil Sensor Code and Connection for Raspberry Pi 351 Test Project: E-paper Weather Forecast 353 What You'll Learn 354 Weather Forecast Code and Connection for Arduino 354 Environment Experiment: Look Ma, No Power Supply 362 Storing Images in Header Files 362 viii Make: Sensors
BMP to C Conversion Program 363 Enclosure Tips 365 Appendix A. Raspberry Pi Linux Quick Reference 369 Index 371 Table of Contents ix