Mastering Arduino - Jon Hoffman - E-Book

Mastering Arduino E-Book

Jon Hoffman

0,0
31,19 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

Mastering Arduino is an all-in-one guide to getting the most out of your Arduino. This practical, no-nonsense guide teaches you all of the electronics and programming skills that you need to create advanced Arduino projects. This book is packed full of real-world projects for you to practice on, bringing all of the knowledge in the book together and giving you the skills to build your own robot from the examples in this book. The final two chapters discuss wireless technologies and how they can be used in your projects.

The book begins with the basics of electronics, making sure that you understand components, circuits, and prototyping before moving on. It then performs the same function for code, getting you into the Arduino IDE and showing you how to connect the Arduino to a computer and run simple projects on your Arduino.

Once the basics are out of the way, the next 10 chapters of the book focus on small projects centered around particular components, such as LCD displays, stepper motors, or voice synthesizers. Each of these chapters will get you familiar with the technology involved, how to build with it, how to program it, and how it can be used in your own projects.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB
MOBI

Seitenzahl: 377

Veröffentlichungsjahr: 2018

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Mastering Arduino

 

 

 

 

 

 

 

A project-based approach to electronics, circuits, and programming

 

 

 

 

 

 

 

 

 

 

 

 

Jon Hoffman

 

 

 

 

 

 

 

 

 

 

BIRMINGHAM - MUMBAI

Mastering Arduino

 

 

 

Copyright © 2018 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

 

 

 

Acquisition Editor: Ben Renow-Clarke, Suresh JainProject Editor: Radhika AtitkarContent Development Editor: Alex SorentinhoTechnical Editor:   Gaurav GavasProofreader: Safis EditingIndexer: Tejal Daruwale SoniGraphics: Tom ScariaProduction Coordinator: Sandip Tadge

 

 

 

First published: September 2018

Production reference: 1270918

 

 

 

Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK.

ISBN 978-1-78883-058-4

www.packt.com

I would like to thank my wife, Kim; without her support none of my books would have been possible. I would also like to thank my two daughters who have both been my inspiration and driving force since the days they were born.

-Jon Hoffman

Packt Upsell

 
mapt.io

Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website.

Why subscribe?

Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals

Improve your learning with Skill Plans built especially for you

Get a free eBook or video every month

Mapt is fully searchable

Copy and paste, print, and bookmark content

packt.com

Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.packt.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.

At www.packt.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.

Contributors

About the author

Jon Hoffman has over 25 years of experience in the field of Information Technology. Over those years, Jon worked in the areas of System Administration, Network Administration, Network Security, Application Development, and Architecture. Currently, Jon works as an Enterprise Software Manager for Syntech Systems.

Jon has developed extensively for the iOS platform since 2008. This includes several apps that he has published in App Store, apps that he wrote for third parties and numerous enterprise applications. What really drives Jon is the challenges in the Information Technology field, and there is nothing more exciting to him than overcoming a challenge.

Some of Jon’s other interests are baseball and basketball. Jon also really enjoys TaeKwonDo, where he and his oldest daughter earned their Black Belts together early in 2014. Kim (his wife) earned her Black Belt at the end of 2014.

 

About the reviewers

Dr. Pratik Desai is a computer scientist and an engineering leader. His fields of expertise include the Internet of Things, Artificial Intelligence, and Connected Cars. He is a pioneer researcher in IoT and the published author of Python Programming for Arduino. In his spare time, Pratik likes tinkering with sensors and working on Python projects.

Ejike is a developer with a serious love of sharing his skills and knowledge with his fellow developers and beginners in the industry. He has spent his few years as a lead instructor in computer programming and has enjoyed the most part of his life doing live trainings with over hundreds of students. Ejike derives joy in building the next generation of software and hardware developers who do amazing things in the industry today. He led the team of developers who participated in Forbes' Under 30 Change the World Challenge, judged by a panel of professionals at the Wharton school of the university of Pennsylvania in 2015.

His love for computer programming in his early stage in life got Ejike into Java, C, C#, C++, Python, MikroC, Arduino, and lots of other programming languages and embedded system designs. He began to think that he is crazy until I started developing advance projects for clients and getting amazing job offers from reputable companies across America, Asia, and Africa.

