100% found this document useful (4 votes)
29 views

Arduino and Raspberry Pi Sensor Projects for the Evil Genius 1st Edition Chin - eBook PDF download

The document is a promotional and informational piece about the eBook 'Arduino and Raspberry Pi Sensor Projects for the Evil Genius' by Robert Chin, which covers various projects using Arduino and Raspberry Pi. It includes details on the content structure, programming basics, and hands-on examples involving sensors and electronics. Additionally, it provides links to download the book and other related eBooks from the same publisher.

Uploaded by

uwongpav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (4 votes)
29 views

Arduino and Raspberry Pi Sensor Projects for the Evil Genius 1st Edition Chin - eBook PDF download

The document is a promotional and informational piece about the eBook 'Arduino and Raspberry Pi Sensor Projects for the Evil Genius' by Robert Chin, which covers various projects using Arduino and Raspberry Pi. It includes details on the content structure, programming basics, and hands-on examples involving sensors and electronics. Additionally, it provides links to download the book and other related eBooks from the same publisher.

Uploaded by

uwongpav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

Arduino and Raspberry Pi Sensor Projects for the

Evil Genius 1st Edition Chin - eBook PDF pdf


download

https://ebooksecure.com/download/arduino-and-raspberry-pi-sensor-
projects-for-the-evil-genius-ebook-pdf/

Download more ebook from https://ebooksecure.com


We believe these products will be a great fit for you. Click
the link to download now, or visit ebooksecure.com
to discover even more!

Raspberry Pi Electronics Projects for the Evil Genius


1st Edition Norris - eBook PDF

https://ebooksecure.com/download/raspberry-pi-electronics-
projects-for-the-evil-genius-ebook-pdf/

Hacking Electronics: Learning Electronics with Arduino


and Raspberry Pi, 2nd Edition Simon Monk - eBook PDF

https://ebooksecure.com/download/hacking-electronics-learning-
electronics-with-arduino-and-raspberry-pi-2nd-edition-ebook-pdf/

DIY Drones for the Evil Genius: Design, Build, and


Customize Your Own Drones: Design, Build, and Customize
Your Own Drones 1st Edition - eBook PDF

https://ebooksecure.com/download/diy-drones-for-the-evil-genius-
design-build-and-customize-your-own-drones-design-build-and-
customize-your-own-drones-ebook-pdf/

Programming the Raspberry Pi, Third Edition: Getting


Started with Python Simon Monk - eBook PDF

https://ebooksecure.com/download/programming-the-raspberry-pi-
third-edition-getting-started-with-python-ebook-pdf/
(eBook PDF) Fluid Mechanics for Engineers by David A.
Chin

http://ebooksecure.com/product/ebook-pdf-fluid-mechanics-for-
engineers-by-david-a-chin/

Advanced Sensor Technology: Biomedical, Environmental,


and Construction Applications 1st edition - eBook PDF

https://ebooksecure.com/download/advanced-sensor-technology-
biomedical-environmental-and-construction-applications-ebook-pdf/

(eBook PDF) Templates For Managing Training Projects

http://ebooksecure.com/product/ebook-pdf-templates-for-managing-
training-projects/

Programming the Intel Galileo: Getting Started with the


Arduino - Compatible Development Board 1st Edition -
eBook PDF

https://ebooksecure.com/download/programming-the-intel-galileo-
getting-started-with-the-arduino-compatible-development-board-
ebook-pdf/

Randomly Deployed Wireless Sensor Networks 1st Edition


Xi Chen - eBook PDF

https://ebooksecure.com/download/randomly-deployed-wireless-
sensor-networks-ebook-pdf/
Arduino and Raspberry Pi Sensor
Projectsfor the Evil Genius™
Arduino and Raspberry Pi Sensor
Projects for the Evil Genius™

Robert Chin

New York Chicago San Francisco Athens London Madrid


Mexico City Milan New Delhi Singapore Sydney Toronto
Library of Congress Control Number: 201794884

McGraw-Hill Education books are available at special quantity discounts to use as premiums and sales
promotions or for use in corporate training programs. To contact a representative, please visit the Contact
Us page at www.mhprofessional.com.

Arduino and Raspberry Pi Sensor Projects for the Evil Genius™

Copyright © 2018 by McGraw-Hill Education. All rights reserved. Printed in the United States of America.
Except as permitted under the United States Copyright Act of 1976, no part of this publication may be
reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without
the prior written permission of the publisher.

