Arduino 2025
Arduino 2025
Staff:
1. Shuaib Rejab Awad
2. Mohammed Jassim
3. Zahraa Ahemd
0
Contents
1. Introduction to Arduino ........................................2
2. Simple Input /Output ............................................8
3. HC-SR04 Ultrasonic Sensor ..................................12
4. DHT11 Humidity & Temperature Sensor.............18
5. IR Receiver and LCD Display ................................22
1
Real-time and Control lab: Arduino
1. Introduction to Arduino
1. Introduction
Arduino is a prototype platform (open-source) based on an easy-to-use
hardware and software. It consists of a circuit board, which can be
programed (referred to as a microcontroller) and a ready-made software
called Arduino IDE (Integrated Development Environment), which is used
to write and upload the computer code to the physical board.
2. Types of Arduino
There are several types of Arduino Boards such as Arduino Uno, Zero,
Duemilanove,Diecimila, Leonardo, Nano and Mega as shown in Fig. 2.
Each one has its own strength and in terms of I/O pins, memory size, etc.
• The differences are based on the number of inputs and outputs (the
number of sensors, LEDs, and buttons you can use on a single
board), speed, operating voltage, form factor etc.
3
In our experiments, Arduino UNO is going to be used due to its simplicity
(shown in Fig. 3).
4
Fig. 5: Board description.
4. Installation
• First you must have your Arduino board, in addition you will need a
standard USB cable to connect the Arduino.
• Download Arduino IDE Software: you can get different versions of
Arduino IDE from the Download page on the Arduino Official website.
https://www.arduino.cc/en/Main/Software
• After your file download is complete, unzip the file and power up your
board.
• Connect the Arduino board to your computer using the USB cable. The
green power LED (labelled PWR) should glow.
• After your Arduino IDE software is downloaded, you need to unzip the
folder. Inside the folder, you can find the application icon with an infinity
label (application.exe). Double-click the icon to start the IDE.
• Step 5: Open your first project.
• Once the software starts, you have two options: create a new project or
open an existing project example.
• To create a new project, select File --> New.
• To open an existing project example, select File -> Example -> Basics ->
Blink.
• Step 6: Select your Arduino board.
• Go to Tools -> Board and select your board as shown below.
5
5. Input/output pins
• In digital pins, you have just two possible states, which are on or off.
These can also be referred as High or Low, 1 or 0 and 5V or 0V.
• In analog pins, you have unlimited possible states between 0 and 1023.
• PWM pins are digital pins, so they output either 0 or 5V. However, these
pins can output “fake” intermediate voltage values between 0 and 5V,
because they can perform “Pulse Width Modulation” (PWM). PWM
allows to “simulate” varying levels of power by oscillating the output
voltage of the Arduino. Samples of I/O signals are shown below.
• To control a digital output you can use the function digitalWrite() and
between the brackets the pin number and values (HIGH or LOW) are
written. For example: digitalWrite(3, HIGH).
• To control a PWM pin you use the function analogWrite() and between
the brackets the pin number and values (0-255) are written. For
example: analogWrite(5, 240).
• To read an analog input you use the function analogRead(pin_no) and
for a digital input you use digitalRead(pin_no).
6. Main functions
• setup():used for program initializations which is programmed once only.
• loop(): main function of the program which is run continuously.
6
• delay(): delay in milliseconds.
• Serial.begin(9600): used to initiate the serial connection. Standard buad
rate of 9600 is usually used.
• Serial.println(sensorValue): used to print a value on the serial monitor.
7. Sample program
The program shown below is going to turn the internal led in the Arduino board
for one second and turn it off for one second, repeatedly. Also it displays the
value on the serial monitor.
7
Real-time and Control lab: Arduino
1. Introduction
In this Experiment, an Arduino UNO device is going to be utilized to learn
the basic of analog and digital input/output. A set of simple input
components are used such as a switch button and a photo cell (CDS) with
an output component such as a LED.
Fig. 1 Shows the some I/O components used in this experiment.
8
(a) CDS
(b) LED
(C) Dipswitch
2. Analog terminals
Arduino UNO has 6 analog input pins (A0-A5) as shown in Fig. 2. Each
pin can read an analog value from (0-5V) and quantize the value to the
digital format (0-1023). To read an analog value, use the function
analogRead(pin_number).
The pins (3, 5,6, 9 ,10, 11) can be used to write analog value via Pulse
Width Modulation (PWM). To control a PWM pin, the function
analogWrite(pin_number, value) can be used to generate PWM signal
between the values of 0-255.
4. Procedure:
a. Connect a CDS sensor with a suitable resistor as shown in Fig. 3 to
form a voltage divider circuit. The CDS will change its resistance
according the amount of light, brighter light will reduce the resistor
9
of the CDS. Read the value of the CDS using analogRead function
and print the value on the serial monitor. 5V will result in 1023 value
and 0V will produce 0 and so on.
b. Connect a dip switch to the 5V and use the function digital read in
order to read the digital value and print them on the serial monitor.
10
Fig. 5: Dipswitch and LED connection with Arduino.
e. Repeat the last step but use the function AnalogWrite() to control
the brightness of the LED. In this way the LED brightness is
controlled according to the amount of the light of the CDS.
Hint: use the analog value from the CDS as output for the
AnalogWrite() function.
5. Class work
Design a traffic light circuit do the following (Use CDS, red, yellow and
green LEDs):
a. The traffic work normally during the day.
b. During the night the yellow light of the traffic flashes on/off and the
street light (represented by another led) turns on.
11
Real-time and control Lab: Arduino
1. Introduction
12
The HC-SR04 ultrasonic sensor uses sonar to determine distance to an
object like bats do. It offers excellent non-contact range detection with
high accuracy and stable readings in an easy-to-use package.
From 2cm to 400 cm or 1” to 13 feet. Its operation is not affected by
sunlight or black material like sharp rangefinders are (although
acoustically soft materials like cloth can be difficult to detect). It comes
complete with ultrasonic transmitter and receiver module.
2. Pin diagram
• VCC: +5VDC
• Trig : Trigger (INPUT)
• Echo: Echo (OUTPUT)
• GND: GND
The HC-SR04 Ultrasonic Module has 4 pins, Ground, VCC, Trig and
Echo. The Ground and the VCC pins of the module needs to be connected
to the Ground and the 5 volts pins on the Arduino Board respectively and
the trig and echo pins to any Digital I/O pin on the Arduino Board.
13
3. Technical Specifications
• Power Supply :+5V DC
• Quiescent Current : <2mA
• Working Current: 15mA
• Effectual Angle: <15°
• Ranging Distance : 2cm – 400 cm/1″ – 13ft
• Resolution : 0.3 cm
• Measuring Angle: 30 degree
• Trigger Input Pulse width: 10uS
• Dimension: 45mm x 20mm x 15mm
4. How It Works
In order to generate the ultrasound you need to set the Trig on a
High State for 10 µs. That will send out an 8 cycle sonic burst which will
travel at the speed sound and it will be received in the Echo pin. The Echo
pin will output the time in microseconds the sound wave traveled.
For example, if the object is 10 cm away from the sensor, and the speed
of the sound is 340 m/s or 0.034 cm/µs the sound wave will need to travel
about 294 u seconds. But what you will get from the Echo pin will be
double that number because the sound wave needs to travel forward and
bounce backward. So in order to get the distance in cm we need to
multiply the received travel time value from the echo pin by 0.034 and
divide it by 2.
14
5. Procedure
First you have to define the Trig and Echo pins. In this case they are the
pins number 9 and 10 on the Arduino Board and they are named trigPin
and echoPin. Then you need a Long variable, named “duration” for the
travel time that you will get from the sensor and an integer variable for the
distance.
In the setup you have to define the trigPin as an output and the echoPin
as an Input and also start the serial communication for showing the results
on the serial monitor.
In the loop first you have to make sure that the trigPin is clear so you have
to set that pin on a LOW State for just 2 µs. Now for generating the Ultra
15
sound wave we have to set the trigPin on HIGH State for 10 µs. Using
the pulseIn() function you have to read the travel time and put that value
into the variable “duration”. This function has 2 parameters, the first one
is the name of the echo pin and for the second one you can write either
HIGH or LOW. In this case, HIGH means that the pulsIn()function will wait
for the pin to go HIGH caused by the bounced sound wave and it will start
timing, then it will wait for the pin to go LOW when the sound wave will
end which will stop the timing. At the end the function will return the length
of the pulse in microseconds. For getting the distance we will multiply the
duration by 0.034 and divide it by 2 as we explained this equation
previously. At the end we will print the value of the distance on the Serial
Monitor.
16
Steps:
7. Ultrasonic Library
There is another way to program the sensor using library called NewPing
as following:
• Download NewPing Library
• Put the "NewPing" folder in "libraries\".
• In the Arduino IDE, create a new sketch (or open one) and select from
the menubar "Sktech->Import Library->NewPing".
The code :
#include <NewPing.h>
#define TRIGGER_PIN 12
#define ECHO_PIN 11
#define MAX_DISTANCE 200
void setup() {
Serial.begin(115200);
}
void loop() {
delay(50);
Serial.print("Ping: ");
Serial.print(sonar.ping_cm());
Serial.println("cm");
}
17
Real-time and Control lab: Arduino
1. Introduction
18
DHT11 Temperature & Humidity Sensor features a temperature & humidity
sensor complex with a calibrated digital signal output.
This sensor includes a resistive-type humidity and temperature sensor with an
8-bit built in microcontroller offering excellent quality, fast response, anti-
interference ability and cost-effectiveness.
2. Pin diagram
3. Technical Specifications
• Power Supply: 3.3~5.5V DC
Output: 4 pin single row or 3 pin mounted on a PCB
Measurement Range: Humidity 20-90%RH,Temperature 0~50℃
Accuracy: Humidity +-5%RH, Temperature +-2℃
• DHT22 is more expensive but has higher range of temperature.
19
4. Procedure
• Connect the DHT11 sensor to the Arduino as shown in Figure 1.
• To read to from the DHT11 sensor use the instruction:
1. Write a program to read the temperature and humidity from the sensor
and display the results on the serial monitor. You will need to use the
DHTLib to encode the data. This library should be on your PC and can be
added from: Sketch> Include Library> Add .ZIP library
2. Signal an alarm when the temperature exceeds 25C and turn a fan (led)
when humidity exceeds 35%.
3. Connect a CDS to port A0 and a led to the digital output port (9) as
shown in Figure 2
20
Figure 2: Connection of a CDS and a Led to the Arduino.
4. To design an optical fire alarm circuit, point a led to the CDS and assign a
high logic for the led (pin 9). When a light is pointe at the CDS the value
of the resistor is low (more voltage is being fed to the Arduino). If a fire
is happened, the smoke will prevent the light from reaching the CDS and
hence the value of the resistor will increase.
5. Set the alarm if a smoke is detected and the temperature exceeded 30C
(use DHT11 in step 1).
21
Real-time and Control lab: Arduino
22
1. Introduction
In this Experiment, you shall use an infrared (IR) receiver and an Arduino
to control 3 LEDs with a remote control. Also, you’ll learn how to use an
I2C 16x2 LCD display.
You will need the IRremote.h library to decode the remote signal. Also,
the LiquidCrystal_I2C.h and Wire.h to control the LCD.
This type of IR sensor shown in Fig. 1 below, has 3 pins; signal, GND and
VCC. The operation voltage ranges from 2.7-5.5V and can support a
carrier frequency of 38KHz.
IR light is emitted by the sun, light bulbs, and anything else that produces
heat. That means there is a lot of IR light noise all around us. To prevent
this noise from interfering with the IR signal, a signal modulation technique
is used.
The IR signal is modulated during transmission. Modulation means
assigning pattern to the data to be sent to the receiver. The most
commonly used IR modulation is about 38khz. In the figure below, you
can see and modulated signal to the left and an unmodulated signal to the
right. Each time you press a button on the remote control, a unique
hexadecimal code is generated. This is the information that is modulated
and sent over IR to the receiver.
Note: please bring any unwanted TV or satellite remote controller, if you can!
23
Fig. 2: Modulated signal at the transmitter and a decoded signal at the receiver.
2. IR Tester Program
This program is working as a remote tester. Basically, it is going to print
the received code from a remote control on the serial monitor.
24
3. LCD Display Program
This program will show you how to print on the LCD display
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); \\ pins decleration
of the LCD
void setup(){
Serial.begin(9600);
lcd.begin(16,2);//Defining 16 columns and 2 rows of lcd display
lcd.backlight(); //To Power ON the back light }
void loop() {
lcd.setCursor(0,0); //Defining position to write from first row, first column .
lcd.print(" test "); //You can write 16 Characters per line .
}
4. Procedure
25
Fig. 3: experiment diagram.
26