The worst part of Ejike’s life as a software developer is seeing himself coding always in his dreams.

The best part of his life as a developer is NOT building advanced and complicated projects with different programming languages combine together, but, building the next generation of developers and seeing them excel in the industry like him and other senior developers around him. He is always happy each time his students get across to him to tell him how they successfully got employed by one reputable firm or the other, and how they pick up fast in their respective companies to become senior developers with mouth-watering salaries!

Ejike’s courses are one of the most comprehensive ones in the market today!

 

 

 

 

 

Packt is searching for authors like you

If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.

Table of Contents

Title Page

Copyright and Credits

Mastering Arduino

Dedication

Packt Upsell

Why subscribe?

packt.com

Contributors

About the author

About the reviewers

Packt is searching for authors like you

Preface

Who this book is for

What this book covers

To get the most out of this book

Download the example code files

Download the color images

Conventions used

Get in touch

Reviews

The Arduino

History of the Arduino

What is the Arduino?

Touring the Arduino UNO R3

Powering the Arduino

Using the VIN/GND pins to power the Arduino

Using the DC supply input to power the Arduino

Using the USB connector to power the Arduino

Arduino shields

Arduino pin

Digital pins

Analog input pins

PWM pins

Power pins

Serial pins

SPI pins

Different Arduino boards

Arduino Micro

Arduino Mega 2560

Lilypad

Arduino Nano

Generic boards

Summary

Basic Electronics

Electronic building block

Power supply

Input

Output

Control circuit

Multimeter

Electronic components

Resistor

Potentiometer

Switches

Transistor

LED

Capacitor

Integrated circuit

What is electricity?

Current

Voltage

Resistance

Ohm's law

What is power?

Resistor color codes

Summary

Circuit Diagrams

What is a circuit?

Fritzing

Fritzing diagrams

Schematic diagrams

Parallel and series circuits

Series circuits

Resistance

Voltage

Current

Parallel circuits

Resistance

Voltage

Current

Voltage drop

Light up LED

Summary

Basic Prototyping

Setting up a work area

Using a solderless breadboard

Dupont (jumper) cables

Prototyping

Four building blocks of an electronic project

Creating a diagram

Building the prototype

First prototype

Summary

Arduino IDE

Arduino Sketch

Arduino IDE

Exploring the IDE

Configuring the Arduino within the IDE

Arduino web editor

Exploring

Configuring the Arduino within the IDE

Examples

Arduino libraries

Serial monitor

Hello World

Echo

Summary

Programming the Arduino - The Basics

Curly brackets

Semicolons

Comments

Variables

Data types

Boolean

Byte

Integer

Long

Double and float

Character

Arrays

Character arrays

Constants

Arithmetic functions

Comparison operators

Logical operators

Casting

Decision making

Looping

Functions

Summary

Programming the Arduino - Beyond the Basics

Setting digital pin mode

Digital write

Digital read

Analog write

Analog read

Structures

Unions

Adding tabs

Working with tabs

Object-oriented programming

String library

Summary

Motion Sensor

Introduction

Components needed

Circuit diagrams

Code

Running the project

Challenge

Summary

Environment Sensors

Introduction

Components needed

Circuit diagrams

Code

Running the project

Challenge

Summary

Obstacle Avoidance and Collision Detection

Introduction

Crash sensor

Obstacle avoidance sensor

Ultrasonic range finder

Components needed

Circuit diagrams

Code

Running the project

Challenge

Summary

Fun with Lights

Introduction

Components needed

Circuit diagrams

Code

RGB LED

NeoPixel shield

Running the project

Challenge

Summary

Fun with Sound

Introduction

Components needed

Circuit diagrams

Code

Using the tone function

Playing a ringtone in the RTTTL format

Challenge

Summary

Using LCD Displays

Introduction

Components needed

Circuit diagrams

Code

Drawing a line

Displaying text

Rotating text

Basic shapes

Filled shape

Rectangle

Filled rectangle

Rounded rectangle

Filled rounded rectangle

Challenge

Summary

Speech Recognition and Voice Synthesizing

Introduction

Components needed

Circuit diagrams

Code

Running the project

Challenge

Summary

DC Motors and Motor Controllers

Introduction

Components needed

Circuit diagrams

Code

Running the project

Challenge

Summary

