Welcome To The Arduino
Welcome To The Arduino
The Arduino was originally built for designers and artists—people with little
technical expertise.
So, it should come as no surprise that the first steps with the Arduino are very
easy, even more so for people with a strong technical background.
After all, computers are physical objects, and they accept input from physical
keyboards and mice.
They output sound and video to physical speakers and displays. So, isn’t all
computing physical computing in the end?
But controlling special sensors and actuators, using a regular computer is very
difficult. Using an Arduino, it’s a piece of cake to control sophisticated and
sometimes even weird devices. In the rest of this book, you’ll learn how, and in
this chapter you’ll get started with physical computing by learning how to
control the Arduino.
What tools you need, and how to install and configure them. Then we’ll quickly
get to the fun part: you’ll develop your first program for the Arduino.
Wikipedia states “An Arduino is a single-board microcontroller and a software suite for
programming it. The hardware consists of a simple open hardware design for the controller
with an Atmel AVR processor and on-board I/O support. The software consists of a standard
programming language and the boot loader that runs on the board.”
To put that in layman’s terms, an Arduino is a tiny computer that you can program to process
inputs and outputs between the device and external components you connect to it (see Figure
1-1).
The Arduino is what is known as a Physical or Embedded Computing platform, which means
that it is an interactive system that can interact with its environment through the use of
hardware and software.
For example, a simple use of an Arduino would be to turn a light on for a set period of time,
let’s say 30 seconds, after a button has been pressed. In this example, the Arduino would have
a lamp and a button connected to it.
The Arduino would sit patiently waiting for the button to be pressed; once pressed, the Arduino
would turn the lamp on and start counting.
Once it had counted for 30 seconds, it would turn the lamp off and then wait for another button
press. You could use this setup to control a lamp in a closet, for example.
To connect an Arduino to your computer, you just need an USB cable. Then you can
use the USB connection for various purposes:
Upload new software to the board.
Communicate with the Arduino board and your computer.
Supply the Arduino board with power.
As an electronic device, the Arduino needs power. One way to power it is to connect it to a
computer’s USB port, but that isn’t a good solution in some cases. Some projects don’t
necessarily need a computer, and it would be overkill to use a whole computer just to power
the Arduino. Also, the USB port only delivers 5 volts, and sometimes you need more.
In these situations, the best solution usually is an AC adapter supplying 9 volts (the
recommended range is 7V to 12V). Plug it into the Arduino’s power jack, and it will start
immediately, even if it isn’t connected to a computer. By the way, even if you connect the
Arduino to an USB port, it will use the external power supply if available.
www.arduino.cc
1) Download
2) Windows Installer
3) JUST DOWNLOAD
The Arduino Uno automatically draws power from either the USB connection to the computer
or an external power supply.
i. Plug in your board and wait for Windows to begin its driver installation process. After a
few moments, the process will fail, despite its best efforts.
ii. Click on the Start Menu, and open up the Control Panel.
A few seconds after the upload finishes, you should see the pin 13 (L) LED on the board start
to blink (in orange). If it does, congratulations! You've gotten Arduino up-and-running.
Almost all systems that use physical computing will have some form of
output
What are some examples of Outputs?
5 5
V V
0 0
V V
Digital I/O Pins the Arduino I/O board has 14 Digital pins that can be
configured and used individually as Inputs or Outputs from the program. When
a digital pin is configured as INPUT it can be used to read all kind of sensors
that give devices ON and OFF, like push buttons, touch sensors, switches …
etc.
The Arduino I/O board has 6 Analog Inputs capable of reading voltages
between 0-5V. Internally the voltages are translated into number from 0 to
1023. These inputs can be used to measure continuous quantities like light
intensity, temperature, Proximity, Position etc. Depending on the type of
sensor.
The Arduino I/O board has 6 PWM (Pulse Width Modulation) outputs. PWM
consist of switching something ON and off thousands of times per second,
allowing effects like dimming a light or control the speed of a motor.
Indicators
The wiring I/O board has a power (green) LED indicator that is turned ON
when the board is powered. It also includes LEDs for the Serial port (USB)
data transmission reception.
Voltage Out
Digital Pins
• General Purpose I/O Pin are 2, 3, 4, 5, 6, 7,8, 9, 10, 11, 12, & 13
• Analog I/O can also be used as digital pings; analog input A0 as digital pin 14 through
analog input A5 as digital pin 19
• When the pin is HIGH value, the LED is on, when the pin is LOW value, it's off
Software • Hardware
• pinMode() • 5mm LED (1)
• digitalWrite() • 220 Ohm Resister (1)
• delay() • Arduino Uno (1)
Breadboard
Jumper Wires
Breadboard Layout
Connected dots
represent connectivity
Features:
A basic 5mm LED with a red lens.
1.8-2.2VDC forward drop
Max current: 20mA
Suggested using current: 16-18mA
Luminous Intensity: 150-200mcd
Safety Tips:
Must use a pull-down resister to connect to 0V(ground)!
Math: (5V-1.8V)/16mA = 200 ohm
Pick: 220 ohm resister
Software
/*
Blink two LEDs together
Modified by Matt Royer May 5, 2014
*/
int led1 = 13;
int led2 = 12; // Additional LED Pin
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT); // Additional LED Pinmode
}
// the loop routine runs over and over again forever:
void loop() {
digitalWrite(led1, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(led2, LOW); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(led1, LOW); // turn the LED off by making the voltage LOW
digitalWrite(led2, HIGH); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
Arrays
Create an Array:
Syntax: int myInts[6];
int myPins[] = {2, 4, 8, 3, 6, 1};
int mySensVals[6] = {2, 4, -8, 3, 2, 0};
char message[6] = "hello!;
Access an Array:
Array index starts from 0.
int myArray[10]={9,3,2,4,3,2,7,8,9,11};
// myArray[9] contains 11
// myArray[10] is invalid and contains random information (other memory
address)
Arrays are often manipulated inside for loops, where the loop
counter is used as the index for each array element. For example, to
print the elements of an array over the serial port, you could do
something like this:
Example:
int i;
int myPins[3] = {0,1,2};
for (i = 0; i < 3; i = i + 1) {
digitalWrite(myPins[i], HIGH);
delay(2000); //delay for 2 seconds
}
Software Hardware
• pinMode() • 5mm LED (3)
• digitalWrite() • 220 Ohm Resister (3)
• array [ ] • Arduino Uno Board (1)
• for loop Breadboard
• delay() Jumper Wires
Software
const int numberOfLED = 3; // Number of LED in Array
const int lEDToBlink[numberOfLED] = { // Array to store LED Pins
13, 12, 11
};
// the setup routine runs once when you press reset:
void setup() {
for (int initalizeLED = 0; initalizeLED < numberOfLED; initalizeLED++){
pinMode(lEDToBlink[initalizeLED], OUTPUT);
}
}
void loop() {
for (int lightLED = 0; lightLED < numberOfLED; lightLED++){ // For each LED in Array,
digitalWrite(lEDToBlink[lightLED], HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000);
digitalWrite(lEDToBlink[lightLED], LOW); // turn the LED on (HIGH is the voltage level)
}
}
Syntax: while(expression){
// statement(s)
}
Example: var = 0;
while (var < 200) {
// do something repetitive 200 times
var++;
}
If
Syntax: if (boolean condition) {
//statement(s);
}
0V
1.25V
2.5V
3.75V
5V
• Parameters:
• Syntax: Serial.write(val)
Note: Sketch must be loaded first; else, Serial Monitor will close on Sketch upload
Software
• Serial.begin() Hardware
• Serial.print() • Arduino Uno Board (1)
• Serial.println()
• for loop
• delay()
Software
int i;
void setup() {
// initialize the serial interface
Serial.being(9600);
}
void loop(){
delay (2000); //borrow some time for opening
serial monitor
// Serial print
for (i=10; i>=0; i--) {
Serial.println(i);;
delay(1000);
}
Serial.print(“Hello World!\n”);
}
• digitalRead(): Reads the value from a specified digital pin, either HIGH
or LOW
• Syntax: digitalRead(pin)
• Hardware
Software
• 5mm LED (1)
• pinMode()
• Momentary Switch/push button (1)
• digitalWrite()
• 220 Ohm Resister (1)
• digitalRead()
• 10k Ohm Resister (1)
• if / else
• Arduino (1)
Breadboard
Jumper Wires
Software Hardware
• pinMode() • 5mm LED (1)
• digitalWrite() • Momentary Switch/push button (1)
• if • 220 Ohm Resister (1)
• delay() • 10k Ohm Resister (1)
• Arduino Uno Board (1)
Breadboard
Jumper Wires
Action Round #2- Blink LED
Continuously while button is
pressed
Software
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin:
int buttonState = 0;
void setup() {
Hardware pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
• Connect power rails to breadboard }
• D13>220 ohm->anode->cathode- void loop(){
>GND buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
• Vcc->10k ohm-> pushbutton->GND digitalWrite(ledPin, HIGH);
• D2->pushbutton and resistor delay(500);
digitalWrite(ledPin, LOW);
junction point delay(500);
}
digitalWrite(ledPin, LOW);
}
Software Hardware
• pinMode() • 5mm LED (1)
• digitalWrite() • Momentary Switch/push button (1)
• digitalRead() • 220 Ohm Resister (1)
• If • 10k Ohm Resister (1)
• boolean variable • Arduino Uno Board (1)
• boolean operation (!) Breadboard
• delay() Jumper Wires
Analog Signal
• Analog signal is any continuous signal Vs. time!
• Digital signal uses discrete (discontinuous) values.
Analog Sensors
Potentiometer LM35
LDR
Software Hardware
• analogRead() • Potentiometer(1)
• Serial.println() Breadboard
Jumper Wires
Hint:
• The range of POT voltage output is 0 to 5V
• Define a floating point variable, ex. float vPOT;
• vPOT = sensorValue/sensorFullRange * 5.0
• Don’t forget the type casting!
Software • Hardware
• analogWrite() • Potentiometer
• pinMode() Breadboard
Jumper Wires
Software
• First run the code
-> File->Example-> Analog->Calibration
• Add Serial Print to monitor
Print out the Sensor Min value and Sensor Max value to see how it
changes as flex sensor is being bent.
Serial.begin(9600);
Serial.print("Calibration time sensorMin = ");
Serial.println(sensorMin);
Map() function
• map(): Re-maps a number from one range to another.
Value can be mapped to values that are out of range.
• Syntax: map(Source, fromLow, fromHigh, toLow, toHigh)
• Example: map(val, 0, 1023, 0, 255)
Constrain() function
• Constrains a number to be within a range.
• Syntax: constrain(x, a, b)
• Returns:
• x: if x is between a and b
• a: if x is less than a
• b: if x is greater than b
• Example:
• sensVal = constrain(sensVal, 10, 150);
• // limits range of sensor values to between 10 and 150
Schematic Diagram
Features
Software
• Hardware
• analogWrite()
• 5mm LED
• analogRead()
• 220 Ohm Resister
• LDR
Breadboard
Jumper Wires
Goal: When the Lighting is very high all LEDs off, When the
ON, When the lighting is low 3 LED ON, When the lighting is
• Servo motors
A servomotor is a rotary actuator that allows for precise control of angular
position, velocity and acceleration.
0-180 degrees
DC Motor:
The most common actuator in mobile robots robotics is
the direct current (DC) motor
Advantages: simple, cheap, various sized and packages,
easy to interface, clean.
DC motors convert electrical energy into mechanical
energy.
Problem!!!!
Motors need at least 200mA to start on how could I do
this from Arduino?!
• John Bardeen, Walter Brattain, and William Schockly developed the first model
of transistor (a Three Points transistor, made with Germanium)
What is a transistor?
• The Transistor is a three-terminal, semiconductor device.
• It’s possible to control electric current or voltage between
two of the terminals (by applying an electric current or
voltage to the third terminal). The transistor is an active
component.
Transistor symbols
force – voltage/current
water flow – current
- amplification
Connection on breadboard:
Connection on breadboard:
Connections:
Servo Motor:
It is sometimes necessary to move a motor to a specific
position
DC motors are not built for this purpose, but servo motors are
Servo motors are adapted DC motors:
Gear reduction
Position sensor (potentiometer)
Electronic controller
Range of at least 180 degrees
Servo myservo ;
myservo.attach(9);
myservo.write(pos);
Software
#include <Servo.h>
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
Serial.begin(9600);
}
void loop()
{
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
Serial.println(val);
val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
}
Ultrasonic Sensor
An ultrasonic transducer is a device that converts energy
into ultrasound, or sound waves above the normal range
of human hearing.
Ultrasonic operation
Ultrasonic sensors (also known as transceivers when they both send and
receive) work on a principle similar to radar or sonar which evaluate
attributes of a target by interpreting the echoes from radio or sound
waves respectively.
Sensors calculate the time interval between sending the signal and
receiving the echo to determine the distance to an object.
• range: 0.04 to 4m
Ultrasonic Library
#include "Ultrasonic.h“
ultrasonic.Ranging(CM);
IR Sensor
• IR Sensors work by using a specific light sensor to detect
a select light wavelength in the Infra-Red (IR) spectrum.
• By using an LED which produces light at the same
wavelength as what the sensor is looking for, you can
look at the intensity of the received light.
• When an object is close to the sensor, the light from the
LED bounces off the object and into the light sensor.
• This results in a large jump in the intensity, which we
already know can be detected using a threshold.
Digital Read
Examples:
Example1: Connect the IR sensor and use serial monitor to
check the values.
Relay
• A relay is an electrically operated switch.
The light emitting diodes in a seven-segment display are arranged in the figure
below.
Examples:
Example1: show the numbers from 0 to 9 on the 7 segment
Example2: use the 7-segment with the counter you did using
IR sensor.
Pins Configuration
Pin 4 is the Register Select (RS) line, the first of the three
command control inputs, when the line is low, data bytes
transferred to the LCD as command, By setting the RS
line high, character data is transferred to the LCD.
Keypad:
Software • Hardware
• Serial.begin() • Arduino Uno (1)
• Serial.availible()
• Serial.read()
• Serial.print()
• Serial.write()
• Serial.println()
char character;
String content = "";
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600
bps
}
void loop() {
while(Serial.available()) {
character = Serial.read();
if (character != "") {
Bluetooth
• Bluetooth is a wireless technology standard for
exchanging data over short distances (using short-
wavelength UHF radio waves in the ISM band from 2.4 to
2.485 GHz) from fixed and mobile devices and building
personal area networks (PANs).
• Invented by telecom vendor Ericsson in 1994, it was
originally conceived as a wireless alternative to RS-232
data cables.
• It can connect several devices, overcoming problems of
synchronization.
Examples:
Example1: Send the data from Bluetooth and print it to the
serial monitor.
Project Equipment
Arduino Uno.
Car Chassis Kit.
Motor Driver.
Batteries (power supply).
Example:
Example1: Make car avoiding robot using ultrasonic.