Arduino Fundamental Handbook
Arduino Fundamental Handbook
INTERFACING FUNDAMENTAL
HANDBOOK
Prepared by
FEBRUARY 4, 2017
CENTRE OF TEKNOLOGY, ELECTRICAL ENGINEERING
Politeknik Tuanku Sultanah Bahiyah
Table of Content
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
iii
LESSON 1 : ARDUINO HARDWARE& SOFTWARE
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
3
LESSON 2 : Experiment with INPUT and OUTPUT (I/O)
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.
4
2.1.3. Code
Open the Arduino IDE and start write following code,
Sketch #1
NOTES
5
Sketch #2 – LED BLINK
// -- Activity #2 –LED BLINK
void setup () {
// initialize digital pin 13 as an output.
pinMode (13, OUTPUT);
}
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;
Light chaser
with four
LEDs
Combine
exercise (a)
and (b) in
one code
7
2.2. Task 2: Working with 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.
8
2.2.3. Code
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
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.
11
Figure 2-6: Schematic diagram Switch and LED to Arduino
2.3.3. Code
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
14
2.4. Task 4: Using Arduino Serial Function
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.
15
Figure 2-10: Schematic Diagram of Serial Monitor
2.4.3. Code
Open the Arduino IDE and start write following code,
Serial.begin (9600); //opens serial port, sets data rate to 9600 bps
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
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
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
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:
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.
20
Figure 2-12: LCD Connection
2.5.3. Code
Open the Arduino IDE and start write following code,
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
Figure 3-1:
Potentiometer pins
3.1.1. Overview
22
Connect everything as shown in figure below,
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
TASK
25
3.2. Task 7: Working with LM35 temperature sensor
3.2.1. Overview
26
3.2.3. Code
void loop()
{
intval;
intdat;
val=analogRead(0);//Connect LM35 on Analog 0
Serial.print(val);
delay(500);
}
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);
}
27
NOTES
28
3.3. Task 8: Working with LDR sensor
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.
30
NOTES
31
Bibliography
32