Servo Motors

Introduction

Components needed

Circuit diagrams

Code

Challenge

Summary

Using a Relay

Introduction

Components needed

Circuit diagrams

Code

Challenge

Summary

Remotely Controlling the Arduino

Introduction

Components needed

Circuit diagrams

Code

Challenge

Summary

Creating a Robot

Introduction

Chassis and movement

Motors and power

Autonomous robot–obstacle avoidance and collision detection

Remotely controlling a robot

User feedback

Making things rotate

Non-robotic projects

Weather station

Smart thermostat

Proximity sensor

Challenge

Summary

Bluetooth LE

Introduction

Bluetooth LE radio

Network topology

Bluetooth LE broadcasting

Bluetooth LE connections

Bluetooth LE profiles

Generic access profile (GAP)

Generic attribute (GATT) profile

HM-10 Bluetooth module

Components needed

Circuit diagrams

Project 1 – serial communication

Test command

Query software version

Restore factory default

Restart module

Query MAC (Media Access Control) address

Set name

Query name

Set the advertising interval

Query the advertising interval

Set advertising type

Query advertising type

Set baud rate

Query baud rate

Set characteristic id

Set service id

Query service id

Set role

Query role

Clear last connected device

Try to connect to last connected device

Try to connect to an address

Set pin code

Query pin code

Set module power

Query module power

Set bond mode

Query bond mode

Set notify information

Query notify information

Project 2 – controlling LED

Project 3 – environmental sensor

What is new with Bluetooth 4.1, 4.2 and 5.0?

Bluetooth 4.1

Bluetooth 4.2

Bluetooth 5.0

Bluetooth mesh

Challenge

Summary

Bluetooth Classic

Introduction

Bluetooth radio

Network topology

Components needed

Circuit diagrams

Project 1 – configuring the Bluetooth modules

Test command

Reset command

Query firmware

Restore defaults

Query module address

Set/Query module mode

Set/Query UART parameters

Set/Query connection mode

Set/Query bind address

Project 2 – serial connection, sending data

Project 3 – joystick remote control

Summary

Another Book You May Enjoy

Leave a review - let other readers know what you think

Preface

Mastering Arduino is an all-in-one guide to getting the most out of your Arduino. This practical, no-nonsense guide teaches you all of the electronics and programming skills that you need, to create advanced Arduino projects. This book is packed full of real-world projects for you to practice on, bringing all of the knowledge in the book together and giving you the skills to build your own robot from the examples in this book. The final two chapters discuss wireless technologies and how they can be used in your projects. The book begins with the basics of electronics, making sure that you understand components, circuits, and prototyping before moving on. It then performs the same function for code, getting you into the Arduino IDE and showing you how to connect the Arduino to a computer and run simple projects on your Arduino.Once the basics are out of the way, the next 10 chapters of the book focus on small projects centered around particular components, such as LCD displays, stepper motors, or voice synthesizers. Each of these chapters will get you familiar with the technology involved, how to build with it, how to program it, and how it can be used in your own projects.

Who this book is for

Mastering Arduino is for anybody who wants to experiment with an Arduino board and build simple projects. No prior knowledge is required, as the fundamentals of electronics and coding are covered in this book.

What this book covers

Chapter 1, The Arduino, introduces the reader to the Arduino by giving a brief history of the Arduino and going over the different versions. We also look at the headers and what the different pins are used for.

Chapter 2, Basic Electronics, introduces the reader to the basics of electricity and electronics. We also introduce the reader to basic electronic components and discuss what they can be used for.  

Chapter 3, Circuit Diagrams, introduces the reader to circuits and circuit design. It also introduces the reader to circuit properties such as voltage, current, and resistance and how they affect the circuit. We also introduce the reader to the Fritzing tool that can be used for circuit design.

Chapter 4, Basic Prototyping, introduces the reader to prototyping and shows them how they can create basic prototypes of their projects. We also discuss the tools needed to create prototypes with the Arduino.

Chapter 5, Arduino IDE, introduces the reader to the Arduino IDE and the Arduino Web Editor. We will show the reader how they can use the both the IDE and the Web Editor to program the Arduino.

Chapter 6, Programming Arduino - The Basics, gives the reader an introduction to the language used to program the Arduino and the layout of the files.

