0% found this document useful (0 votes)
49 views

Arduino

The document provides information about the Arduino board. It is an open-source single board microcontroller popular for hobby and professional use. Key features include an 8-bit microcontroller, memory, analog and digital pins that can be configured as inputs or outputs, and communication via USB. Programming involves installing the Arduino IDE and writing sketches with setup() and loop() functions to initialize pins and implement the main program logic.

Uploaded by

Luke
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Arduino

The document provides information about the Arduino board. It is an open-source single board microcontroller popular for hobby and professional use. Key features include an 8-bit microcontroller, memory, analog and digital pins that can be configured as inputs or outputs, and communication via USB. Programming involves installing the Arduino IDE and writing sketches with setup() and loop() functions to initialize pins and implement the main program logic.

Uploaded by

Luke
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Arduino

An easy to use, open-source and powerful single board computer that


is very popular in the hobby and professional market.
Arduino project was started in Italy to develop low cost hardware for
interaction design. Arduino home page is: http://www.arduino.cc/
Several Arduino boards exist. First version released in 2009 was the
Arduino Duemilanove. Arduino Uno (2011) is covered here.
Arduino Uno
Arduino Uno features
• Atmel ATmega328P 8-bit Microcontroller
• 32 KB of Flash memory (application programs), 2KB of SRAM
and 1KB of EEPROM (non-volatile data)
• Clock speed is 16MHz.
• USB Port: communicate to computer or as power supply
• Power IN: The power can be supplied either from USB connector
(5V), DC power jack (7-12V via AC-to-DC adapter), or
VIN/Ground PIN (7-12V via 9V battery).
• Power OUT: The board outputs regulated 5V and 3.3V (50mA)
for powering external components.
• 14x Digital INPUT/OUTPUT pins (numbered from 0 to 13):
operates at 5V and can provide/receive a maximum of 40 mA.
 Pin 13: There is a built-in LED connected to Pin 13
 PWM output: 6 pins (pins 3, 5, 6, 9, 10 and 11, marked ~)
• 6x Analog INPUT pins (A0 to A5): By default, each pin can
measure between 0 to 5V with 10-bit of resolution.
Arduino Uno features
• Atmel ATmega328P 8-bit Microcontroller

• 32 KB of Flash memory (application programs), 2KB of SRAM


and 1KB of EEPROM (non-volatile data)
• Clock speed is 16MHz.
• USB Port: communicate to computer or as power supply
Arduino Uno features
• Power OUT: The board outputs regulated 5V and 3.3V (50mA)
for powering external components.
• 14x Digital INPUT/OUTPUT pins (numbered from 0 to 13):
operates at 5V and can provide/receive a maximum of 40 mA.
 Pin 13: There is a built-in LED connected to Pin 13
 PWM output: 6 pins (pins 3, 5, 6, 9, 10 and 11, marked ~)

• 6x Analog INPUT pins (A0 to A5): By default, each pin can


measure between 0 to 5V with 10-bit of resolution.

• RESET Button in case things go wrong.


Powering the Arduino
Power IN:
• from USB connector (5V)
• DC power jack (7-12V via
AC-to-DC adapter)
• VIN/Ground PIN (7-12V
via 9V battery).
Using the Arduino UNO
Step 1: Download and install the Arduino Development Software

 From Arduino website http://arduino.cc/en/Main/Software


 Choose the version that matches your operating platform.
 Called Arduino IDE (Integrated Development Environment)

Step 2: Plug-in the Arduino Board to Install the Driver

 Plug in the Arduino board with the USB cable to the computer. The
"ON" LED lights up (in green or orange).

Step 3: Launch the Arduino SDK to Write your First Program

 In Arduino jargon, programs are called “sketches”


How the Arduino IDE works
How the Arduino IDE works
The command area at the top of IDE includes the title bar, menu
items, and icons. The title bar displays the sketch’s filename as well
as the version of the IDE. Below this is a series of menu items (File,
Edit, Sketch, Tools, and Help). The icons are described below:
Programming the Arduino UNO
An Arduino sketch must have:
setup() and loop() // setup() runs once when the sketch starts

void setup()
 setup(): configures pin modes {
// initialize the digital pin as an output:
pinMode(ledPin, OUTPUT);
}

// loop() method runs forever,


// as long as the Arduino has power

 loop(): runs the main body of void loop()


{
the program forever until the digitalWrite(ledPin, HIGH); // set the LED on
power is shut off or RESET is delay(DELAY_ON); // wait for DELAY_ON
digitalWrite(ledPin, LOW); // set the LED off
pressed. This is where important
delay(DELAY_OFF); // wait for DELAY_OFF
things in your program happen. }

 May require some initialisations


Programming the Arduino
• The setup() section is used for assigning input and outputs (for
examples, motors, LEDs, sensors etc) to ports on the Arduino.

