Sukkur IBA University
Department of Electrical Engineering
ESE-312: Embedded Systems Lab
Handout # 01: Introduction to Arduino UNO, understanding
Digital I/O Ports, interfacing LED and potentiometer with
Arduino UNO Board
Instructor: Dr. Ghulam Abbas Lashari
Submission Profile
Name: Submission date (dd/mm/yy):
Note: Submit this lab hand-out on LMS/e-learning before the next lab with attached solved
activities and exercises.
P a g e 1 | 13
CLO3: Develop solutions with embedded systems (PLO-3, Psy-3)
CLO4: Execute programs containing arithmetic, logic, loop and/or program control instructions
(PLO-4, Cog-4, Psy-4)
Lab Performance Rubrics
CLO Poor Average Good
Criteria Domain- (0 – 33%) (34 – 66 %) (67 – 100 %)
level
No specifications, Some specifications, Specifications,
Development of parameters and parameters and parameters and
System CLO-3 constraints of
constraints of system constraints of system
Psy-3 system
(10 %) development are development are
development are
present partially present
present
Student was Student needed Student has made
unable to make guidance to make correct
Equipment correct correct
CLO-3 equipment/component
connection equipment/compo equipment/componen
Psy-3 connections as per
(10 %) nent connections t connections as per
as per standard standard circuit standard circuit
circuit diagrams diagrams diagrams.
Student can detect the Student has ability to
Troubleshooting CLO-3 Student was unable
error but unable to detect and correct the
(10 %) Psy-3 to detect the error
correct the error errors
Operability of The system could The system partially System perfectly
System CLO-3 not carry out the operates to carry out
operates to perform
Psy-3 specific task
(10 %) the required task the required task
required
The code is
The code has The code is correct
Execution of several syntax completely functional
Programs CLO-4 with regard to syntax and responds correctly
Psy-4 errors. Important
but required output is
(30 %) parts of code are producing the correct
not correct.
missing outputs
Analyses problem Analyses problem
Problem Analysis Partial analysis; requirements
correctly with few
& Instruction CLO-4 instructions are gaps; selects mostly thoroughly, identifies
Application Cog-4 applied inconsistently appropriate correct instructions, and
with weak logical flow. instructions and organizes them logically
(30 %)
structures. into program flow.
P a g e 2 | 13
Lab Learning Objectives:
After completing this session, student should be able to:
➢ Understand the layout of Arduino UNO board
➢ Understand Arduino IDE, and how to write, compile, and upload a code to Arduino
➢ Deal with LED using Arduino digital output pins
➢ Understand the difference between Pull-up and Pull-down resistors configurations
➢ Interface potentiometer
Lab Hardware and Software Required:
1. Desktop/Laptop Computer
2. Arduino IDE
3. Arduino UNO
4. Potentiometers, LEDs
5. Breadboard, Resistors (330 Ω) and Connecting wires
Theory:
Arduino
Arduino is an open source platform based around programmable development boards that
can be integrated into a range of simple and complex projects. The Arduino family consists
of different types of development boards, with the most common being the Arduino UNO.
An Arduino board contains a microcontroller which can be programmed to sense and control
devices in the physical world. The microcontroller is able to interact with a large variety of
components such as LEDs, motors and displays. Because of its flexibility and sustainability,
Arduino has become a popular prototyping development board which is widely used across
the world.
Arduino UNO
Arduino Uno is a microcontroller board, developed by [Link], based on the Atmega328
microcontroller and is marked as the first Arduino board developed (UNO means "one" in
Italian). Arduino UNO is a very valuable addition in electronics that consists of a USB interface,
14 digital I/O pins and an Atmega328 microcontroller. It also supports 3 communication
protocols named Serial, I2C and SPI protocol.
The software used for writing, compiling & uploading code to Arduino boards is called Arduino
IDE (Integrated Development Environment), which is free to download from Arduino Official
Site.
Arduino UNO has an operating voltage of 5V while the input voltage may vary from 7V to 12V.
Arduino UNO has a maximum current rating of 40mA, so the load shouldn't exceed this
current rating or you may harm the board. It comes with a crystal oscillator of 16MHz, which
is its operating frequency. Arduino Uno Pinout consists of 14 digital pins starting from D0 to
D13. It also has 6 analog pins starting from A0 to A5. It also has 1 Reset Pin, which is used to
reset the board programmatically. In order to reset the board, we need to make this pin LOW. It
also has 6 Power Pins, which provide different voltage levels. Out of 14 digital pins, 6 pins are
P a g e 3 | 13
used for generating PWM pulses of 8-Bit resolution. PWM pins in Arduino UNO are D3, D5,
D6, D9, D10 and D11.
Arduino UNO comes with 3 types of memories associated with it, named:
o Flash Memory: 32KB
o SRAM: 2KB
o EEPROM: 1KB
Arduino UNO supports 3 types of communication protocols, used for interfacing with third-
party peripherals, named:
o Serial Protocol
o I2C Protocol
o SPI Protocol
Apart from USB, a battery or AC to DC adopter can also be used to power the board.
Fig 4.1: Arduino UNO Board
P a g e 4 | 13
Fig 4.2: ATmega328 Pinout
Basic Structure of Arduino code
Arduino code (also called Arduino sketch) includes two main parts:
1. Setup code
2. Loop code
1. Setup code
➢ Code in setup() function
➢ Executed right after power-up or reset
➢ Executed only one time
➢ Used to initialize variables, pin modes, and start using libraries
2. Loop code
➢ Code in loop() function
➢ Executed right after setup code
➢ Executed repeatedly (infinitely)
➢ Used to do the main task of application
Serial Monitor
Serial Monitor is one of the tools in Arduino IDE. It is used for two purposes:
1. Arduino to PC: Receives data from Arduino and display data on screen. This is usually
used for debugging and monitoring.
2. PC to Arduino: Sends data (command) from PC to Arduino
Data is exchanged between Serial Monitor and Arduino via USB cable, which is also used to upload
the code to Arduino. Therefore, to use Serial Monitor, we must connect Arduino and PC via this
cable.
Example:
Hello World
o Connect Arduino UNO to PC via USB cable
o Open Arduino IDE
o Select Arduino Board
P a g e 5 | 13
o Select Arduino Serial Port (number may be different)
o Type the following Code
P a g e 6 | 13
o Click Upload button on Arduino IDE to upload code to Arduino
o Open Serial Monitor by Clicking the Serial Monitor Icon
o See the result on Serial Monitor
P a g e 7 | 13
Lab Activities:
Lab Activity # 1: LED Blink
LED Pinout
LED includes two pins:
o Cathode (-) pin needs to be connected to GND (0V)
o Anode (+) pin is used to control LED’s state
Fig 4.3: LED Pinout
How it Works
After connecting the cathode (-) to GND.
o If connecting GND to the anode (+), LED is OFF.
o If connecting VCC to the anode (+), LED is ON.
Fig 4.4: LED Working mechanism
Arduino LED
When an Arduino pin is configured as a digital output, the pin’s voltage can be programmatically
set to GND or VCC value.
By connecting the Arduino’s pin to LED’s anode (+) pin (via a resistor), we can programmatically
control LED’s state.
P a g e 8 | 13
Fig 4.5: Arduino - LED Circuit Diagram
How to program
o Configure an Arduino’s pin to the digital output mode by using pinMode() function
pinMode(9, OUTPUT);
o Program the pin to GND to turn OFF led by using digitalWrite() function
digitalWrite(9, LOW);
o Program the pin to VCC to turn ON led by using digitalWrite() function
digitalWrite(9, HIGH);
Code
void setup() {
// initialize digital pin 9 as an output.
pinMode(9, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
P a g e 9 | 13
Lab Activities:
Lab Activity # 2: Reading and printing Potentiometer Serial Data
Potentiometer
Rotary potentiometer (also called rotary angle sensor) is used to manually adjust the value of
something (e.g. volume of the stereo, the brightness of lamp, zoom level of oscilloscope etc).
Pinout
Potentiometer usually has 3 pins:
o GND pin needs to be connected to GND (0V)
o VCC pin needs to be connected to VCC (5V or 3.3 V)
o Output pin outputs the voltage to Arduino’s input pin
Fig 4.6: Potentiometer Pinout
How it Works
The shaft of the potentiometer is rotatable from 0° (nearest by GND) to an upper bound angle
(nearest by VCC pin), called ANGLE_MAX.
The voltage at the output pin ranges from GND’s voltage to VCC’s voltage. The output voltage is
in direct proportion to the rotated angle of the shaft.
o If the angle is 0°, output pin’s voltage is 0V
o If the angle is ANGLE_MAX, output pin’s voltage is VCC’s voltage
o If the angle is in between 0° and ANGLE_MAX,
output_voltage = angle x VCC / ANGLE_MAX
P a g e 10 | 13
Arduino- Rotary Potentiometer
Arduino’s pin A0 to A5 can work as analog input. The analog input pin converts the voltage
(between 0v and VCC) into integer values (between 0 and 1023), called ADC value or analog value.
By connecting an output pin of the potentiometer to an analog input pin, we can read the analog
value from pin, and then converts it to a meaningful value.
The value Arduino get is NOT angle, NOT voltage. It is integer value ranges from 0 to 1023.
After getting the integer value from the analog input pin, we rescale this value into another value.
Let’s see the use cases
Use Cases
o Rescale to potentiometer’s angle
o Rescale to potentiometer’s voltage
o Rescale to the controllable value (e.g volume of stereo, brightness, speed of DC motor
etc).This is the most common use case.
Fig 4.7: Arduino – Potentiometer Circuit Diagram
How to program for potentiometer
Read the value from an input pin, which connected to the output pin of the potentiometer by using
analogRead() function.
P a g e 11 | 13
analogValue = analogRead (A0);
Rescale to the potentiometer’s angle by using map() function.
angle = map (analogValue, 0, 1023, 0, ANGLE_MAX);
Rescale to the potentiometer’s voltage.
voltage = map (analogValue, 0, 1023, 0, VCC);
Rescale to the controllable value (e.g volume of stereo, brightness, speed of DC motor etc)
Value = map (analogValue, 0, 1023, VALUE_MIN, VALUE_MAX);
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
[Link](9600);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin A0:
int analogValue = analogRead(A0);
// Rescale to potentiometer's voltage (from 0V to 5V):
int voltage = Map(analogValue, 0, 1023, 0, 5);
// print out the value you read:
[Link]("Analog: ");
[Link](analogValue);
[Link](", Voltage: ");
[Link](voltage);
delay(1000);
}
P a g e 12 | 13
Lab Exercise:
1. Develop a system to demonstrate a street traffic light system.
First state Second state Third state
Duration:2 sec Duration:4 sec. Duration:6 sec.
Your hardware might look like as:
2. Develop a system that changes the brightness of LED according to the potentiometer’s output
value.
P a g e 13 | 13