Chapter 7, Programming Arduino - Beyond the Basics, shows the reader how they can interact with the pin headers of the Arduino. We also go over more advance topics such as structures, unions, and classes.

Chapter 8, Motion Sensor, is the first “project” chapter. We show how to use the HC-SR01 motion sensor with the Arduino. 

Chapter 9, Environment Sensors, helps the reader build a basic weather station using a temperature and humidity sensor and a rain sensor.

Chapter 10, Obstacle Avoidance and Collision Detection, teaches the reader how to use crash sensors, infrared obstacle avoidance sensors, and ultrasonic range finders to sense objects nearby.

Chapter 11, Fun with Lights, teaches the reader how to use the Arduino to control RGB Leds and NeoPixels.

Chapter 12, Fun with Sound, shows how the reader can use piezo buzzers and an 8-ohm speaker with the Arduino tone library to create sounds and music. It also teaches the reader learn how to play RTTTL (Ring Tone Text Transfer Language) ring tones with the Arduino.

Chapter 13, Using LCD Display, teaches the reader how to connect and use the Nokia 5110 LCD display with the Arduino.

Chapter 14, Speech Recognition and Voice Synthesizing, teaches the reader how to use the MOVI speech recognition and voice synthesizing shield to create a voice activated temperature device that will tell them the temperature.

Chapter 15, DC Motors and Motor Controllers, teaches the reader how to use DC motors with the L298 motor controller and L293D h-bridge motor driver.

Chapter 16, Servo Motors, teaches the reader how to use servo motors with an external power supply to create a robotic claw.

Chapter 17, Using a Relay, shows the reader how a relay can be used to allow the Arduino to control an AC powered device.

Chapter 18, Remotely Controlling the Arduino, shows the reader how to use both a RF (radio frequency) and an IR (infrared) remote controls to control the Arduino.

Chapter 19, Creating a Robot, shows the reader how to take the knowledge they gained in previous chapter and use it to design a robot. We do not actually design a robot, we show how the parts can be used so that the reader can design their own creation.

Chapter 20, Bluetooth LE, teaches the reader about Bluetooth LE and how to use the HM-10 Bluetooth LE radio module with the Arduino.

Chapter 21, Bluetooth Classic, teaches the reader about Bluetooth Classic and how to use the HC-05 Bluetooth radio module with the Arduino.

To get the most out of this book

This book assumes no previous knowledge of electronics, programming, or the Arduino. Everything that is needed is covered in this book.

Download the example code files

You can download the example code files for this book from your account at www.packt.com. If you purchased this book elsewhere, you can visit www.packt.com/support and register to have the files emailed directly to you.

You can download the code files by following these steps:

Log in or register at

www.packt.com

.

Select the

SUPPORT

tab.

Click on

Code Downloads & Errata

.

Enter the name of the book in the

Search

box and follow the onscreen instructions.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

WinRAR/7-Zip for Windows

Zipeg/iZip/UnRarX for Mac

7-Zip/PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Mastering-Arduino. In case there's an update to the code, it will be updated on the existing GitHub repository.

We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Download the color images

We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://www.packtpub.com/sites/default/files/downloads/9781788830584_ColorImages.pdf.

Conventions used

There are a number of text conventions used throughout this book.

CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "Mount the downloaded WebStorm-10*.dmg disk image file as another disk in your system."

A block of code is set as follows:

#define BUTTON_ONE 12 #define LED_ONE 11 void setup() { pinMode(BUTTON_ONE, INPUT); pinMode(LED_ONE, OUTPUT); }

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

display.clearDisplay();

display.drawPixel(10, 10, BLACK);

display.display();

Any command-line input or output is written as follows:

at+nameBuddy

at+name?

Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "Select System info from the Administration panel."

Warnings or important notes appear like this.
Tips and tricks appear like this.

Get in touch

Feedback from our readers is always welcome.

General feedback: Email [email protected] and mention the book title in the subject of your message. If you have questions about any aspect of this book, please email us at [email protected].

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packt.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.

Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Reviews

Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!

For more information about Packt, please visit packt.com.

The Arduino

Have you ever looked at a gadget and wondered how it worked? Do you want to create your own cool and exciting electronics project but do not know how to get started? Your decision to start reading this book is an excellent first step.

