Tải bản đầy đủ (.pdf) (555 trang)

Arduino applied comprehensive projects for everyday electronics by neil cameron

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (18.72 MB, 555 trang )

TECHNOLOGY IN AC TION™

Arduino
Applied
Comprehensive Projects for
Everyday Electronics

Neil Cameron


Comprehensive Projects for
Everyday Electronics

Neil Cameron

www.electronicbo.com

Arduino Applied


Arduino Applied: Comprehensive Projects for Everyday Electronics
Neil Cameron
Edinburgh, UK
ISBN-13 (pbk): 978-1-4842-3959-9
/>
ISBN-13 (electronic): 978-1-4842-3960-5

Library of Congress Control Number: 2018965611

Copyright © 2019 by Neil Cameron
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or


part of the material is concerned, specifically the rights of translation, reprinting, reuse of
illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way,
and transmission or information storage and retrieval, electronic adaptation, computer software,
or by similar or dissimilar methodology now known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark
symbol with every occurrence of a trademarked name, logo, or image we use the names, logos,
and images only in an editorial fashion and to the benefit of the trademark owner, with no
intention of infringement of the trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if
they are not identified as such, is not to be taken as an expression of opinion as to whether or not
they are subject to proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of
publication, neither the authors nor the editors nor the publisher can accept any legal
responsibility for any errors or omissions that may be made. The publisher makes no warranty,
express or implied, with respect to the material contained herein.
Managing Director, Apress Media LLC: Welmoed Spahr
Acquisitions Editor: Natalie Pao
Development Editor: James Markham
Coordinating Editor: Jessica Vakili
Cover image designed by Freepik (www.freepik.com)
Distributed to the book trade worldwide by Springer Science+Business Media New York,
233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505,
e-mail , or visit www.springeronline.com. Apress Media, LLC is a
California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc
(SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.
For information on translations, please e-mail , or visit ess.
com/rights-permissions.
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook
versions and licenses are also available for most titles. For more information, reference our Print
and eBook Bulk Sales web page at />Any source code or other supplementary material referenced by the author in this book is available

to readers on GitHub via the book’s product page, located at www.apress.com/978-1-4842-3959-9.
For more detailed information, please visit />Printed on acid-free paper


Table of Contents
About the Author�������������������������������������������������������������������������������xiii
About the Technical Reviewer������������������������������������������������������������xv
Preface���������������������������������������������������������������������������������������������xvii

Arduino Uno����������������������������������������������������������������������������������������������������������1
Breadboards����������������������������������������������������������������������������������������������������������3
Arduino IDE Software��������������������������������������������������������������������������������������������4
Arduino IDE Sketch�����������������������������������������������������������������������������������������������5
Run the Blink Sketch���������������������������������������������������������������������������������������������6
Electricity Explained����������������������������������������������������������������������������������������������7
Revise the Blink Sketch����������������������������������������������������������������������������������������8
Pulse Width Modulation��������������������������������������������������������������������������������������12
Opening and Saving Sketches����������������������������������������������������������������������������14
Summary������������������������������������������������������������������������������������������������������������15
Components List�������������������������������������������������������������������������������������������������15

Chapter 2: Switches���������������������������������������������������������������������������17
Tactile Switch������������������������������������������������������������������������������������������������������17
Comparison Operators����������������������������������������������������������������������������������������21
Debouncing a Switch������������������������������������������������������������������������������������������22
Hardware Switch Debounce��������������������������������������������������������������������������������25

iii

www.electronicbo.com


Chapter 1: Introduction������������������������������������������������������������������������1


Table of Contents

Ball Switch����������������������������������������������������������������������������������������������������������27
Summary������������������������������������������������������������������������������������������������������������29
Components List�������������������������������������������������������������������������������������������������29

Chapter 3: Sensors�����������������������������������������������������������������������������31
Temperature Sensor��������������������������������������������������������������������������������������������31
Variables�������������������������������������������������������������������������������������������������������������35
Humidity Sensor��������������������������������������������������������������������������������������������������37
Library Installation����������������������������������������������������������������������������������������������39
Library Installation Method 1�������������������������������������������������������������������������39
Library Installation Method 2�������������������������������������������������������������������������39
Library Installation Method 3�������������������������������������������������������������������������40
Light Dependent Resistor������������������������������������������������������������������������������������42
Light Dependent Resistor and Several LEDs�������������������������������������������������������46
Voltage Divider����������������������������������������������������������������������������������������������������48
Ultrasonic Distance Sensor���������������������������������������������������������������������������������50
Speed of Sound���������������������������������������������������������������������������������������������������56
Hall Effect Sensor�����������������������������������������������������������������������������������������������57
Sound Sensor������������������������������������������������������������������������������������������������������61
Infrared Sensor���������������������������������������������������������������������������������������������������64
Infrared Distance Module������������������������������������������������������������������������������������67
Passive Infrared Sensor��������������������������������������������������������������������������������������69
Accelerometer and Gyroscope����������������������������������������������������������������������������72
Summary������������������������������������������������������������������������������������������������������������77

Components List�������������������������������������������������������������������������������������������������78

iv


Table of Contents

Chapter 4: Liquid Crystal Display�������������������������������������������������������79
Contrast Adjustment with PWM��������������������������������������������������������������������������83
Scrolling Text�������������������������������������������������������������������������������������������������������85
LCD with I2C Bus�������������������������������������������������������������������������������������������������87
I2C with Temperature and Pressure Sensor��������������������������������������������������������88
16×4 LCD Cursor Positioning������������������������������������������������������������������������������93
Display Entered Values on LCD���������������������������������������������������������������������������95
LCD Character Set�����������������������������������������������������������������������������������������������96
Summary����������������������������������������������������������������������������������������������������������100
Components List�����������������������������������������������������������������������������������������������100

Chapter 5: 7-Segment LED Display���������������������������������������������������101
Basic Schematic�����������������������������������������������������������������������������������������������102
PWM and LED Brightness���������������������������������������������������������������������������������105
Shift Register����������������������������������������������������������������������������������������������������107
Shift Register, PWM, and LED Brightness���������������������������������������������������������113
Alphanumeric Characters���������������������������������������������������������������������������������116
Summary����������������������������������������������������������������������������������������������������������118
Components List�����������������������������������������������������������������������������������������������118

Chapter 6: 4-Digit 7-Segment Display����������������������������������������������119
Functions����������������������������������������������������������������������������������������������������������123
One Shift Register���������������������������������������������������������������������������������������������126

Two Shift Registers�������������������������������������������������������������������������������������������131
Summary����������������������������������������������������������������������������������������������������������135
Components List�����������������������������������������������������������������������������������������������136

v

www.electronicbo.com

Additional Characters������������������������������������������������������������������������������������������98


Table of Contents

Chapter 7: 8×8 Dot Matrix Display���������������������������������������������������137
One Shift Register���������������������������������������������������������������������������������������������143
Two Shift Registers�������������������������������������������������������������������������������������������146
Scrolling Text�����������������������������������������������������������������������������������������������������150
Summary����������������������������������������������������������������������������������������������������������156
Components List�����������������������������������������������������������������������������������������������156

Chapter 8: Servo and Stepper Motors����������������������������������������������157
Servo Motors�����������������������������������������������������������������������������������������������������157
Servo Motor and a Potentiometer���������������������������������������������������������������������161
Stepper Motor���������������������������������������������������������������������������������������������������165
Stepper Motor and a Potentiometer������������������������������������������������������������������172
Stepper Motor Gear Ratio���������������������������������������������������������������������������������175
Summary����������������������������������������������������������������������������������������������������������176
Components List�����������������������������������������������������������������������������������������������176

Chapter 9: Rotary Encoder����������������������������������������������������������������177

Rotary Encoder and Stepper Motor�������������������������������������������������������������������182
Summary����������������������������������������������������������������������������������������������������������186
Components List�����������������������������������������������������������������������������������������������187

Chapter 10: Infrared Sensor�������������������������������������������������������������189
Infrared Emitter and Sensor������������������������������������������������������������������������������195
Infrared Emitter and Receiver���������������������������������������������������������������������������197
Summary����������������������������������������������������������������������������������������������������������200
Components List�����������������������������������������������������������������������������������������������201

Chapter 11: Radio Frequency Identification�������������������������������������203
Display Content of MIFARE Classic 1K and 4K��������������������������������������������������205
Mimic RFID and Secure Site�����������������������������������������������������������������������������208
vi


Table of Contents

Master Card Validation��������������������������������������������������������������������������������������211
Read and Write to Classic 1KB Card�����������������������������������������������������������������213
Summary����������������������������������������������������������������������������������������������������������217
Components List�����������������������������������������������������������������������������������������������217

Chapter 12: SD Card Module�������������������������������������������������������������219
Temperature and Light Intensity Logging���������������������������������������������������������220
Date and Time Logging�������������������������������������������������������������������������������������226
Logging Weather Station Data��������������������������������������������������������������������������228
Listing Files on an SD Card�������������������������������������������������������������������������������234
Summary����������������������������������������������������������������������������������������������������������236
Components List�����������������������������������������������������������������������������������������������236


Chapter 13: Screen Displays�������������������������������������������������������������237
TFT LCD Screen�������������������������������������������������������������������������������������������������237
Displaying Images from an SD Card�����������������������������������������������������������������242
Screen, Servo Motor, and Ultrasonic Distance Sensor��������������������������������������243
OLED Display�����������������������������������������������������������������������������������������������������249
Touch Screen����������������������������������������������������������������������������������������������������252
Summary����������������������������������������������������������������������������������������������������������258
Components List�����������������������������������������������������������������������������������������������259

Chapter 14: Sensing Colors��������������������������������������������������������������261
Red Green Blue (RGB) LED��������������������������������������������������������������������������������262
565 Color Format����������������������������������������������������������������������������������������������264
Color-Recognition Sensor���������������������������������������������������������������������������������267
Summary����������������������������������������������������������������������������������������������������������275
Components List�����������������������������������������������������������������������������������������������275
vii

www.electronicbo.com

Increment File Name for Data Logging�������������������������������������������������������������232


Table of Contents

Chapter 15: Camera��������������������������������������������������������������������������277
Camera Image Capture Setup���������������������������������������������������������������������������281
Capturing Camera Images��������������������������������������������������������������������������������285
Summary����������������������������������������������������������������������������������������������������������288
Components List�����������������������������������������������������������������������������������������������288


Chapter 16: Bluetooth Communication���������������������������������������������289
Bluetooth Terminal HC-05 App��������������������������������������������������������������������������292
ArduDroid App���������������������������������������������������������������������������������������������������295
Message Scrolling with MAX7219 Dot Matrix Module��������������������������������������300
MAX7219 and Bluetooth Terminal HC-05 App���������������������������������������������������302
Message Speed and Potentiometer������������������������������������������������������������������306
MAX7219 and ArduDroid App����������������������������������������������������������������������������307
Summary����������������������������������������������������������������������������������������������������������310
Components List�����������������������������������������������������������������������������������������������310

Chapter 17: Wireless Communication����������������������������������������������311
Transmit or Receive������������������������������������������������������������������������������������������315
Transmit and Receive����������������������������������������������������������������������������������������317
Summary����������������������������������������������������������������������������������������������������������322
Components List�����������������������������������������������������������������������������������������������323

Chapter 18: Build Arduino����������������������������������������������������������������325
ATmega328P Pin Layout�����������������������������������������������������������������������������������326
Building an Arduino�������������������������������������������������������������������������������������������328
Installing the Bootloader�����������������������������������������������������������������������������������332
Summary����������������������������������������������������������������������������������������������������������336
Components List�����������������������������������������������������������������������������������������������337

viii


Table of Contents

Chapter 19: Global Navigation Satellite System�������������������������������339

GNSS Messages on Serial Monitor�������������������������������������������������������������������339
u-blox u-center�������������������������������������������������������������������������������������������������341
Arduino and GNSS���������������������������������������������������������������������������������������������343
GNSS Data Logging to SD Card�������������������������������������������������������������������������357
GNSS and ST7735 Screen���������������������������������������������������������������������������������360
Displaying GNSS Data���������������������������������������������������������������������������������������368
Summary����������������������������������������������������������������������������������������������������������369

Chapter 20: Interrupts and Timed Events�����������������������������������������371
Interrupts����������������������������������������������������������������������������������������������������������371
Types of Interrupt����������������������������������������������������������������������������������������������376
Additional Interrupt Pins�����������������������������������������������������������������������������������379
Interrupts and Rotary Encoder��������������������������������������������������������������������������380
Timed Events: delay( )���������������������������������������������������������������������������������������384
Timed Events: millis( )���������������������������������������������������������������������������������������384
Timed Events: Timer1����������������������������������������������������������������������������������������387
Timer Register Manipulation�����������������������������������������������������������������������������390
Summary����������������������������������������������������������������������������������������������������������395
Components List�����������������������������������������������������������������������������������������������395

Chapter 21: Power Saving����������������������������������������������������������������397
avr/sleep Module����������������������������������������������������������������������������������������������402
LowPower Library���������������������������������������������������������������������������������������������405
Power Down and an Infrared Sensor����������������������������������������������������������������406
Summary����������������������������������������������������������������������������������������������������������410
Components List�����������������������������������������������������������������������������������������������410

ix

www.electronicbo.com


Components List�����������������������������������������������������������������������������������������������369


Table of Contents

Chapter 22: Sound and Square Waves����������������������������������������������411
Piezo Transducer and Buzzer����������������������������������������������������������������������������416
Musical Notes���������������������������������������������������������������������������������������������������416
Sensor and Sound���������������������������������������������������������������������������������������������420
Generating Square Waves���������������������������������������������������������������������������������425
Square Wave and Servo Motor��������������������������������������������������������������������������431
Summary����������������������������������������������������������������������������������������������������������432
Components List�����������������������������������������������������������������������������������������������432

Chapter 23: DC Motors����������������������������������������������������������������������433
Motor Control Set in the Sketch������������������������������������������������������������������������438
Motor Speed������������������������������������������������������������������������������������������������������441
Motor Control with Infrared Remote Control�����������������������������������������������������444
Motor Control with Wireless Communication����������������������������������������������������445
Motor Control with Accelerometer��������������������������������������������������������������������452
Motor Control with Photoelectric Encoder��������������������������������������������������������457
Summary����������������������������������������������������������������������������������������������������������465
Components List�����������������������������������������������������������������������������������������������465

Chapter 24: Robot Car����������������������������������������������������������������������467
PID Controller����������������������������������������������������������������������������������������������������475
Balancing Robot������������������������������������������������������������������������������������������������481
Determining PID Coefficients����������������������������������������������������������������������������483
Circular Buffer���������������������������������������������������������������������������������������������������485

Quaternion Measurements��������������������������������������������������������������������������������489
Summary����������������������������������������������������������������������������������������������������������496
Components List�����������������������������������������������������������������������������������������������497

x


Table of Contents

Chapter 25: Wi-Fi Communication����������������������������������������������������499
NodeMCU ESP8266�������������������������������������������������������������������������������������������499
WeMos D1 Mini�������������������������������������������������������������������������������������������������502
Wi-Fi and Web Server���������������������������������������������������������������������������������������504
Wi-Fi and HTML�������������������������������������������������������������������������������������������������510
Wi-Fi and Internet Access���������������������������������������������������������������������������������519
Summary����������������������������������������������������������������������������������������������������������530
Components List�����������������������������������������������������������������������������������������������531

Libraries������������������������������������������������������������������������������������������������������������534
Quaternion Measurements��������������������������������������������������������������������������������537
Who’s Who in Electronics����������������������������������������������������������������������������������541
Sources of Electronic Components�������������������������������������������������������������������542

Index�������������������������������������������������������������������������������������������������545

xi

www.electronicbo.com

Appendix: Resistor Banding�������������������������������������������������������������533



About the Author
Neil Cameron was a research scientist in quantitative genetics at Roslin
Institute (of “Dolly the sheep” fame) with expertise in data analysis and
computer programming. Neil has taught at the University of Edinburgh
and Cornell University. He has a deep interest in electronics and “how
things work,” with a focus on programming the Arduino and its application
on a range of comprehensive projects for everyday electronics, which
inspired him to write this book.

xiii


Fabio Claudio Ferracchiati is a senior consultant and a senior analyst/
developer using Microsoft technologies. He works at BluArancio S.p.A
(www.bluarancio.com) as senior analyst/developer and Microsoft
Dynamics CRM Specialist. He is a Microsoft Certified Solution Developer
for .NET, a Microsoft Certified Application Developer for .NET, a Microsoft
Certified Professional, and a prolific author and technical reviewer.
Over the past ten years, he’s written articles for Italian and international
magazines, and co-authored more than ten books on a variety of
computer topics.

xv

www.electronicbo.com

About the Technical Reviewer



Preface
Microcontrollers are incorporated in car control systems, domestic
appliances, office machines, mobile phones, medical implants, remote
controls, and the list goes on. The Arduino Uno is a microcontroller board
that can be easily programmed and used to build projects. The objective
of this book is to provide information to use the Arduino Uno in a range
of applications, from blinking an LED to a motion sensor alarm, to route
mapping with a mobile GPS system, to uploading information to the
Internet. Prior knowledge of electronics is not required, as each topic is
described and illustrated with examples using the Arduino Uno.
The book covers a comprehensive range of topics. In Chapters 1–3,
the Arduino Uno and the Arduino programming environment are set
up, and several sensors are described with practical examples to provide
the basis for subsequent projects. Information display with the Arduino
Uno using liquid crystal, LED, and dot matrix displays are described in
Chapters 4–7. Several projects are developed with servo and stepper
motors, infrared control, RFID, and SD card data logging in Chapters 8–12.
Sensing and displaying color is outlined in Chapters 13–14, and recording
images in Chapter 15. Bluetooth, wireless, and Wi-Fi communication
systems are described in Chapters 16, 17 and 25, with practical examples
of message scrolling, servo motor control, and web-based information
display projects, respectively. The Arduino Uno is deconstructed to the
microcontroller for use in a mobile GPS system, with timed events and
power-saving methods in Chapters 18–21. Electronic sound projects are
outlined in Chapter 22. An obstacle-avoiding robot car and a balancing
robot are described in Chapters 23 and 24, with the robot car controlled by
systems described in earlier chapters.

xvii



Projects covered in the book include and extend those in Arduino
Uno starter kits to increase knowledge of microcontrollers in electronic
applications. Many of the projects are practically orientated, such as
information displays, GPS tracking, RFID entry systems, motion detector
alarms, and robots. Building projects helps you understand how many
electronic applications function in everyday life. Examples include flashing
numbers on a screen, a scrolling message in the train station, electronic
tags on items in a shop or books in the library, a desktop weather station,
Bluetooth communication with a mobile phone, digital sound systems,
and an obstacle-avoiding robot vacuum cleaner.
Each example in the book is accompanied by code and a description
of that code, which helps you learn how to program a microcontroller and
a computer, which is a highly valuable skill. The Arduino programming
language is C, which is widely used. Learning to program an Arduino
provides the framework for other computer programming languages.
Throughout the book, schematic diagrams were produced with Fritzing
software (www.fritzing.org), with an emphasis on maximizing the clarity
of component layout and minimizing overlapping connections. The
authors of the libraries used in the book are identified in each chapter,
with library details covered in the appendix. There are several approaches
to structuring sketches, and the approach taken in the book is to declare
variables at the start of the sketch, rather than throughout the sketch.
All the code used in the book is available to download from github.
com/Apress/arduino-applied. The Arduino programming environment
and libraries are constantly being updated, so information on the
consequences of those updates on the content of the book is also available
at github.com/Apress/arduino-applied.
Many chapters of the book are stand-alone, so that you can delve

into a section of the book rather than having to start from the beginning,
while several chapters utilize information from earlier chapters to build
a project. You learn how to break down a complex project into smaller

xviii

www.electronicbo.com

Preface


Preface

projects, just as each chapter addresses a different topic, to then be able to
build and enhance the initial project.
If you bought, or are thinking about buying, an Arduino Uno starter
kit that contains a few LEDs, a variety of sensors, with some switches and
resistors, then this book is for you. If you want to build electronics projects
with a microcontroller, then the comprehensive range of topics covered in
the book provides the detailed instructions to get started.

xix


CHAPTER 1

The Arduino Uno provides the framework to learn about electronics, and to
understand and build electronic devices. The Arduino Uno can monitor an
environment with sensors, drive LED message boards, generate sound and
light patterns, take and display digital photos, communicate by Bluetooth

or wirelessly with other electronic devices, communicate by Wi-­Fi to the
Internet, and record data on the route, speed, and altitude of a trip with GPS.

Arduino Uno
The Arduino Uno R3 (see Figure 1-1) contains the ATmega328P
microcontroller to carry out programmed instructions and memory
to store data. The Arduino is powered through a DC input or a USB
connection, which is also used to upload instructions and communicate
with a computer or laptop. An ATmega16U2 chip manages USB (Universal
Serial Bus) to serial communication.
The power pins allow 5V (5 volts) or 3.3V and ground (GND) to
connect other devices. Pins 0 and 1 are for transmitting and receiving
serial data from other devices. Pins 2 to 13 are digital input and output,
which input or output 5V for a digital one or 0V for a digital zero. Several
output pins vary the time that a pin state is 5V to emulate voltages between
0V and 5V. The analog pins, A0 to A5, measure voltages between 0V and
5V and convert analog signals to digital values (ADC). Pins A4 and A5

© Neil Cameron 2019
N. Cameron, Arduino Applied, />
1

www.electronicbo.com

Introduction


Chapter 1

Introduction


can also communicate with other devices, as can pins 10 to 13, but using
different communication systems, I2C and SPI respectively, than the
USB connection. Three LEDs (light-emitting diode) indicate power (ON),
transmitting (TX), and receiving (RX), with a fourth LED connected to pin 13.
The Reset button is used to restart the microcontroller.
The functionality of the Arduino Uno enables a comprehensive range
of projects to be developed, which are described throughout the book.
Several of the terms—such as ADC, I2C, and SPI—may mean little to you
just now, but they are explained in the relevant chapters.

Figure 1-1.  Arduino Uno

2


Chapter 1

Introduction

Breadboards

www.electronicbo.com

The solderless breadboard contains columns of connected sockets for
positioning electronic components to create a circuit and for connecting to
the Arduino (see Figure 1-2). The two rows along the length (left to right)
of the breadboard are used to connect to power (red) or ground (blue)
lines in a circuit. Holes in each short column (green) of the breadboard
are connected together, but the columns are not connected, so that two

components each with one “leg” in the same green column are connected
together. The middle area in the breadboard separates the breadboard into
two unconnected halves. Breadboards come in a variety of sizes.

Figure 1-2.  Breadboard
The term breadboard originates from radio amateurs attaching
fixing points to a wooden breadboard and then connecting electronic
components to the fixing points.
For example, Figure 1-3 shows a circuit with an LED, a 100Ω resistor,
and a 3V battery. The positive or red terminal of the 3V battery is
connected to the long leg of the LED, as the relevant component legs are
in the same short column. Likewise, the short leg of the LED is connected
to the “top” end of the 100Ω resistor, but not to the “bottom” end of the

3


Chapter 1

Introduction

resistor due to the separating middle area of the breadboard. To complete
the circuit, a black wire connects the negative or black terminal of the 3V
battery to the “bottom” end of the resistor.

Figure 1-3.  LED and resistor circuit

Arduino IDE Software
The Arduino IDE (interactive development environment) software
is downloaded from www.arduino.cc/en/Main/Software, with the

downloaded arduino-version number-windows.exe file saved to the
desktop. The .exe file is double-clicked to start the installation.
The Arduino IDE program files are stored in C: ➤ Program Files (x86)
➤ Arduino, which includes example sketches located in C: ➤ Program
Files (x86) ➤ Arduino ➤ examples. Each example sketch is accompanied
by a text file outlining the objective of the sketch, the breadboard layout of
the components, and a circuit diagram.
The Arduino IDE is used to write, compile, and upload files to the
microcontroller. A file containing Arduino code is called a sketch. Within the
Arduino IDE, clicking one of the five IDE symbols
provides quick access to compile a sketch, to compile and upload a sketch,
to open a blank sketch, to open an existing sketch from a list of all sketches,

4


Chapter 1

Introduction

and to save the current sketch. The Open an existing sketch option
does not scroll the complete list of sketches, so use File ➤ Sketchbook
instead. Some useful options from the drop-down menu are given in
Table 1-1.

Options

Description

File ➤ Open Recent


A list of recently accessed sketches

File ➤ Examples

Arduino IDE built-in sketches

Edit ➤ Find

Find and replace text in a sketch

Sketch ➤ Include Library

Arduino and contributed libraries

Tools ➤ Serial Monitor

Displays serial data to serial monitor

Tools ➤ Serial Plotter

Graphic display of serial data

Tools ➤ Board

Description of the microcontroller

www.electronicbo.com

Table 1-1.  Drop-down Menu Options of the Arduino IDE


for example Arduino/Genuino Uno
Tools ➤ Port

Detail of serial port,
for example COM3 Arduino/Genuino Uno

File ➤ Open Recent

List of recently accessed sketches

Arduino IDE Sketch
An Arduino IDE sketch consists of three parts: variable definition, the
void setup(), and the void loop() functions. The first part includes
defining which Arduino pins are connected to sensors, LEDs, or devices,
and declaring the values of variables. For example, the int LEDpin = 9
instruction defines a variable, named LEDpin, with the integer value of 9.
The void setup() function implements definitions in the first part of the
5


Chapter 1

Introduction

sketch and only runs once. For example, the pinMode(LEDpin, OUTPUT)
instruction defines the Arduino pin 9 as an output pin, rather than an
input pin by default, since LEDpin has the value 9.
The void loop() function runs continuously and implements the
sketch instructions. For example, a sketch may turn on and off an LED at

given times.
Declaring variables in the first part of the sketch makes it easier to
update the variable once at the start of the sketch, rather than having to
check through the sketch and update variables throughout the sketch.
Comments are prefaced by //, such as // Set LED to pin 9, and are
not implemented by the microcontroller. With a couple of exceptions, all
instruction lines end with a semicolon.

Run the Blink Sketch
Follow these steps to run the blink sketch.
1. Connect the Arduino to a computer or laptop with
the USB-to-serial cable.
2. In Arduino IDE, select File ➤ Examples ➤ 01. Basics ➤ Blink.
3. Click the Compile and Upload,

, button.

The built-in LED on the Arduino will now flash every second. Welcome
to Arduino !
error message indicates that the serial
The
port should be updated. Select Tools ➤ Port and choose the appropriate
port (for example, COM3 or COM4) for the Arduino. Go to step 3.
The error message

indicates

that the description of the microcontroller should be updated. Select
Tools ➤ Board and choose the relevant board (for example, Arduino/
Genuino Uno). Go to step 3.

6


Chapter 1

Introduction

Electricity Explained

www.electronicbo.com

An understanding of electricity is helpful before progressing further.
All materials are made of atoms, which consist of protons, neutrons,
and electrons. Electrons have a negative charge and can move from one
atom to another. Electricity is the movement of electrons between atoms,
or rather the flow of an electrical charge.
A simple example of an electrical charge is rubbing a cloth over an
inflated balloon. Electrons are rubbed off the cloth and onto the balloon,
making the balloon negatively charged. If the balloon is now placed near
an object, then the balloon “sticks” to the object. The negative charge of
the balloon repels the negatively charged electrons of the object, leaving
an excess of positive charge next to the balloon. Since positive and
negative charges attract, then the balloon is attracted to the object.
The effect of moving an electric charge from one object to another has
been known for centuries. More than two-and-a-half-thousand years ago,
the Greeks knew that rubbed amber, which is fossilized tree resin, could
attract light objects, such as hair. The word electric derives from the Greek
word for amber, elektron.
A discharging battery is a source of electrons, and the electrons
move from the negative terminal, the anode, to the positive terminal, the

cathode. The words anode and cathode are derived from the Greek words
anodos and kathodos, so cathode is abbreviated as K. Although electrons
flow from anode to cathode, the conventional current flows from cathode
to anode, or from positive to negative.
Describing electricity uses the terms charge, voltage, current, and
resistance. The analogy of water flowing from a reservoir through a pipe
can be used to envisage some of the electrical terms (see Table 1-2).

7


Chapter 1

Introduction

Table 1-2.  Electrical Parameter and Water Analogy
Electrical Parameter

Water Analogy

Electrical charge (coulombs, C)

Amount of water in the reservoir

Voltage (volts, V)

Water pressure at the reservoir end of the pipe

Current (amperes or amps, A)


Rate of water flow

Resistance (ohms, Ω)

Inverse of pipe width
(narrow pipe ⇒ high resistance)

The relationship between voltage (V), current (I), and resistance (R) is
V = I × R, which is Ohm’s law.
Charge is measured in amp-hours (Ah), which is the charge
transferred by a current of one amp for one hour. The length of time that a
battery, such as a nickel metal hydride (NiMH) AA battery with a charge of
2400mAh, can supply a given current depends on the size of the current.
For example, with discharge rates of 2400, 4800, or 7200mAh, the battery
would last 60, 30, or 20 minutes.
Electrical power, measured in watts (W), is the rate that energy is
transferred in unit time, equal to the product of voltage and current.

Revise the Blink Sketch
The blink sketch can be changed to make a separate
LED blink rather than the LED on the Arduino. The
Arduino supplies a regulated 5V output from the pin
marked 5V, but a resistor is required to ensure that the
current does not exceed the LED’s maximum permitted
current of 20mA. Without the resistor, the high current
would damage the LED.
Using Ohm’s law, which states voltage equals the product of current
and resistance, or V = I × R, the value of the resistor (R) can be determined,
8



×