McGraw-Hill Education, the McGraw-Hill Education logo, Evil Genius and TAB, and related trade dress
are trademarks or registered trademarks of McGraw-Hill Education and/or its affiliates in the United States
and other countries and may not be used without written permission. All other trademarks are the property
of their respective owners. McGraw-Hill Education is not associated with any product or vendor mentioned
in this book.

1 2 3 4 5 6 7 8 9 DSS 22 21 20 19 18 17

ISBN 978-1-260-01089-3
MHID 1-260-01089-9

e-ISBN 978-1-260-01090-9
e-MHID 1-260-01090-2

This book is printed on acid-free paper.

Sponsoring Editor
Michael McCabe
Editorial Supervisor
Donna M. Martone Production Supervisor
Lynn M. Messina

Acquisitions Coordinator
Lauren Rogers
Project Manager
Patricia Wallenburg, TypeWriting

Copy Editor
James Madru

Proofreader
Claire Splan

Indexer
Claire Splan
Art Director, Cover
Jeff Weeks

Composition
TypeWriting

Information contained in this work has been obtained by McGraw-Hill Education from sources believed to
be reliable. However, neither McGraw-Hill Education nor its authors guarantee the accuracy or
completeness of any information published herein, and neither McGraw-Hill Education nor its authors shall
be responsible for any errors, omissions, or damages arising out of use of this information.
About the Author

Robert Chin has a Bachelor of Science degree in computer engineering and is


experienced in developing projects on the TI CC3200 SimpleLink, Android,
Arduino, Raspberry Pi, and PC Windows platforms using C/C++, Java, Python,
Unreal Script, DirectX, OpenGL, and OpenGL ES 2.0. He is the author of Home
Security System DIY PRO Using Android and TI CC3200 SimpleLink, Home
Security Systems DIY Using Android and Arduino, and Beginning Arduino
ov7670 Camera Development. He is also the author of Beginning Android 3d
Game Development and Beginning IOS 3d Unreal Games Development, both
published by Apress, and was the technical reviewer for UDK Game
Development, published by Course Technology CENGAGE Learning. Beginning
Android 3d Game Development was licensed to Tsinghua University through
Tsinghua University Press.
Contents

1 Introducing the Arduino and Raspberry Pi


What Is an Arduino?
Arduino Uno
Arduino Uno Specifications
Arduino Uno Components
Arduino Development System Requirements
Arduino IDE Software
Hands-on Example:
A Simple Arduino “Hello World” Program with a LED
Playing Around with the Code
What Is Raspberry Pi?
Raspberry Pi 3 Hardware Overview
Raspberry Pi System Setup
Python IDLE Development Environment
Hands-on Example:
A Simple Raspberry Pi “Hello World” Program with a LED
Summary

2 Arduino and Raspberry Pi Programming Language Basics


Overview of the C/C++ Language for the Arduino
Object-Oriented Programming
Overview of Python for Raspberry Pi
Python Functions
Python Modules
Python Class Overview
Python Class Inheritance
Summary

3 Basic Electrical Components


Electronics Basics
Resistor
LEDs
Piezo Buzzer
Potentiometer
Push Buttons
Breadboards
Hands-on Example:
Arduino Serial Monitor and LED Indicator
Hands-on Example:
Arduino RGB LED
Hands-on Example:
Arduino LED Buzzer Siren
Hands-on Example:
Arduino Random RGB LED Colors Using a Potentiometer
Hands-on Example:
Arduino RGB Light Switch
Hands-on Example:
Raspberry Pi LED Blinker Counter
Hands-on Example:
Raspberry Pi LED Fading
Hands-on Example:
Raspberry Pi RGB LED Color Selector
Hands-on Example:
Raspberry Pi LED Fading Up and Down Using a Button
Summary

4 Touch Sensor Projects


Analog Joystick
Hands-on Example:
Arduino Joystick Test
Hands-on Example:
Arduino “Simon Says” Game
Running the Program
Rotary Encoder Controller (KY-040)
Hands-on Example:
Arduino Rotary Encoder Test
Hands-on Example:
Arduino Rotary Encoder Controlling an RGB LED
Hands-on Example:
Raspberry Pi Rotary Encoder Test
Hands-on Example:
Raspberry Pi Rotary Encoder LED Blinker
Summary

5 Environmental Sensor Projects I