In this book, we will teach you everything you need to get started with the Arduino. Everything from basic electronics and prototyping to setting up the Arduino development environment and programming is covered. This book also has numerous sample projects to show you how to use this knowledge with real-world examples. Before we get to all that fun stuff, let's a look at the Arduino itself and get familiar with it.

In this chapter, you will learn:

What the Arduino boards are

How to power the Arduino boards

What Arduino shields are

What the pins on the Arduino boards do

Learn about generic and compatible Arduino boards

Arduino is a company, development boards, community and a way of thinking. As you will soon find out, Arduino is also the name of a bar in northern Italy. While we could begin this book by writing several chapters on everything that the Arduino name stands for, that is not what this book is about. This book is about teaching you how to use the Arduino development board to build fun and exciting projects. Anywhere in this book, unless noted otherwise, when we refer to the Arduino we will be referring to the Arduino development boards. However, we do believe to really understand the Arduino board, you should at least have a basic understanding of its history; therefore, we will start off by giving you a brief history of the board and its predecessors.

History of the Arduino

In 2003 Hernando Barragan started working on a project called Wiring for his master's thesis at the Interaction Design Institute Ivrea (IDII) in Italy. At that time students used a microcontroller board that cost USD $100 and needed additional hardware and software to use. Massimo Banzi and Casey Reas, who is known for work on the Processing language, were supervisors for his thesis. The name was Wiring: Prototyping Physical Interaction Design.

You can view the thesis here: http://people.interactionivrea.org/h.barragan/thesis/thesis_low_res.pdf.

The purpose of the thesis was to create a low-cost and easy-to-use tool so non-engineers could create digital projects. To do this, Hernando wanted to abstract away the complicated details of the electronics to let the user focus on their project. This meant that it had to be work by simply plugging the device into a host computer and have an easy-to-use interface to program it.

The first prototype used the Parallax Javelin Stamp microcontroller, which used a subset of the Java programming language. This solution required the Parallax proprietary tools to compile, link and upload the projects to the microcontroller; therefore, it did not meet the requirements of the project because the wiring was going to be an open source project.

The second prototype used the Atmel ARM-based 91R40008 microcontroller. Hernando obtained better results with this new microcontroller; however, he determined that the microcontroller was far too complex, and it was almost impossible to solder it by hand to a circuit board.

The third prototype used the Atmel ATmega128 microcontroller with the MAVRIC microcontroller board. Hernando had great success using this microcontroller. He used a tool written by Brian Dan called Avrdude to easily upload new programs to the board.

Avrdude is still used today and can be found here: http://www.nongnu.org/avrdude/.

FTDI's hardware was chosen for the USB to serial communication because it had easy-to-obtain drivers for Linux, Windows and macOS platforms. This allowed the Wiring project to be compatible with all three major platforms.

In 2004, the IDII ordered and paid for 25 Wiring circuit boards. These boards were manufactured by SERP. They included the ATmega128 microcontroller, FTDI USB to serial hardware, onboard LED connected to a pin and serial RX/TX LEDs. Usability tests were performed using these boards and the results were great.

After graduating with distinction in 2004, Hernando moved back to his native Colombia to teach at the Universidad de Los Andes where he continues to work on Wiring. In May 2005, Hernando ordered 200 circuit boards and begin assembling the first Wiring boards outside of IDII. He sold these boards for approximately USD $60. By the end of 2005 Wiring was being used in various parts of the world.

Also, in 2005, the first Arduino board was created. The Arduino board used the less expensive ATmega128 microcontroller to reduce cost. The Arduino team forked the Wiring code and added support for this board.

The initial Arduino core team consisted of Massimo Banzi, David Cuartielles, Tom Igoe, Gianluca Martino and David Mellis. Hernando was not invited to participate in this project. There are several accounts from different individuals involved about why he was not invited.

I do not have any first-hand knowledge about which of these stories are true and which are false; therefore, for this book, I will leave it at the known truth of Hernando was not invited to participate in the Arduino project.

The Arduino team strongly believed in open source hardware and software. They believed that by opening the platform up, many more people would have access to and be involved with it. Another reason for opening the platform up was that IDII had used up its funding and was going to be shut down. By open sourcing the platform they knew it would survive and would not be exploited by others.