• It also specifies whether the device is OUTPUT or INPUT

• To do this we use the command “pinMode”

• pinMode takes two arguments, the number of the Arduino pin and
whether it is specified as INPUT or OUTPUT. For example, if we
connect an LED to digital I/O pin 9:
void setup() {

pinMode(9, OUTPUT); //configure pin 9 as output

}
Programming the Arduino
• loop(): runs the main body of the program forever until the power
is shut off or someone presses the RESET button. This is where
important things in your program happen.

Variables
• A variable is a place for storing a piece of data. It has a name, a
type, and a value.

int ledPin = 13;


int bright = 0;

Functions
• A function is a named piece of code that can be used from
elsewhere in a sketch. A function that is already been defined can
be called by stating the function name and passing its parameters.
Arduino Programming
Essential Functions

• pinMode(pinNumber, mode); // declares a pin INPUT or OUTPUT

• digitalRead(pinNumber); // reads the HIGH/LOW status of pin

• digitalWrite(pinNumber, value); // forces HIGH/LOW voltage

• analogWrite(pinNumber, value); // PWM for intermediate values

• analogRead(pinNumber); // reads analog pin through ADC


Arduino Programming
Additional Functions

• delay(ms); // delay for ms milliseconds


• millis(); // return total milliseconds since program start
• Serial.begin(baud); // set up serial communication to host
• Serial.print(val); // print on monitor (number, char, or string)
• Serial.println(val); // print with line feed
• random(min, max); // return random between min, max-1
• map(val, fromLo, fromHi, toLo, toHi); // interpolate to range
• constrain(val, lo, hi); // constrain value to a range
Programming the Arduino
• Two functions are available in Arduino to receive/send commands
and other information to input/output devices through digital input
& output pins:
 digitalRead
 digitalWrite

• Each digital pin can be set to one of two values:


 High - logic 1 or +5V and Low - logic 0 or 0V
Programming the Arduino
1. digitalWrite(pin, value) where pin is the pin number of Arduino
and value is either LOW or HIGH

 digitalWrite is able to turn on or off the specified digital pin that


has been configured as an OUTPUT.

 In the previous example, pin 9 was configured as an output pin


using pinMode function. Suppose we want to send a low voltage
(0 V) to pin 9.

digitalWrite(9, LOW);

If an LED is connected to pin 9, it will be OFF when this line is


written in loop().
Programming the Arduino
2. digitalRead(pin) where pin is the pin number of Arduino

 digitalRead reads the value from a specified digital pin that has
been configured as an input (either HIGH or LOW).

 Suppose a switch is connected to pin 8 and we want to know the


status of the switch.

digitalRead(8);

If the switch is OFF, the function will return LOW, else it returns
HIGH.
Programming the Arduino
• Another interesting function is the delay function which pauses the
program for the amount of time (in miliseconds) specified as
parameter.

delay(ms) where ms is the number of milliseconds during


which the program pauses.
How to connect an LED to Arduino
• When connect LEDs to an Arduino, you need to consider the
operating voltage and current. Common LEDs require a voltage of
about 1.7 V and a current in the range of 5-20 mA to operate.

• Problem: Arduino outputs 5 V and a much higher current (40 mA).

• Fortunately, the voltage across and current through the LED can
be reduced by using a resistor (What value)?

• Use Ohm’s Law (V = IR) to determine resistor value.

• If you are not sure, select a slightly higher value resistor because it
is better to have a dim LED than a dead one!
Flashing LED
Write a sketch that flashes an LED connected to Pin 2 continuously
(on/off for 0.5 second each)

void setup() // one-time actions


{
pinMode(2, OUTPUT); // define pin 2 as an output
}

void loop() // loop forever


{
digitalWrite(2, HIGH); // pin 2 high (LED on)
delay(500); // wait 500 ms
digitalWrite(2, LOW); // pin 2 low (LED off)
delay(500); // wait 500 ms
}
Walking LEDs
Write a sketch that implements a continuous walking LED sequence
with three LEDS connected to pins 2, 3 and 4 (0.5 seconds ON in
turn).

Often in sketches, you will repeat the same function. Instead of


duplicating the function in a sketch which will waste the Arduino’s
program memory, you can use for loops.

The benefit of using a for loop is that you can determine how many
times the code inside the loop will repeat.

for (int a = 1; a < 5; a++)


{statements}
Making Decisions with if
• Many times decisions must be made in a sketch. If a condition
arises, the circuit performs a task, else it does something else.
if (condition)
{do something}
else
{do something else}

• If the condition is met, the first block of code will be executed,


otherwise the block of code directly following else will be
executed. Sometimes in sketches, the else part may not be present
or there may be several nested if statements.