Reed Switch Magnetic Field Sensor
Hands-on Example:
Arduino Reed Switch Test
Hands-on Example:
Arduino Door Buzzer Alarm
Hands-on Example:
Raspberry Pi Reed Switch Test
Hands-on Example:
Raspberry Pi Reed Switch Door Alarm
TMP36 Temperature Sensor
Hands-on Example:
Arduino TMP36 Temperature Sensor Test
Flame Sensor
Hands-on Example:
Arduino Flame Sensor Test
Hands-on Example:
Arduino Fire Alarm
Hands-on Example:
Raspberry Pi Flame Sensor Test
Hands-on Example:
Raspberry Pi Fire Alarm
Infrared Proximity/Collision Sensor
Hands-on Example:
Arduino Infrared Proximity/Collision Detector
Hands-on Example:
Raspberry Pi Infrared Collision/Proximity Sensor Alarm
Temperature and Humidity Sensor (DHT11)
Hands-on Example:
Arduino DHT11 Temperature and Humidity Sensor Test
Summary

6 Environmental Sensor Projects II


Water Detector/Soil Moisture Sensor
Hands-on Example:
Arduino Water Detector Alarm
Hands-on Example:
Arduino Soil Moisture Detector
Light Detector (Photo Resistor)
Hands-on Example:
Arduino Rooster Alarm
Sound Detector
Hands-on Example:
Arduino Light Clapper
Hands-on Example:
Raspberry Pi “Out of Breath” Game
Summary

7 Human Sensor Projects


Hands-on Example:
Arduino Glass Break Alarm
Hands-on Example:
Raspberry Pi Glass Break Alarm
HC-SR501 Infrared Motion Detector
Hands-on Example:
Arduino Infrared Motion Detector Alarm
Hands-on Example:
Raspberry Pi Infrared Motion Detection Alarm
HC-SR04 Distance Sensor
Hands-on Example:
Arduino HC-SR04 Distance Sensor Intruder Alarm
Hands-on Example:
Arduino Collision Alarm
Summary

8 Arduino TFT LCD Display and Image Sensor Projects


Arduino TFT LCD Color Display Screen with SD Card Reader/Writer
Hands-on Example:
Arduino TFT Display and SD Card Reader/Writer Test
Hands-on Example:
Arduino TFT Display Door Entry Alarm with SD Card Logging
Arduino Cameras
Hands-on Example:
Arduino ArduCAM OV2640 Mini Portable Programmable Digital Camera
System
Summary
Index
CHAPTER 1
Introducing the Arduino and Raspberry Pi

THIS CHAPTER WILL INTRODUCE YOU to the Arduino and the Raspberry Pi. First, I
give a brief explanation of what the Arduino is. Then I specifically address the
Arduino Uno, discussing its general features, including its capabilities and key
functional components. This is followed by a discussion of the Arduino
Integrated Development Environment (IDE) software, which is needed to
develop programs for the Arduino. Each key function of the Arduino IDE is
reviewed, followed by a hands-on example giving detailed step-by-step
instructions on how to set up the Arduino for development and how to run and
modify an example program using the Arduino IDE. Next comes the Raspberry
Pi. I discuss what the Raspberry Pi is and the specifications for the Raspberry Pi
3. This section tells you how to set up the Raspberry Pi before using it for the
first time. Raspberry Pi hardware features are addressed, including the general
purpose input-output (GPiO) pin specifications. The final section includes a
hands-on example of how to control a light-emitting diode (LED) using the
Raspberry Pi.

What Is an Arduino?
The Arduino is an open-source microcontroller that uses the C and C++
languages to control digital and analog outputs to devices and electronic
components and to read in digital and analog inputs from other devices and
electronic components for processing. For example, an Arduino can read a signal
from a sensor in a home security system that detects the heat that a human being
emits. The sensor sends a signal to the Arduino indicating that a person is in the
home. After receiving this information, the Arduino can send commands to a
camera such as the ArduCAM Mini digital camera to start taking pictures of the
intruder. There are many different Arduino models out there. However, to create
the examples in this book, you will need an Arduino model with enough pins to
connect the components you desire, such as a camera, Bluetooth adapter, and/or
motion sensor. Figure 1-1 shows the official Arduino logo.

Figure 1-1 Official Arduino logo.

Note: The official website of the Arduino is www.arduino.cc.

Arduino Uno
There are a great many Arduino products out there, ranging from models that are
small and can actually be worn by the user to models with many digital and
analog input-output pins. For the projects in this book, I recommend the Arduino
Uno, which is an open-source microcontroller that has enough digital ports to
accommodate a camera, a Secure Digital (SD) card reader/writer with enough
digital and analog ports for other devices, sensors, lights, and any other gadgets
that you may require for your own custom projects. The official Arduino Uno
board is made by a company called Arduino SRL, formerly Smart Projects,
formed by one of the founders of the Arduino (Figure 1-2). The newer official
Arduino Uno boards are slightly different in that they are more blue-green
instead of blue in color and contain the Genuino logo under the main Arduino
logo. The Genuino trademark is used outside the United States as a result of the
split between Arduino founders.
Figure 1-2 The official Arduino Uno.

