Arduino LAB Manual PDF
Arduino LAB Manual PDF
Prepared by
Dr. Abdulrahman Ikram Siddiq
2015
Contents
1 Digital Output 3
2 Digital Input 4
3 Digital I/O 5
4 Serial I/O 6
5 Analog Input 7
6 Load Driving 8
8 Light Sensor 10
9 LCD Interfacing 11
10 Heat Sensor 13
11 Interrupts 14
Arduino UNO Board Briefly
Component Explanations
Analog input pins – pins (A0-A5) that take-in analog values to be converted to
be represented with a number range 0-1023 through an Analog to Digital
Converter (ADC).
ATmega328 chip – 8-bit microcontroller that processes the sketch you
programmed.
Built-in LED – an on board LED to pin 13.
Crystal Oscillator – clock that has a frequency of 16MHz
DC Jack – where the power source (AC-to-DC adapter or battery) should be
connected. It is limited to input values between 6-20V but recommended to be
around 7-12V.
Digital I/O pins – input and output pins (0-13) of which 6 of them (3, 5, 6, 9, 10
and 11) also provide PWM (Pulse Width Modulated) output by using the
analogWrite() function. Pins (0 (RX) and 1 (TX)) are also used to transmit and
receive serial data.
ICSP Header – pins for “In-Circuit Serial Programming” which is another
method of programming.
ON indicator – LED that lights up when the board is connected to a power
source.
Power Pins – pins that can be used to supply a circuit with values VIN (voltage
from DC Jack), 3.3V and 5V.
1
Reset Button – a button that is pressed whenever you need to restart the
sketch programmed in the board.
USB port – allows the user to connect with a USB cable the board to a PC to
upload sketches or provide a voltage supply to the board. This is also used for
serial communication through the serial monitor from the Arduino software.
2
THIRD Digital Output MICROCONTROLLER
STAGE Experiment Number (1) LABORATORY
Objective: To learn how to implement digital output from the Arduino board.
Instructions: The software of this experiment requires the following instructions:
(pinMode, digitalWrite, and delay). See the instruction reference book.
Equipment:
Arduino UNO board
LDEs
220 resistors
Procedure:
1. Connect the circuit of Fig.1.
2. Write a program to:
define pin 13 as a digital output line. Fig. 1
make the LED blink every 1 second.
3. Modify the circuit of Fig.1 to have 2 output LEDs on pins 8 and 9, and write
a program to make them blink complementarily every 1 second.
Discussion:
1. What is the roll of the 220 resistor? Can we just remove it?
2. Design and write a program for an Arduino based traffic light control
system for the traffic junction below.
3
THIRD Digital Input MICROCONTROLLER
STAGE Experiment Number (2) LABORATORY
Objective: To learn how to implement digital input using the Arduino board.
Instructions: (digitalRead) See the instruction reference book.
Equipment:
Arduino UNO board
LDE
Resistors 220 and 10
Two pushbuttons
Procedure:
1. Connect the circuit of Fig.1.
2. Write a program to output the state of pin 2 on pin 13.
3. Modify the circuit of Fig.1 and write a program to read the state of two
input switches sw1 and sw2. When sw1 is pressed, pin 13 becomes 1.
When sw2 is pressed, pin 13 becomes 0.
Fig. 1
Discussion:
1. What is the difference between pulled-up and pulled-down input pins?
2. Repeat step 2 of the procedure using a pulled-up input.
4
THIRD Digital I/O MICROCONTROLLER
STAGE Experiment Number (3) LABORATORY
Discussion:
Design and write a program for the system shown in Fig. 2, such that the output
LED is turned ON when the decimal representation of the 4-bit input code is
greater or equal to 8. Otherwise, the output LED is turned OFF.
Fig. 2
5
THIRD Serial I/O MICROCONTROLLER
STAGE Experiment Number (4) LABORATORY
Objective:
To learn how to implement serial I/O between the Arduino board and the PC
using the serial monitor graphical user interface (GUI).
Instructions:
(Serial.begin, Serial.print, Serial.println, and Serial.write). See the instruction
reference book.
Equipment:
Arduino UNO board
PC
Procedure:
1. Connect the Arduino card to the PC through the USB cable.
2. Open the serial monitor GUI.
3. Write a program to send the following data to the PC through the serial
port. Observe the output on the serial monitor GUI.
data type
427, 61, 92 integers
a, b, A, C, g characters
Hello string
4. Write a program to receive a binary value X (0 or 1) from the serial monitor
GUI. If X=1, then turn pin 13 ON. Otherwise, (X=0) turn pin 13 OFF.
Discussion:
Write a program to:
1. Print the phrase "Enter the average" on the serial monitor GUI.
2. Receive an average X, ( X ) from the serial monitor GUI.
3. If the average X 50, then turn pin 13 ON. Otherwise, (X 50), then pin 13
is turned OFF.
4. Repeat steps 1-3.
6
THIRD Analog Input MICROCONTROLLER
STAGE Experiment Number (5) LABORATORY
Objective:
To learn how to implement analog input using the Arduino board.
Instructions:
(analogRead, and map). See the instruction reference book.
Equipment:
Arduino UNO board
A variable resistance
Procedure:
1. Connect the circuit of Fig.1.
2. Write a program to read the analog voltage on the AN0 pin, and display it
on the serial monitor GUI every 1 second.
3. Repeat step 2 with scaling the analog voltage readings to the range (0-5)V.
4. Repeat step 3 using a different baud rate.
Fig. 1
Discussion:
Design and write the program for an Arduino based system to read two analog
signals continuously. Pin 13 is ON when Sig1>Sig2. Otherwise, pin 13 is OFF.
7
THIRD Load Driving MICROCONTROLLER
STAGE Experiment Number (6) LABORATORY
Objective:
To learn how to interface loads to the Arduino board.
Equipment:
Arduino UNO board
npn transistor
DC motor
DC power supply
Theory:
When the load requires an amount of current greater than that can be
supplied by the output pins of the Arduino card, then a driving circuit must
be used.
The driving circuit connects/disconnects the load to an external power
supply under the control of an Arduino output pin.
This switching action is implemented by using a transistor or a relay.
Procedure:
1. Connect the circuit of Fig.1.
2. Write a program to turn the CD motor ON and OFF every 1 second.
3. Connect the circuit of Fig. 2, and repeat step 2.
Fig. 1 Fig. 2
Discussion:
1. Compare between using transistors and relays from the driving capability,
size, and cost points of view.
2. Design and write a program for an Arduino based system that controls the
ON/OFF switching of an AC lamp using a switch.
8
THIRD Analog Output (PWM) MICROCONTROLLER
STAGE Experiment Number (7) LABORATORY
Objective:
To learn how to implement analog output from the Arduino board in the form of
Pulse Width Modulation (PWM).
Theory:
9
THIRD Light Sensor MICROCONTROLLER
STAGE Experiment Number (8) LABORATORY
Fig. 1
Discussion:
Design and write a program for an Arduino based system to automatically control
the switching of a house outer lighting using a photo resistance. Assume that
there are five AC lamps.
11
THIRD LCD Interfacing MICROCONTROLLER
STAGE Experiment Number (9) LABORATORY
Objective: To learn how to interface a 2x16 LCD in 4-bit mode to the Arduino
board.
Theory:
The LCDs have a parallel interface, meaning that the microcontroller has to
manipulate several interface pins at once to control the display.
Instructions:
(lcd.begin,lcd.print,lcd.setCursor, lcd.clear, lcd.autoscroll, lcd.blink,
lcd.scroll). See the IDE examples.
Equipment:
Arduino Board
LCD Screen (compatible with Hitachi HD44780 driver)
10k Potentiometer
breadboard
Procedure:
1. Connect the circuit of Fig.1 with the following details:
To wire your LCD screen to your Arduino, connect the following pins:
LCD RS pin to digital pin 12
LCD Enable pin to digital pin 11
LCD D4 pin to digital pin 5
LCD D5 pin to digital pin 4
LCD D6 pin to digital pin 3
LCD D7 pin to digital pin 2
Additionally, wire a 10K pot to +5V and GND, with it's wiper (output) to LCD screens
VO pin (pin3).
LCD LED+ to +5V and LCD LED- to GND.
11
Fig. 1
12
THIRD Heat Sensor MICROCONTROLLER
STAGE Experiment Number (10) LABORATORY
Objective: To learn how to interface a heat sensor to the Arduino board and
display its reading on an LCD.
Theory:
The LM35 is a low voltage IC which uses approximately +5VDC of power. This is
ideal because the Arduino's power pin gives out 5V of power. The IC has just 3
pins, 2 for the power supply and one for the analog output.
Equipment:
Arduino UNO board
LM35 heat sensor
resistances
LCD
Procedure:
1. Connect the circuit of Fig.1.
2. Write a program to read the analog input
from the sensor, convert it to temperature
in Celsius degrees, and display it on the LCD,
all every 100 ms.
Fig. 1
Discussion:
Design and write a program for a modification to the system of Fig. 1. Such that,
when the temperature is greater than 26 oC, then a fan is turned ON. Otherwise,
the fan is turned OFF.
13
THIRD Interrupts MICROCONTROLLER
STAGE Experiment Number (11) LABORATORY
Objective: To learn how to use the external interrupts of the Arduino board.
Theory:
The processor at the heart of any Arduino has two different kinds of interrupts:
“external”, and “pin change”. There are only two external interrupt pins on the
ATmega168/328 (ie, in the Arduino Uno/Nano/Duemilanove), INT0 and INT1, and
they are mapped to Arduino pins 2 and 3. These interrupts can be set to trigger
on RISING or FALLING signal edges, or on low level. The triggers are interpreted by
hardware, and the interrupt is very fast. The Arduino Mega has a few more
external interrupt pins available. The advantage of hardware interrupts is the CPU
doesn't waste most of its time "polling" or constantly checking the status of an IO
pin.
There are four available functions for controlling interrupts with Arduino:
attachInterrupt(), detachInterrupt(), interrupts(), and noInterrupts(). We shall
explore all four functions.
#define LED 9
volatile byte state = LOW;
void setup() {
pinMode(LED, OUTPUT);
attachInterrupt(0, toggle, RISING);
}
void loop() {
digitalWrite(LED, state);
}
void toggle() {
state = !state;
}
The label "LED" to the compiler is defined as the number 9. So digitalWrite(LED, HIGH)
is the same as digitalWrite(9, HIGH) which is the same as digitalWrite(LED, 1), etc.
14
The second line defines the variable "state" as both a byte (8-bit variable) and "volatile".
This differs from the usual byte, integer, or float in how the system and compiler use it. If
being used as an interrupt routine variable be it byte, float, etc. is must have a "volatile"
declaration. The variable is set to 0 - in reality we are using only one of the eight bits, bit
0.
In setup() we come to the function attachInterrupt(interrupt, function, mode) where
"interrupt" is the interrupt number 0 to 5; "function" is known as the interrupt service
routine or ISR a function address pointed to by the interrupt vector location; "mode"
configures the hardware electrical characteristics for an interrupt. This is done internally
by the compiler and hidden from the user.
So in this case of attachInterrupt(0, toggle, FALLING) zero corresponds to interrupt 0
on DP2, toggle() is the ISR routine at the bottom of the program, and RISING means
when an electrical signal goes from 0V to 5V the toggle() ISR performs its function -
what started as state = LOW is now state = HIGH and vise-versa. The right "}" is
considered the "return" command - in fact "return;" plased after "state = !state;" is
ignored and won't produce an error.
In other words loop() will simply keep writing the variable "state" to the LED on DP9,
and on an interrupt caused by pressing SW0 will halt, save address counter, jump to
ISR toggle(), will come back where it stopped and continue.
Assuming the LED1 is off press SW0 and the LED will come on, release nothing
happens. Press again and the LED is off. What toggle() really does is a bitwise XOR of
1; state = !state is the same as state = state ^ 1.
With attachInterrupt(interrupt, function, mode) there are four "mode" declarations
that defines when the interrupt should be triggered and are predefined as:
RISING to trigger when the pin goes from low to high - as wired above press switch and
LED state will toggle.
FALLING for when the pin goes from high to low - press switch nothing, release switch
LED state will toggle.
CHANGE to trigger the interrupt whenever the pin changes value - press switch LED
state will toggle, release switch LED will toggle again.
LOW to trigger the interrupt whenever the pin is low - sort of like FALLING but erratic -
don't use.
Example 2:
#define LED1 9
#define LED2 10
#define SW1 2
#define SW2 3
15
void setup() {
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
digitalWrite(LED1, 0); // LED off
digitalWrite(LED2, 0); // LED off
pinMode(SW1, INPUT);
pinMode(SW2, INPUT);
attachInterrupt(0, ISR0, FALLING);
// interrupt 0 digital pin 2 connected SW0
attachInterrupt(1, ISR1, RISING);
// interrupt 1 digital pin 3 connected SW1
}
void loop() {
// do nothing
}
void ISR1() {
toggle(LED2);
}
The above program introduces several new concepts. There are no "volatile" variables
defined or needed. Both INTR0 (DP2) and INTR1 (DP3) both have their own separate
ISR routines ISR0() and ISR1() respectively. There is a separate non-interrupt
subroutine toggle().
In this case either ISR routines will simply call toggle() and will let toggle() do the work.
Certain functions such as delay() won't work inside a ISR function so it's a good idea to
use separate subroutines as long as they are not overly complex or time consuming -
another interrupt rolling onto an ISR called subroutine that hasn't finished could be
interesting.
In addition functions that use interrupts such as delay() will not work if called from an
ISR routine. For example the modified version of toggle() that follows won't work if
called by an ISR but works fine if called from loop() or another non-interrupt subroutine.
The selected LED will come on for two seconds then go off.
The ISR routine maintains control of the interrupts until it's finished and executes a
"return" from interrupt command.
16
pinState = pinState ^ 1;
digitalWrite(pinNum, pinState);
} // last brace is understood as return.
The real solution is not to use the Arduino delay() function but write your own. the
delayMicroceconds() doesn't require the use of interrupts but is limited to about 16,000 -
1000uSec. = 1mSec.; 1,000,000uSec. - 1 Sec. Simply use a for loop:
void myDelay(int x) {
for(int i=0; i<=x; i++)
{
delayMicroseconds(1000);
}
}
Example 3:
void setup() {
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
digitalWrite(LED1, 0); // LED off
digitalWrite(LED2, 0); // LED off
pinMode(SW1, INPUT);
pinMode(SW2, INPUT);
attachInterrupt(0, ISR0, FALLING);
// interrupt 0 digital pin 2 connected SW0
attachInterrupt(1, ISR1, RISING);
// interrupt 1 digital pin 3 connected SW1
}
void loop() {
17
} // end loop
void myDelay(int x) {
for(int i=0; i<=x; i++)
{
delayMicroseconds(1000);
}
}
void ISR0()
{
toggle(LED1);
}
void ISR1(){
detachInterrupt(0); // not needed!
toggle(LED2);
myDelay(5000); // 5 sec.
toggle(LED2);
attachInterrupt(0, ISR0, RISING); // not needed
flag1 = 0; // clear flag
}
In the above sketch press SW0 and LED1 will toggle. Press SW1 and LED2 will turn on
for 5 seconds at the same time ISR1 disconnects INTR0. Press SW1 during this time
and when the 5 seconds are up LED1 will then toggle. If SW0 is pressed more than
once during that 5 seconds only one will be remembered because flag0 will be
interpreted as HIGH only once even if the count is greater than 1.
Example 4:
#define LED1 9
#define LED2 10
#define SW1 2
#define SW2 3
volatile byte flag0 = LOW; // declare IRQ flag
volatile byte flag1 = LOW; // declare IRQ flag
void setup() {
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
digitalWrite(LED1, 0); // LED off
18
digitalWrite(LED2, 0); // LED off
pinMode(SW1, INPUT);
pinMode(SW2, INPUT);
attachInterrupt(0, ISR0, FALLING);
attachInterrupt(1, ISR1, RISING);
}
void loop() {
noInterrupts();
toggle(LED2);
myDelay(2000);
toggle(LED2);
myDelay(2000);
interrupts();
// no other interrupt work except from here to begin loop.
} // end loop
void myDelay(int x) {
for(unsigned int i=0; i<=x; i++)
{
delayMicroseconds(1000);
}
}
void ISR0()
{
toggle(LED1);
}
void ISR1(){
toggle(LED2);
myDelay(5000); // 5 sec.
toggle(LED2);
flag1 = 0; // clear flag
}
Equipment:
Arduino UNO board
LDEs
Switches and resistances
Procedure:
1. Connect two switches to the Arduino card. SW0 to pin 2 and SW1 to pin 3.
2. Write and execute the above examples.
19
Solved lab experiments
digitalWrite(13,LOW);
void setup()
{ else
pinMode(8,OUTPUT);
pinMode(9,OUTPUT); digitalWrite(13,HIGH);
} delay(100);
}
void loop() =========================
{ void setup()
digitalWrite(8,HIGH); {
digitalWrite(9,LOW); pinMode(13,OUTPUT);
delay(1000); pinMode(2,INPUT);
digitalWrite(8,LOW); pinMode(4,INPUT);
digitalWrite(9,HIGH);
delay(1000); }
} void loop()
{
int sw1=digitalRead(2);
int sw2=digitalRead(4);
if (sw1==1)
digitalWrite(13,LOW);
else if (sw2==1)
digitalWrite(13,HIGH);
delay(100);
}
1
Exp. No. 3 Exp. No. 4
void setup()
void setup() { Serial.begin(9600); }
{ void loop()
pinMode(9,OUTPUT); {
pinMode(10,OUTPUT); Serial.println(427);
pinMode(2,INPUT); delay(1000);
pinMode(3,INPUT); Serial.println(61);
pinMode(4,INPUT); delay(1000);
} Serial.println(92);
delay(1000);
void loop() Serial.println('a');
{ delay(1000);
int sw1=digitalRead(2); Serial.println('b');
int sw2=digitalRead(3); delay(1000);
int sw3=digitalRead(4); Serial.println('A');
if (sw1==1 && sw2==0 && sw3==0) delay(1000);
{digitalWrite(9,LOW); Serial.println('C');
digitalWrite(10,LOW);} delay(1000);
else if (sw1==0 && sw2==1 && sw3==0) Serial.println('g');
{digitalWrite(9,LOW); delay(1000);
digitalWrite(10,HIGH);} Serial.println("Hello");
else if (sw1==0 && sw2==0 && sw3==1) delay(1000);
{digitalWrite(9,HIGH); }
digitalWrite(10,LOW);} ====================
else void setup()
{digitalWrite(9,HIGH); {pinMode(13,OUTPUT);
digitalWrite(10,HIGH);} Serial.begin(9600);
delay(100); // for stability }
} void loop()
{
if (Serial.available()>0)
{
char x=Serial.read();
Serial.println(x);
if (x=='1')
{digitalWrite(13,HIGH);}
else
{digitalWrite(13,LOW);}
}
delay(100);
}
2
Exp. No. 5 Exp. No. 7
3
Exp. No. 9 // (note: line 1 is the second row, since
counting begins with 0):
// include the library code: lcd.setCursor(4, 0);
#include <LiquidCrystal.h> x=x+1;
// initialize the library with the numbers of lcd.print(x);
the interface pins delay(1000);
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); }
void setup() {
// set up the LCD's number of columns Exp. No. 10
and rows:
lcd.begin(16, 2); // include the library code:
} #include <LiquidCrystal.h>
void loop() { // initialize the library with the numbers of
// set the cursor to column 0, line 1 the interface pins
// (note: line 1 is the second row, since LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
counting begins with 0): float x;
lcd.setCursor(0, 0); void setup() {
// set up the LCD's number of columns
// Print a message to the LCD. and rows:
lcd.print("Arduino LAB"); lcd.begin(16, 2);
lcd.setCursor(0, 0);
// Turn off the display: lcd.print("temp=");
lcd.noDisplay(); }
delay(1000); void loop() {
// Turn on the display: // set the cursor to column 0, line 1
lcd.display(); // (note: line 1 is the second row, since
delay(1000); counting begins with 0):
}
======================= lcd.setCursor(6, 0);
// include the library code: x=analogRead(0);
#include <LiquidCrystal.h> x=(x*5.0);
// initialize the library with the numbers of lcd.print(x);
the interface pins delay(100);
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); }
int x=0;
void setup() {
// set up the LCD's number of columns
and rows:
lcd.begin(16, 2);
}
void loop() {
// set the cursor to column 0, line 1