• Other decision-making statements:


 do {statements} while (condition)
 while (condition) {statements}
 switch (var) {case 1: statements; break; case 2……)
Comparison Operators

Logical Operators

if (!a)
Output Analog Signals
• With digitalWrite function, only digital signals can be output at the
I/O pins (LOW or HIGH).

• Often, in real circuits, you need to adjust the output within a range.
For example:

 Brightness of a lamp
 a volume knob on a stereo
 a heat setting on an oven
 a steering wheel in a car
 speed of a DC motor

• The Arduino board (like other digital devices) does not have
analog output. Instead, it uses Pulse Width Modulation (PWM) to
output analog signals. The Arduino Uno has 6 digital I/O pins that
can be used as PWM (pins 3, 5, 6, 9, 10 and 11, marked ~)
Pulse Width Modulation (PWM)
A microcontroller can easily work with inputs and outputs that have
only two states: on and off (for example, LED, relay).

Sometimes more than just "on" & "off " control of device is needed.
For example, control the brightness of a lamp or the speed of DC
motor (signals between 0 and 5V). This situation can be handled by a
technique called PWM.

Generate a square wave and control the % of total time the output is
high. Also ensure that frequency is high enough.
Pulse Width Modulation (PWM)
• analogWrite(pin, duty-cycle) is used to output a PWM signal

where pin is the digital pin from which PWM signal is output and

duty-cycle is a value between 0 and 255 representing the output


voltage level. 0 indicates a 0% duty cycle (0 V) and 255 indicates
100% duty cycle (5 V).

Thus analogWrite(9, 128) will output a voltage of 2.5 V at pin 9.

Example:

Write a sketch that will turn on an LED connected to I/O pin 3 and
progressively increase its level of brightness until it is fully lit. Then,
the sketch decreases the brightness of the LED gradually until it is off.
Example
#define LED 3
int d = 5;

void setup()
{
pinMode(LED, OUTPUT);
}
for ( int a = 255 ; a >= 0 ; a-- )
void loop()
{
{
analogWrite(LED, a);
for ( int a = 0 ; a < 256 ; a++ )
delay(d);
{
}
analogWrite(LED, a);
delay(d);
delay(1000);
}
}
Reading Analog Inputs
• As mentioned earlier, there are six analog input pins on the
Arduino UNO that allows measurement of analog voltage values.

• analogRead(pin) function reads the value from the specified


analog pin pin.

• Since the Arduino processes only 0s and 1s, it will map the input
voltage (between 0-5 V) into integer values between 0 and 1023.
This yields a resolution of: 5 V/1024 units or 4.9 mV per unit.
Reading Analog Inputs
• Usually in sketches, an integer variable is used to store the value
returned by the analogRead() function.

int a = analogRead(2);

IMPORTANT NOTE

While the on-board ADC has a 10-bit resolution (0 to 1023), the


analog outputs have an 8-bit resolution only (0 to 255).

Therefore, if you are reading an analog input and outputting it


directly, you need to perform a mapping:

 Either divide by 4
 Or use map(value, fromLow, fromHigh, toLow, toHigh)
function.
Reading Analog Inputs
• How to make an analog input signal (varying input signal)?

Answer: use a potentiometer (referred to as pot in Arduino jargon)


It is a variable resistor that can generally be adjusted from 0 Ω up
to its rated value.
Example
Write a sketch that controls the brightness of an LED (connected to
digital I/O pin 11) with a potentiometer (connected to analog input pin
0).

Hardware Required:
1x Arduino Uno
1x USB cable
1x Breadboard
1x LED
1x Potentiometer
1x 220 Ohm resistor (Red Red Brown Gold)
7x Jumper Wires
Example
int pot_pin = 0;
int led_pin = 11;
//variable for storing potentiometer value
int pot_value = 0;
void setup()
{
pinMode(led_pin, OUTPUT);
}
void loop()
{
pot_value = analogRead(pot_pin); // Read potentiometer value
analogWrite(led_pin, pot_value / 4); // map value and output to LED
delay(50);
}
The Serial Monitor
• The Arduino IDE has a window called
the Serial Monitor which can be
opened by clicking on the upper-right
icon.
• It allows you to both send messages
from your computer to an Arduino
board (over USB) and also to receive
messages from the Arduino.
• It is very useful during debugging as it
allows you to see what’s going on as
the program executes.
• In particular, it allows you to print
input values and messages to a serial
monitor.
The Serial Monitor
• To establish communication
between your Arduino board
and a laptop/ computer,
include Serial.begin(9600)
statement in setup()

• “9600” refers to the baud


rate (how fast the data is to
be sent)

• You can then use the Serial.print() to send information from your
Arduino to your computer, so you can see the value or message
displayed on your computer’s monitor in real-time.

• Serial.println is similar to Serial.print but starts a new line after


printing whatever is in its parameter.

You might also like