ARDUINO PROGRAMMING AND
INTERFACING FUNDAMENTAL
HANDBOOK
Prepared by
Arduino Team PTSB
HUSAINI AZA MOHD ADAM
(Head of Department)
MAHDZIR BIN JAMIAAN
ASMAYUZIE BINTI AHMAD
ZARINA ISMAIL
AHMAD BIN ZULKIFLI
AZMAN SALLEH
ISHAM SAHAR ISMAIL
NURAKHYAR NORDIN
MOHAMAD ZHAHIR MOHAMAD ZULKUFLI
FEBRUARY 4, 2017
CENTRE OF TEKNOLOGY, ELECTRICAL ENGINEERING
Politeknik Tuanku Sultanah Bahiyah
Table of Content
LESSON 1 : ARDUINO HARDWARE& SOFTWARE ................................................ 1
1.1. Introduction to Arduino Hardware ......................................................... 1
1.1.1. Arduino Pins .................................................................................... 1
1.2. Introduction to Arduino Software .......................................................... 3
1.2.1. Install the software .......................................................................... 3
1.2.2. The Integrated Development Environment (IDE) ............................. 3
LESSON 2 : Experiment with INPUT and OUTPUT (I/O) ...................................... 4
2.1. Task 1: Working with LED as an OUTPUT ............................................... 4
2.1.1. Overview ......................................................................................... 4
2.1.2. Connection Diagram ........................................................................ 4
2.1.3. Code ................................................................................................ 5
2.1.4. Exercise ........................................................................................... 7
2.2. Task 2: Working with active buzzer ....................................................... 8
2.2.1. Overview ......................................................................................... 8
2.2.2. Connection Diagram ........................................................................ 8
2.2.3. Code ................................................................................................ 9
2.3. Task3: Working with LED and Switch – INPUT & OUTPUT .................... 11
2.3.1. Overview ....................................................................................... 11
2.3.2. Connection Diagram ...................................................................... 11
2.3.3. Code .............................................................................................. 12
2.3.4. Exercise ......................................................................................... 14
2.4. Task 4: Using Arduino Serial Function .................................................. 15
2.4.1. Overview ....................................................................................... 15
2.4.2. Connection Diagram ...................................................................... 15
2.4.3. Code .............................................................................................. 16
2.4.4. Exercise ......................................................................................... 18
2.5. Task 5: Working with LCD Display ........................................................ 20
i
2.5.1. Overview ....................................................................................... 20
2.5.2. Connection Diagram ...................................................................... 20
2.5.3. Code .............................................................................................. 21
LESSON 3 : EXPERIMENT WITH ANALOG SIGNAL ............................................. 22
3.1. Task 6: Working with potentiometers .................................................. 22
3.1.1. Overview ....................................................................................... 22
3.1.2. Code .............................................................................................. 23
3.1.3. Exercise ......................................................................................... 25
3.2. Task 7: Working with LM35 temperature sensor ................................. 26
3.2.1. Overview ....................................................................................... 26
3.2.2. Connection Diagram ...................................................................... 26
3.2.3. Code .............................................................................................. 27
3.3. Task 8: Working with LDR sensor ......................................................... 29
3.3.1. Overview ....................................................................................... 29
3.3.2. Connection Diagram ...................................................................... 29
3.3.3. Code .............................................................................................. 30
Bibliography ..................................................................................................... 32
ii
Table of Figures
Figure 1-1: Arduino Board.................................................................................. 1
Figure 1-2: Arduino IDE ...................................................................................... 3
Figure 2-1: LED to Arduino Connection .............................................................. 4
Figure 2-2: Active Buzzer ................................................................................... 8
Figure 2-3: Connecting buzzer to Arduino .......................................................... 8
Figure 2-4: Switch Connection ......................................................................... 11
Figure 2-5: Switch and LED to Arduino............................................................. 11
Figure 2-6: Schematic diagram Switch and LED to Arduino .............................. 12
Figure 2-7: Push button arrangement .............................................................. 12
Figure 2-8: Debounce from example................................................................ 14
Figure 2-9: Serial monitor ................................................................................ 15
Figure 2-10: Schematic Diagram of Serial Monitor........................................... 16
Figure 2-11: Liquid Crystal Display (LCD) .......................................................... 20
Figure 2-12: LCD Connection............................................................................ 21
Figure 3-2: Types of potentiometer ................................................................. 22
Figure 3-1: Potentiometer pins ........................................................................ 22
Figure 3-3: Potentiometer connection diagram ............................................... 23
Figure 3-4: LM35 .............................................................................................. 26
Figure 3-5: LM35 symbol ................................................................................. 26
Figure 3-6: LM 35 connection .......................................................................... 26
Figure 3-7: LDR ................................................................................................ 29
Figure 3-8: LDR Connection ............................................................................. 29
iii
LESSON 1 : ARDUINO HARDWARE& SOFTWARE
1.1. Introduction to Arduino Hardware
Arduino is an open-source platform used for building electronics projects. Arduino consists of both a
physical programmable circuit board (often referred to as a microcontroller) and a piece of software,
or IDE (Integrated Development Environment) that runs on your computer, used to write and upload
computer code to the physical board.
Arduino is open source hardware Platform
Arduino does not need a programmer hardware
Arduino IDE an open-source and easy-to-use programming tool uses a simplified version of
C++
Arduino provides a standard form factor that breaks out the functions of the micro-
controller into a more accessible package.
Figure 1-1: Arduino Board
1.1.1. Arduino Pins
The pins on your Arduino are the places where you connect wires to construct a circuit (probably in
conjunction with a breadboard and some wire. They usually have black plastic ‘headers’ that allow
you to just plug a wire right into the board. The Arduino has several different kinds of pins, each of
which is label LED on the board and used for different functions.
GND: Ground Pin. There are several GND pins on the Arduino.
5V & 3.3V: the 5V pin supplies 5 volts of power, and the 3.3V pin supplies 3.3 volts.
1
Analog (6): The areas of pins under the ‘Analog In’ label (A0 through A5 on the UNO) are
Analog In pins. These pins can read the signal from an analogue sensor (like a temperature
sensor) and convert it into a digital value that we can read.
Digital (13): Across from the analogue pins are the digital pins (0 through 13 on the UNO).
These pins can be used for both digital input (like telling if a button is pushed) and digital
output (like powering an LED).
PWM (8): You may have noticed the tilde (~) next to some of the digital pins (3, 5, 6, 9, 10,
and 11 on the UNO). These pins act as normal digital pins, but can also be used for Pulse-
Width Modulation (PWM).
AREF (9): Stands for Analog Reference. It is use to set an external reference voltage
(between 0 and 5 Volts) as the upper limit for the analogue input pins.
Serial: 0 (RX) and 1 (TX). Used to receive (RX) and transmit (TX) TTL serial data.
External Interrupts: 2 and 3. These pins can be configured to trigger an interrupt on a low
value, a rising or falling edge, or a change in value
SPI: 10 (SS), 11 (MOSI), 12 (MISO), 13 (SCK). These pins support SPI communication, which,
although provided by the underlying hardware, is not currently included in the Arduino
language.
LED: 13. There is a built-in LED connected to digital pin 13. When the pin is HIGH value, the
LED is on, when the pin is LOW, it's off.
I2C: 4 (SDA) and 5 (SCL). Support I2C (TWI) communication using the Wire
library (documentation on the Wiring website).
Although all the ports have only two states, HIGH / LOW, some of them (marked by tildes ~) can be
switched on and off very rapidly. Although the actual voltage can only take on two values, by varying
the percentage of time in each state anywhere from 0-5V can be achieved.
2
1.2. Introduction to Arduino Software
1.2.1. Install the software
You’ll need to download the Arduino Software package for your operating system from the Arduino
web site https://www.arduino.cc/en/Main/Software.
1.2.2. The Integrated Development Environment (IDE)
You use the Arduino IDE on your computer (picture following) to create, open, and change sketches
(Arduino calls programs “sketches”). Sketches define what the board will do. You can either use the
buttons along the top of the IDE or the menu items.
Figure 1-2: Arduino IDE
3
LESSON 2 : Experiment with INPUT and OUTPUT (I/O)
2.1. Task 1: Working with LED as an OUTPUT
What you will need Specification - LED
1 x Arduino UNO R3 As LED forward voltages
1 x Small size Breadboard Red and green: 2 volts
1 x Red LED
1 x 220Ω Resistor LED current
Jumper Wires 20mA will work for most regular LED. Super
bright LED can go from 30mA up to several
amps.
2.1.1. Overview
This is your first sketch, your task is to light up a LED and make it blink. Next you are given tasks to
code playing with multiple LEDs.
2.1.2. Connection Diagram
Connect everything as shown in Error! Reference source not found.. Make sure that your LED is
connected the right way with the longer leg connected to Digital Pin 10.
Figure 2-1: LED to Arduino Connection
4
2.1.3. Code
Open the Arduino IDE and start write following code,
Sketch #1
// -- Activity #1 –LED // -- Activity #1 –LED
void setup () { intLED1 = 13;
pinMode (13, OUTPUT);
} void setup () {
OR pinMode (LED1, OUTPUT);
void loop () { }
digitalWrite (13, HIGH);
} void loop () {
digitalWrite (LED1, HIGH);
)
NOTES
5
Sketch #2 – LED BLINK
// -- Activity #2 –LED BLINK
void setup () {
// initialize digital pin 13 as an output.
pinMode (13, OUTPUT);
}
//the loop function runs over and over again forever
void loop () {
digitalWrite (13, HIGH);
delay (1000);
digitalWrite (13, LOW);
delay (1000);
}
NOTES
6
You can compile your sketch by click on VERIFY button. Click UPLOAD button to download your
compiled code to Arduino.
2.1.4. Exercise
Connect multiple LED and do the exercise below;
a) Blink between two LEDs (D1& D2)
b) Light chaser with four LEDs
c) Combine exercise (a) and (b) in one code.
Exercise Connection diagram Code
Blink
between 2
LED
Light chaser
with four
LEDs
Combine
exercise (a)
and (b) in
one code
7
2.2. Task 2: Working with active buzzer
What you will need Specification - Active Buzzer
1 x Arduino UNO R3
1 x Small size Breadboard
Active Buzzer
Jumper Wires
Figure 2-2: Active Buzzer
2.2.1. Overview
A buzzer or beeper is an audio signalling device, which may be mechanical, electromechanical, or
piezoelectric. Typical uses of buzzers and beepers include alarm devices, timers and confirmation of
user input such as a mouse click or keystroke.
Active buzzer is easy use, just apply voltage to the buzzer and it makes sound. The active buzzer has
built-in oscillating source, so it will beep as long as it is electrified, but it can only beep with a fixed
frequency. Disadvantage is that you can't determine the frequency of the sounds, for this you need a
passive buzzer.
2.2.2. Connection Diagram
Figure 2-3: Connecting buzzer to Arduino
8
2.2.3. Code
Sketch #1 –OUTPUT digital ( On and OFF)
1.
2. int buzzer = 13;
3. void setup () {
4. pinMode (buzzer, OUTPUT);
5. }
6.
void loop () {
digitalWrite (buzzer, HIGH);
delay (1000);
digitalWrite (buzzer, LOW);
delay (1000);
}
NOTES
9
Sketch #2 – OUTPUT with analog ( PWM)
7.
8. intspeakerPin = 8;
9. void setup ()
{
pinMode (speakerPin, OUTPUT); //Define pin 8 as output
void loop () {
digitalWrite (speakerPin, HIGH);
delay (50); // Pin 8 on for 0.5sec
digitalWrite (speakerPin, LOW);
delay (50); // Pin 8 off for 0.5sec
}
10.
NOTES
10
2.3. Task3: Working with LED and Switch – INPUT & OUTPUT
What you will need Specification - LED
1 x Small size Breadboard As LED forward voltages:
4 x Red LED Red and green: 2 volts
4 x 220Ω Resistor Blue and white: 3.0 - 3.5 volts
2 x 220Ω Resistor LED current:
2 x Tactile Switch Push Button 20mA will work for most regular LEDs.Super
Jumper Wires brightLEDs can go from 30mA up to several
amps.
2.3.1. Overview
The easiest way to change the voltage from 0 to 5 V (or vice versa) is by using a button. A button is
just a switch that closes an electrical circuit.
Figure 2-4: Switch Connection
2.3.2. Connection Diagram
Figure 2-5: Switch and LED to Arduino
11
Figure 2-6: Schematic diagram Switch and LED to Arduino
Figure 2-7: Push button arrangement
2.3.3. Code
11. Sketch #1 - Turn on a LED when the button is pressed
void setup () {
pinMode (2, OUTPUT);
pinMode (8, INPUT);
}
void loop () {
intbuttonState = digitalRead (8);
if (buttonState == HIGH) {
digitalWrite (2, HIGH);
else {
digitalWrite (2, LOW);
}
}
12
NOTES
Sketch #2 –Turn on a LED when the button is pressed and let it on when the button is released
IntpinButton = 8;
IntLED = 2;
void setup () {
pinMode (LED, OUTPUT);
pinMode (PinButton, INPUT);
}
void loop () {
intstateButton = digitalRead (pinButton);
if (stateButton == 1) {
digitalWrite (LED, HIGH);
}
}
NOTES
13
2.3.4. Exercise
Use digital sensor instead of switch.
Try example Debounce
Figure 2-8: Debounce from example
Code Circuit Diagram
14
2.4. Task 4: Using Arduino Serial Function
What you will need Specification -
Computer
USB cable,
Arduino UNO
Arduino IDE
DuPont jumper wires
2.4.1. Overview
Serial is used for communication between the Arduino board and a computer or other devices. All
Arduino boards have at least one serial port (also known as a UART or USART): Serial. It
communicates on digital pins 0 (RX) and 1 (TX) as well as with the computer via USB. Thus, if you use
these functions, you cannot also use pins 0 and 1 for digital input or output.
We’ll learn how to use the Serial Library to communicate from the Arduino board back to the
computer over the USB port. Then we'll learn how to manipulate numbers and data.
2.4.2. Connection Diagram
Figure 2-9: Serial monitor
15
Figure 2-10: Schematic Diagram of Serial Monitor
2.4.3. Code
Open the Arduino IDE and start write following code,
Sketch #1 – Serial Monitor
void setup () {
Serial.begin (9600); //opens serial port, sets data rate to 9600 bps
Serial.println (“Kursus Arduino”);
}
void loop () {
}
Once the Arduino sketch has been uploaded to the Arduino. Open the Serial monitor, which looks
like a magnifying glass at the top right section of the Arduino IDE. Please note that you need to keep
the USB connected to the Arduino during this process, as the USB cable is your communication link
between your computer and the Arduino.
16
NOTES
Sketch #2 – Serial monitor with push button
void setup () {
pinMode (2, OUTPUT);
pinMode (8, INPUT);
Serial.begin (9600); //opens serial port, sets data rate to 9600 bps
}
void loop () {
intbuttonState = digitalRead (8);
Serial.print (“KeadaanSuisadalah logic : ”);
Serial.println (buttonState);
if (buttonState == HIGH) {
digitalWrite (2, HIGH);
Serial.println (“LEDmenyala ”);
}
else {
digitalWrite (2, LOW);
Serial.println (“LEDpadam ”);
}
delay (1000);
}
17
NOTES
2.4.4. Exercise
Sketch #2 – Serial with input from keyboard
void setup()
{
//start serial port at 9600 bps
Serial.begin(9600);
}
void loop()
{
If (Serial.available())
{
char ch = Serial.read();
if (ch==’A’) {
Serial.println(“Key A was pressed”);
Serial.println(“LED on”);
}
If (ch==’B’) {
Serial.println(“Key B was pressed”);
Serial.println(“LED blink”);
}
}
}
18
NOTES
TASK: Solve the problems below
(HINT: Refer Task 2.1, Task 2.3 and Task 2.4)
a) ON LED 1 using keypad ‘A’. Display “LED 1 ON” on the serial monitor.
b) ON LED 2 using push button. Display “LED 2 ON” on the serial monitor.
19
2.5. Task 5: Working with LCD Display
What you will need Specification - LCD
Arduino UNO Board
1 x Small size Breadboard
LCD 16 x 2 with female pin headers
5k ohm potentiometer
10k ohm resistor
DuPont jumper wires
2.5.1. Overview
Before wiring the LCD screen to your Arduino or Genuino board we suggest to solder a pin header
strip to the 14 (or 16) pin count connector of the LCD screen, as you can see in the image above. To
wire your LCD screen to your board, connect the following pins:
Figure 2-11: Liquid Crystal Display (LCD)
Additionally, wire a 5k potentiometer to +5V and GND, with its wiper (output) to LCD screens VO pin
(pin3). A 220 ohm resistor is used to power the backlight of the display, usually on pin 15 and 16 of
the LCD connector.
2.5.2. Connection Diagram
Connect everything as shown in figure below
LiquidCrystallcd (RS, E, 5, 4, 3, 2);
Arduino LCD Potentiometer
D12 LCD RS
D11 CLD E
D5 LCD D4
D4 LCD D5
D3 LCD D6
D2 LCD D7
R/W & VSS to GND GND
VDD to 5V 5V
VEE VEE
20
Figure 2-12: LCD Connection
2.5.3. Code
Open the Arduino IDE and start write following code,
Sketch #1 – LCD Hello world
//include the LCD Library code
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystallcd(12,11,5,4,3,2);
Void setup(){
//set up the LCD’s number of columns and rows
lcd.begin(16,2);
//print the message to the LCD
lcd.print(“Hello World!”);
}
void loop(){
//setthe cursor to column 0 and row 1
lcd.setCursor(0,1);
//print the number of seconds since reset
lcd.print(mills()/1000);
}
21
NOTES
LESSON 3 : EXPERIMENT WITH ANALOG SIGNAL
3.1. Task 6: Working with potentiometers
What you will need Specification
Arduino UNO Board :
Potentiometers
pin headers to solder to the LCD display pins
220 ohm resistor
DuPont jumper wires
breadboard
Figure 3-1:
Potentiometer pins
3.1.1. Overview
Figure 3-2: Types of potentiometer
22
Connect everything as shown in figure below,
Figure 3-3: Potentiometer connection diagram
3.1.2. Code
Open the Arduino IDE and start write following code,
Sketch #1 – basic VR
void setup()
{
Serial.begin(9600); // open serial port, set the baud rate to
9600 bps
}
void loop()
{
int val;
val=analogRead(0); //connect grayscale sensor to Analog 0
Serial.println(val,DEC);//print the value to serial
delay(100);
}
NOTES
23
Sketch #2 – using “if else”
void setup()
{
Serial.begin(9600); // open serial port, set the baud rate to 9600
bps
}
void loop()
{
intval;
val=analogRead(0); //connect grayscale sensor to Analog 0
Serial.println(val,DEC);//print the value to serial
if ( val> 512)
{
Serial.println(" Value besardari 2.5V");
}
else
Serial.println(" ");
}
NOTES
24
3.1.3. Exercise
Try to convert digital value to voltage
Use if—else to manipulate data condition.
o If Value > 500 send to serial to print “ data above 500”
o if value <500 send to serial to print “ data below 500”
TASK
25
3.2. Task 7: Working with LM35 temperature sensor
What you will need Specification
Arduino UNO R3 Board Can be used to detect ambient air
LM 35 temperature
pin headers to solder to the LCD display pins Sensitivity: 10mV per degree Celsius
hook-up wires Functional range: 0 degree Celsius to
10k potentiometer 100 degree Celsius
3.2.1. Overview
Figure 3-4: LM35
Figure 3-5: LM35 symbol
3.2.2. Connection Diagram
Figure 3-6: LM 35 connection
26
3.2.3. Code
Sketch #1 – LM35 Digital Value
void setup()
{
Serial.begin(9600);//Set Baud Rate to 9600 bps
}
void loop()
{
intval;
intdat;
val=analogRead(0);//Connect LM35 on Analog 0
Serial.print(val);
delay(500);
}
Sketch #2 – LM35 display temperature
void setup()
{
Serial.begin(9600);//Set Baud Rate to 9600 bps
}
void loop()
{
intval;
intdat;
val=analogRead(0);//Connect LM35 on Analog 0
dat=(500 * val) /1024;;
Serial.print("Temp:"); //Display the temperature on Serial monitor
Serial.print(dat);
Serial.println("C");
delay(500);
}
TASK: Display the temperature value on the LCD. GOOD LUCK!!
(Write your sketch below)
27
NOTES
28
3.3. Task 8: Working with LDR sensor
What you will need Specification - LED
Arduino UNO R3 As LED forward voltages:
1 x Small size Breadboard Red and green: 2 volts
LDR Blue and white: 3.0 - 3.5 volts
Resistor 10K LED current:
20mA will work for most regular LED. Super
bright LED can go from 30mA up to several
amps.
3.3.1. Overview
LDR stands for Light dependant resistor. LDR is made of a semiconductor material doped with a
small percentage of a valency 5 material (commonly Arsenic), to make it an "N" material. Another
word for LDR is photoresistor. The resistance of LDR decreases with increase in the intensity of light.
LDR works in the similar manner as any other analogue device would work.LDRs are non-polarized.
That means you can connect it either way.
Figure 3-7: LDR
3.3.2. Connection Diagram
Figure 3-8: LDR Connection
29
3.3.3. Code
Open the Arduino IDE and start write following code,
Sketch #1 – LDR display the value
Task: Write the sketch (HINT: Refer Sketch #1 – LM35 Digital Value)
TASK: Display the LDR value on the LCD. GOOD LUCK!!
(Write your sketch below)
30
NOTES
31
Bibliography
(n.d.). Retrieved 10 February, 2017, from theengineeringprojects:
http://www.theengineeringprojects.com/
(n.d.). Retrieved 25 January, 2017, from instructables:
http://www.instructables.com/
(n.d.). Retrieved 4 February, 2017, from electro schematics:
http://www.electroschematics.com/
(n.d.). Retrieved 28 January, 2017, from http://www.hacdc.org:
http://www.hacdc.org/summer-school-2013/
Arduino. (n.d.). Retrieved 20 January, 2017, from
https://www.arduino.cc/en/Main/Software: https://www.arduino.cc/
micropinguino. (n.d.). Retrieved 5 February, 2017, from
http://micropinguino.blogspot.my/2011_04_01_archive.html
Sparkfun Electronics. (n.d.). Retrieved 14 February, 2017, from Sparkfun :
https://www.sparkfun.com
www.fritzing.org. (2010). Retrieved 14 February, 2017, from
http://fritzing.org/home/
32