The team initially decided on a price of USD $30 for the board. They figured it would make it easily accessible to students as well individuals. They also decided to make the board blue, which was different from most other boards at the time, which were green. Another design decision that helped add to the popularity of the board was giving it lots of input and output pins. Most boards at the time limited the number of I/O to reduce costs.

Initially, the team ordered 300 printed circuit boards to conduct a usability test. They handed these boards out to students at IDII with three simple instructions: look up the assembly instructions online, build your board and use it to create something. They had great success with this test because the students were able to assemble the boards and create numerous projects with it.

Shortly after this test, people began to hear about this board and wanted one for themselves. The project started to take off; however, it was still missing a name. While discussing the name, the team was having drinks at a local a bar frequented by Massimo Banzi. The bar's name was Bar Di Re Arduino and the new board became known as the Arduino.

What is the Arduino?

At the heart of the Arduino is the microcontroller. A microcontroller is a standalone, single-chip integrated circuit that contains a CPU, read-only memory, random access memory and various I/O busses. Most Arduino boards use the Atmel 8-bit AVR microcontroller.

The Arduino UNO R3, which is the primary board used in this book, uses the ATmega328 chip. This chip is an 8-bit RISC-based microcontroller that features 32 KB of flash memory with read-write capabilities, 1 Kbyte EEPROM, 2 Kbytes SRAM, 23-general purpose I/O lines and 32 general-purpose registers. Do not be too concerned if you do not understand all those specifications because we will be interacting with the microcontroller using the interface that the Arduino board provides us. It is good to know these specifications as you begin to develop more complex applications because they do put limits on what we can do.

All the hardware and software that make up the Arduino platform are distributed as open source and licensed under the GNU Lesser General Public License (LGPL) or the GNU General Public License (GPL). This allows for the manufacture and distribution of Arduino boards by anyone and has led to numerous generic, lower cost, Arduino compatible boards.

You can find more information about the license and the Arduino boards on the Arduino website here: https://www.arduino.cc.

Touring the Arduino UNO R3

The Arduino is an open source hardware and software platform that is incredibly powerful yet easy to use. You can look at and download the code from any of the Arduino repositories on GitHub here: https://github.com/arduino. This platform has captured the imagination of electronic enthusiasts and the maker community everywhere. It enables people to inexpensively experiment with electronic prototypes and see their projects come to life. These projects can range from simply making an LED blink or recording the temperature to controlling 3D printers or making robots.

While there are numerous models of the Arduino, in this book we will primarily be using the very popular Arduino UNO R3 board. The following photograph shows the Arduino Uno's board layout with the main connectors identified:

For the purposes of this book, anytime we refer to the Arduino Uno board or the Uno board we are referring to the Arduino Uno R3 depicted in the preceding photograph.

As we can see, the Arduino Uno of today still uses the blue color that the original Arduino designers chose to help their boards stand out. The following is a list of major components of the Arduino Uno:

DC supply Input

: The DC supply input can be used with an AC-to-DC power adapter or a battery. The power source can be connected using a 2.1 mm center-positive plug. The Arduino Uno operates at 5 volts but can have a maximum input of 20 volts; however, it is recommended to not use more than 12V.

Voltage Regulator:

The Arduino uses a linear regulator to control the voltage going into the board.

USB Port:

The USB port can be used to power and program the board.

RESET button:

This button, when pressed, will reset the board.

ICSP for USB:

The in-circuit serial programming pins are used to flash the firmware on the USB interface chip.

ICSP for ATmega328:

The in-circuit serial programming pins are used to flash the firmware on the ATmega microcontroller.

Digital and PWM connectors

: These pins, labeled 0 to 13, can be used as either a digital input or output pins. The pins labeled with the tilde (~) can also be used for

Pulse-Width Modulation

(

PWM

) output.

Analog In Connectors:

The pins, labeled A0 to A5, can be used for analog input. These pins can be used to read the output from analog sensors.

Power and External Reset:

These pins in this header, provide ground and power for external devices and sensors from the Arduino. The Arduino can also be powered through these pins. There is also a reset pin that can be used to reset the Arduino.

ATmega328:

The microcontroller for the Arduino Uno board.