There are also unofficial Arduino Uno boards made by other companies. A
good way to tell whether a board is official or unofficial is by the color of a
component that is located near the Arduino’s USB port. This component on an
official Arduino board is a metallic gold color. The component on an unofficial
board is green. The writing on the components also differs (Figure 1-3).
Figure 1-3 Metallic gold colored component on an official Arduino Uno board.

A number of other companies also manufacture Arduino Uno boards.


Because the Arduino is an open-source item, other companies can legally
manufacture the board, and the boards vary in quality and price. Generally, an
unofficial Arduino Uno costs around $10, and an official Arduino Uno costs
around $20. The distinguishing parts of an official Arduino board are the golden
component and the high-quality Arduino and Genuino logos printed on the board
(Figure 1-4).
Figure 1-4 Key parts of an official Arduino Uno.

Arduino Uno Specifications


Microcontroller: ATmega328P
Operating voltage: 5 V
Input voltage (recommended): 7–12 V
Input voltage limit: 6–20 V
Digital I/O pins: 14 (of which 6 provide PWM output) PWM digital I/O pins:
6
Analog input pins: 6
DC current per I/O pin: 20 mA DC current for 3.3-V pin: 50 mA Flash
memory: 32 kB (ATmega328P) (of which 0.5 kB used by boot loader)
SRAM: 2 kB (ATmega328P) EEPROM: 1 kB (ATmega328P) Clock
speed: 16 MHz Length: 68.6 mm Width: 53.4 mm Weight: 25 g

Arduino Uno Components


This section covers the functional components of the Arduino Uno.

USB Connection Port


The Arduino Uno has a USB connector that is used to connect the Arduino to the
main computer development system via standard USB A male to B male cable
so that it can be programmed and debugged (Figure 1-5).

Figure 1-5 USB connector.

9-V Battery Connector


The Arduino Uno has a 9-V battery connector where you can attach a 9-V
battery to power the Arduino (Figure 1-6).
Figure 1-6 Arduino Uno 9-V battery connector.

Reset Button
There is a Reset button on the Arduino Uno that you can press to reset the board.
This restarts the program contained in the Arduino’s memory (Figure 1-7).

Figure 1-7 Arduino Uno Reset button.

Digital Pins
The Arduino Uno has many digital pins capable of simulating analog output
through the process of pulse-width modulation (PWM). For example, a LED
light generally has only two modes: on (full brightness) and off (no light
emitted). However, with digital PWM, the LED light can appear to have a
brightness in between on and off. For instance, with PWM, a LED can start from
an off state and slowly brighten until it is at its highest brightness level and then
slowly dim until back to the off state. The digital pins on the Arduino Uno are
pins 0 through 13. These PWM-capable digital pins are circled in Figure 1-8.

Figure 1-8 Arduino Uno digital pins.

Communication
The communication section of the Arduino Uno contains pins for serial
communication between the Arduino and other devices, such as a Bluetooth
adapter or a personal computer. The Tx0 and Rx0 pins are connected to the USB
port and are used for communication between your Arduino and your computer
by means of a USB cable. The Serial Monitor that can be used for sending data
to the Arduino and reading data from the Arduino uses the Tx0 and Rx0 pins.
Thus you should not connect anything to these pins if you want to use the Serial
Monitor to debug your Arduino programs or to receive user input (Figure 1-9). I
will talk more about the Serial Monitor later in this book.
Figure 1-9 Arduino Uno serial communication.

The I2C interface consists of an SDA pin (which is pin 4) that is used for
data and an SCL pin (which is pin 5) that is used for clocking or driving the
device or devices attached to the I2C interface. The SDA and SCL pins are
circled in Figure 1-10.
Figure 1-10 Arduino Uno I2C interface.

Analog Input
The Arduino Uno has six analog input pins that can read in a range of values
instead of just digital values of 0 or 1. The analog input pin uses a 10-bit analog-
to-digital converter to transform voltage input in the range of 0 to 5 V into a
number in the range between 0 and 1,023 (Figure 1-11).
Figure 1-11 Arduino Uno analog input.

Power
The Arduino Uno has outputs for 3.3 and 5 V. One section that provides power is
located on the side of the Arduino. You can also provide a separate power source
by connecting the positive terminal of the power source to the Vin pin and the
ground of the power source to the Arduino’s ground. Make sure that the voltage
being supplied is within the Arduino’s voltage range (Figure 1-12).
Figure 1-12 Arduino Uno 3.3- and 5-V power outputs.

