Professional Documents
Culture Documents
Robert Chin
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.
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
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
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.
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.
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).
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.
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.
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.
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).
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).
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).
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.
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
Figure 1-21, the Arduino version number is 1.0.5 r2. The sketch name is
displayed in the source code tab and is “Blink,” which is one of the sample
sketches that comes with the Arduino IDE. The source code area, which is the
large white area with scrollbars on the right side and bottom, is where you enter
your C/C++ source code that will control the behavior of the Arduino. The
bottom black area in the IDE is where warning and errors are displayed from the
code verification process. At the bottom left-hand corner of the IDE is a number
that represents the line number in the source code where the user’s cursor is
currently located. In the lower right-hand corner of the IDE is the currently
selected Arduino model and COM port to which the Arduino is attached.
Note: The Arduino website also contains links to instructions for installing the Arduino IDE
for Windows, Mac, and Linux at www.arduino.cc/en/Guide/HomePage. The installation for
Linux depends on the exact version of Linux being used.
The code that is loaded into the Arduino IDE should look like the code in
Listing 1-1.
If you are using a Mac, then the serial port selection should be something
like “/dev/tty.usbmodem” instead of a COMxx value.
Next, with the Arduino connected, press the Upload button to verify,
compile, and then transfer the “Blink” example program to the Arduino. After
the program has finished uploading, you should see a message that the upload
has been completed in the Warnings/error window at the bottom of the IDE
inside the black window (Figure 1-27).
Note: The Upload button does the job of the Verify button and also uploads the final
compiled program to the Arduino.
Final Result
The final result will be a blinking light on the Arduino board near digital pin 13.
By design, the Arduino board has a built-in LED connected to pin 13. So this
example did not require you to connect an actual separate LED to the Arduino
board (Figure 1-28).
Raspberry Pi 3 Model B
Many models of the Raspberry Pi have been produced, but the type of Raspberry
Pi that I will use in this book is the newest and most popular Raspberry Pi: the
Raspberry Pi 3 Model B. This model replaces the Raspberry Pi 2 and is
recommended for use in schools and for general purpose use. For those who
want to embed a Raspberry Pi as part of a larger project or require a Raspberry
Pi that uses low power, then the Raspberry Pi 0 or Model A+ is recommended.
The specifications of the Raspberry Pi 3 Model B are as follows:
1.2-GHz 64-bit quad-core ARMv8 CPU
802.11n wireless LAN
Bluetooth 4.1
Bluetooth Low Energy (BLE) 1-GB RAM
Four USB ports 40 GPiO pins Full HDMI port Ethernet port Combined
3.5-mm audio jack and composite video Camera interface (CSI) Display
interface (DSI)
Micro SD card slot (now push-pull rather than push-push) VideoCore IV 3D
graphics core
With the Raspberry Pi oriented like the one in Figure 1-29, the GPiO pin
specifications are as shown in Figure 1-31.
Figure 1-31 Raspberry Pi 2 and Raspberry Pi 3 GPiO pin numbering.
From within the IDLE program you can also create and save new files. An
alternative to using IDLE to create Python programs is to use a text editor such
as Vim, Nano, or LeafPad and execute the Python program by typing “python
filename.py” on the command line.
Hands-on Example:
A Simple Raspberry Pi “Hello World” Program
with a LED
This section presents a simple hands-on example of creating a blinking LED
using the Raspberry Pi 3.
Parts List
For this hands-on example, you will need
One LED
One breadboard Two wires with one end having a female plug to connect to
the Raspberry Pi’s GPiO pins and the other end having a male plug to connect
to the breadboard
Language: English
CAIRO TO KISUMU
EGYPT—THE SUDAN—KENYA COLONY
ON THE GREAT ASWAN DAM
“The dam serves also as a bridge over the Nile. I crossed on a car, my motive
power being two Arab boys who trotted behind.”
BY
FRANK G. CARPENTER
LITT.D., F.R.G.S.