The Digital/PWM/Analog in/Power/Reset connectors are collectively known as the pin headers. The pins in these headers allow the Arduino to communicate with external sensors and other devices. Let's look at the different ways that we can power the Arduino board.

Powering the Arduino

The Arduino can be powered in one of three ways: through the VIN/GND pins, the DC Supply Input port or the USB port.

Using the VIN/GND pins to power the Arduino

The VIN and GND pins in the power and external reset header can be used to power the Arduino with an external battery. Powering the Arduino in this way is mainly used when we wish to connect a battery, in series, with a switch to turn the power to the Arduino on and off. The following photograph illustrates this:

It is not recommended that we power the Arduino in this manner unless we are looking for the most expensive and short-lived way to power the Arduino. We could use six AA batteries in series, which will provide the same voltage as the 9V battery in the preceding photograph but would give us approximately four times the capacity. It is still not recommended that we power the Arduino in this manner as it would be fairly expensive.

Unless there is a specific need to use a battery to power the Arduino, I would avoid using them.

Using the DC supply input to power the Arduino

The DC supply input connector can be used with an AC-to-DC power adapter or a battery to power the Arduino. The connector has a female 2.1 mm center-positive plug. While the Arduino operates at 5 volts a maximum input of 20 volts can be used; however, as was stated earlier, it is recommended to not use more than 12V.

We can use an AC-to-DC adjustable power adapter like the one shown in the following photograph to power the Arduino using the DC supply input connector:

With this adapter, you can adjust the output power to the desired voltage. You can find power supplies similar to this online or at most stores that sell electronic items.

Using the USB connector to power the Arduino

Using the USB connector to power the Arduino is the way that I usually power it. It is by far the easiest and safest way to power the Arduino and the least expensive. You can power the Arduino directly from the USB port on your computer or from a USB rechargeable power bank like the one shown in the following photograph:

This is a very affordable and simple way to power the Arduino. It can also be used for robotic or similar projects that need the mobility to move around; however, we do need to be careful when we connect shields or other accessories to the Arduino that the USB connector can draw enough power. As an example, later in this book, we will look at the MOVI speech synthesizing and voice recognition shield that draws too much power for the Arduino to be powered by the USB connector while the shield is connected.

Now that we have mentioned Arduino shields, let's look at what they are and see the types of functionality they can provide.

Arduino shields

An Arduino shield is a modular circuit board that plugs directly into the pin headers of the Arduino board. These shields will add extra functionality to the Arduino board. If we are looking to connect to the internet, do speech recognition, control DC motors or add other functionality to the Arduino, there is probably a shield that can help us. While we are not required to use shields, they do make adding extra functionality to our Arduino boards very easy.

The following photograph shows examples of a few shields. We will be using shields in some of our sample projects later in this book:

A shield fits on top of the Arduino by plugging directly into the pin headers. We can also stack one shield on top of another if they do not use the same resources. Here is how an Arduino looks with two shields attached:

An Arduino shield makes it incredibly easy to add functionality to an Arduino Uno. Most shields usually have great documentation as well, which makes programming them also very easy. The drawback to shields is they usually cost more than purchasing the components and connecting them to the Arduino with a breadboard.

Some shields, such as the MOVI speech synthesizing and voice recognition shield and the Sparkfun Xbee radio module shield, add functionality that cannot simply be added as a single component. For functionality like this, a shield or an external circuit board would be required.

Let's take a closer look at the pin headers for the Arduino Uno R3.

Arduino pin

There is a total of 31 pins in the Arduino Uno pin headers. Most of these pins can be configured to perform different functions. The following diagram shows what the various pins can be used for:

Let's look at what the different pins do.

Digital pins

The digital pins on the Arduino are the ones that are used the most when connecting external sensors. These pins can be configured for either input or output. These pins default to an input state; therefore, when we are using a pin for input we do not need to explicitly declare them as input pins; however, it is good practice to do so because it will make it easier for someone reading our code to understand what the pin is being used for.

The digital pins will have one of two values: HIGH (1), which is 5V, or LOW (0), which is 0V. Once we start to program the Arduino, we will see how to read from or write to these pins.

Analog input pins