The ground connections on the Arduino Uno are shown circled in Figure 1-
13.
Figure 1-13 Arduino Uno ground connections.

Arduino Development System Requirements


Arduino projects can be developed on Windows, Mac, and Linux operating
systems. The software needed to develop programs that run on the Arduino can
be downloaded from the main website at www.arduino.cc/en/Main/Software.
The following is a summary of the different types of Arduino IDE
distributions that are available for download. You will only need to download
and install one of these files. The file you choose will depend on the operating
system your computer is using.

Windows
Windows Installer. This is an .exe file that must be run to install the Arduino
IDE.
Windows zip file for non-administrator install. This is a zip file that must be
uncompressed in order to install the Arduino IDE. 7-zip is a free file
compression and uncompression program available at www.7-zip.org that can
be used to uncompress this program.

An Important Note: For Windows XP, I recommend the 1.0.5 r2 version of the Arduino IDE.
Later versions may not be stable and may terminate unexpectedly, behave erratically, or
may not be able to compile Arduino source code.

Mac
Mac OS X 10.7 Lion or newer. This is a zip file that must be uncompressed
and installed for users of the Mac operating system.

Linux
Linux 32 bits. Installation file for the Linux 32-bit operating system.
Linux 64 bits. Installation file for the Linux 64-bit operating system.
The easiest and cheapest way to start Arduino development is probably by
using the Windows version on an older operating system such as Windows XP.
In fact, the examples in this book were created using the Windows version of the
Arduino IDE running on Windows XP. There are in fact many sellers on Ebay
from whom you can buy a used Windows XP computer for around $50 to $100.
If you are starting from scratch and are looking for an inexpensive development
system for the Arduino, consider buying a used computer with Windows XP. The
only caution is that support for Windows XP has ended in the United States and
some other parts of the world. In China, Windows XP may still be supported
with software updates such as security patches.

Arduino IDE Software


Arduino IDE is the program used to develop the program code that runs on and
controls the Arduino. For example, to have your Arduino control the lighting
state of a LED, you will need to write a computer program in C/C++ using the
Arduino IDE. Then you will need to compile the program into a form that the
Arduino is able to execute and then transfer the final compiled program using
the Arduino IDE. From there the program automatically executes and controls
the LED that is connected to the Arduino.
New versions of the Arduino IDE are compiled daily or hourly and are
available for download. Older versions of the IDE are also available for
downloading at www.arduino.cc/en/Main/OldSoftwareReleases.
This section discusses the key features of the Arduino IDE software. The
IDE you are using may be slightly different from the version discussed in this
section, but the general functions should still be the same. I won’t go in depth
into every detail of the IDE because this section is meant as a quick-start guide
and not a reference manual. I will cover the critical features of the Arduino IDE
that you will need to get started on the projects in this book (Figure 1-14).
Figure 1-14 The Arduino IDE.

The Verify button checks to see whether the program you have entered into
the Arduino IDE is valid and without errors (Figure 1-15). These uncompiled
programs are called sketches.
Figure 1-15 The Verify button.

The Upload button first verifies that the program in the IDE is a valid C/C++
program with no errors, compiles the program into a form the Arduino can
execute, and then finally transfers the program via the USB cable connected to
your computer to your Arduino board (Figure 1-16).

Figure 1-16 The Upload button.

The New File button creates a new blank file or sketch inside the Arduino
IDE, where you can create your own C/C++ program for verification,
compilation, and transfer to the Arduino (Figure 1-17).

Figure 1-17 The New File button.

The Open File button is used to open and load the Arduino C/C++ program
source code from a file or load various sample source codes from example
Arduino projects that are included with the IDE (Figure 1-18).
Figure 1-18 The Open File button.

The Save button saves the sketch on which you are currently working to
disk. A File Save dialog is brought up first, and then you will be able to save the
file on your computer’s hard drive (Figure 1-19).

Figure 1-19 The Save button.

The Serial Monitor button brings up the Serial Monitor debug program,
where you can examine the output of debug statements from the Arduino
program. The Serial Monitor can also accept user input that can be processed by
the Arduino program (Figure 1-20). I will discuss the Serial Monitor and how to
use it as a debugger and input console later in this book.

Figure 1-20 The Serial Monitor button.

The main window of the Arduino IDE also includes other important features.
The title bar of the IDE window contains the Arduino IDE version number. In
Discovering Diverse Content Through
Random Scribd Documents
back
back
back
back
back
back
back
back
back
back
back
back
back

You might also like