The Arduino Uno contains a built-in Analog-To-Digital (ADC) converter with six channels, which gives us six analog input pins. The ADC converts an analog signal into a digital value. While the digital pins have two values, either high or low, the analog input pins have values from 0 to 1023 relative to the reference value of the Arduino. The Arduino Uno has a reference value of 5V.

The analog input pins are used to read analog sensors such as rangefinders and temperature sensors. The six analog pins can also be configured as digital pins if we run out of digital pins in our project.

PWM pins

Where the analog input pins are designed to read analog sensors (input), the PWM pins are designed for output. PWM is a technique for obtaining analog results with digital output.

Since a digital output can be either on or off, to obtain the analog output the digital output is switch between HIGH and LOW rapidly. The percentage of the time that the signal is high is called the duty cycle. The following diagram illustrates this concept:

We have the ability to set the frequency of how fast the signal can switch between HIGH and LOW. This frequency is measured in Hertz and sets how many times the signal can switch per second. For example, if we set the frequency to 500 Hz, that would mean that the signal could switch 500 times a second.

We will be using the PWM pins for several examples in this book and will examine them more when we learn how to program the Arduino.

Power pins

The Arduino has several power pins. They are as follows:

VIN

: This pin is used when we power the Arduino board using an external power supply. This is the pin used in the

Using the VIN/GND pins to power the Arduino

section of this chapter.

GND

: These are the ground pins.

5V

: This is 5V out and is used to power most sensors.

3.3V

: This is 3.3V out and can be used to power sensors that are compatible with 3.3V. A list of some compatible 3.3V sensors can be found here:

https://www.dfrobot.com/wiki/index.php/3.3V_Compatible_Device_List

.

Reset

: This pin can be used to reset the Arduino board by an external source.

ioref

: This is the reference voltage for the board. For the Arduino, this will be 5V.

Serial pins

These pins can be used for serial communication. The RX (digital pin 0) is used to receive while TX (digital pin 1) is used to transmit. These pins are connected directly to the USB-to-TTL serial chip. One note, you should not connect these pins directly to an RS-232 serial port because you will damage your board.

SPI pins

The Serial Peripheral Interface (SPI) pins are used for a synchronous serial data protocol that is used by microcontrollers for communicating with peripheral devices. This protocol always has one master with one or more slave devices. The pins are:

MISO

: The

Master in Slave out

pin is used to send data from the slave to the master device.

MOSI

: The

Master out Slave in

the pin is used to send data from the master to the slave device.

SCK

: The

serial clock

synchronizes the data transmission and is generated by the master.

SS

: The

slave select

pin tells the slave to go active or to go to sleep. This is used to select which slave device should receive the transmission from the master.

Now that we have quickly looked at the pins on the Arduino Uno R3 let's look at some of the different Arduino boards.

Different Arduino boards

There are a number of different official Arduino boards and modules that can be used for various purposes. To see all the different boards, you can go to that Arduino product page (https://www.arduino.cc/en/Main/Products) where they list all the official Arduino boards.

While the Arduino Uno R3 is the most popular Arduino board within the maker community, the following lists some of the other popular boards:

Arduino Micro

The Arduino Micro is the smallest board in the Arduino family. It is based on the ATmega32U4 microcontroller. This board features 20 digital I/O pins of which 7 can be used for PWM output and 12 can be used as analog input. The Micro and the Nano (which we will see a little later) can be used for a project where the Arduino Uno may be too big.

Arduino Mega 2560

The Arduino Mega 2560 is designed for the most complex projects. It features 53 digital I/O pins, 16 analog input pins and 15 PWM output pins. It also has 4 serial UARTs for serial connections. If you want to create a complex project like a robot, the Mega is the board you will want to start with.

Lilypad

 

The Arduino Lilypad is designed for wearable projects. It can be sewn into fabrics and use power supplies and sensors that are also sewn into fabrics. The Lilypad is based on the ATmega168V or ATmega328V (low power versions). This board features 16 digital I/O, 6 analog inputs and 6 PWM outputs.

Arduino Nano

 

There are a lot of similarities between the Nano and the Micro. The Micro was released in 2012 while the Nano was released in 2008. The Nano features 14 digital I/O pins, 8 analog input pins and 6 PWM output pins. With those specifications, you may think that you should use the Micro board over the Nano however if you look at most online retailers like Amazon or eBay you can find the Nano for about half the price of the Micro.