Day 16
Modules 2
Ultrasonic Sensor as Distance Meter
1
Objectives
• Learn how to use Ultrasonic sensor module with Arduino to measure
distance in centimeter (cm)
• Learn how to use the measured distance to control LEDs as proximity
indicators
• Learn how to use transistor to drive high current device such as DC
motor
2
What is Ultrasonic Sensor Module?
This is an electronic range finder module used for measuring physical
distance between itself and a point in front of it, similar to an infrared
obstacle sensor module but this is based on ultrasound instead.
HC-SR04 Ultrasonic
Sensor Module
3
About Ultrasonic Sensor Module
Ultrasonic Sensor Pin Meaning
Power Source, can be
VCC
connected to 3.3V or 5V
Trigger pin, to initiate the
TRIG emission of ultrasound from
the transmitter
Echo pin, to return the
pulse duration that can
ECHO
represent distance between
sensor and object
Power Ground, connect to
GND
0V of control system
4
How Ultrasonic Sensor Works?
The sensor is composed of two ultrasonic transducers. One is
transmitter (TX) which outputs ultrasonic sound pulses and the other is
receiver (RX) which listens for reflected waves.
Object
5
How Ultrasonic Sensor Works?
During the moment that
the transmitter emit the
ultrasound, the module
start timer, until the
ultrasound is reflected
back to the receiver,
then module stop the
timer, then output an
Echo signal in form of
pulse with a duration.
6
How Ultrasonic Sensor works with Arduino?
The sensor works by receiving a start signal at TRIG pin. The transmitter
will emit sound at ultrasonic range that is inaudible to human.
When there is object, this
sound can reflect back to the
sensor, particularly to the
receiver.
At this moment, the ECHO pin
will output a pulse duration,
and Arduino is responsible to
read this pulse and perform
the calculation to get the
distance measurement.
7
How to use Ultrasonic Sensor
with Arduino?
You can connect the ultrasonic
sensor to Arduino board as
Ultrasonic Sensor Pin Arduino Pin
VCC 5V
TRIG Pin 2
ECHO Pin 3
GND GND
8
Code to use Ultrasonic Sensor in Pictoblox
In Pictoblox, after you have chosen board type as Arduino Uno, you can
find the block to use with the ultrasonic sensor module under Sensors
Almost all I/O pins on Arduino Uno board can be used to connect to the
ultrasonic sensor module TRIG and ECHO pins, except pin 0 and 1. You
need a variable to store the data read from ultrasonic sensor module,
such as
9
Testing the Ultrasonic Sensor in Pictoblox
You can build this Pictoblox Can be found in
code, upload into Arduino Add Extension >
board and observe the Communication
Serial Monitor to test the
ultrasonic sensor module.
If done properly, you
should be able to see the
distance data displayed in
the Serial Monitor from
time to time.
10
Testing the Ultrasonic Sensor in Pictoblox
In Serial Monitor…
These are the data
of distance by the
ultrasonic sensor, in
centimeter (cm)
Located at the
bottom right of the
Pictoblox screen
11
Project 1
Ultrasonic Distance Alarm
12
Introduction
In this project, you will learn how to use LEDs to tell the proximity of
the output of an ultrasonic sensor module, how close the sensor is to
the obstacle.
Let’s make the logic this way…
• Red LED lights up when distance is less than 10cm
• Yellow LED lights up when distance is in between 10cm and 30cm
• Green LED lights up when distance is more than 30cm
13
Program Distance is
Yes Switch on
Flowchart less than
10cm?
Red LED
No
Distance is
Yes Switch on
10 < d < 30
Yellow LED
cm?
Read Distance
Start of No
from Ultrasonic
Program
Sensor Module
Distance is Yes Switch on
more than
Green LED
30cm?
Program cycle
0.2s
14
Components Required
• Arduino Uno board
• Jumper Wires (F-M & M-M)
Hc-SR04 Ultrasonic Sensor Module
• Breadboard
• Ultrasonic Sensor Module (HC-SR04)
• LEDs (Red, Yellow, Green are preferred)
• Resistor 220R
Jumper Wire Female-to-Male (F-M)
15
Breadboard
Diagram
Pin Arduino
VCC 5V
GND GND
TRIG Pin 2
ECHO Pin 3
Red LED Pin 9
Yellow LED Pin 10
Green LED Pin 11
16
Circuit
Diagram
17
Pictoblox
Code
18
Code Explanation (VARIABLE)
• distance
This is where the distance data from ultrasonic sensor is kept, in cm.
• farDistance
This is to define the far limit value for the distance for checking.
• nearDistance
This is to define the near limit value for the distance for checking.
19
Code Explanation (SETUP)
Before starting the program, you will need to let Arduino board knows
the distance limits so it know which subsequent LED need to turn on
for the measured distance.
• Set near distance to 10cm by putting 10 into nearDistance.
• Set far distance to 30cm by putting 30 into farDistance.
20
Code Explanation (INPUT)
This is the most essential block in this project, which is to tell Arduino
board to get the distance by reading the ultrasonic sensor module,
connected at pin 2 and 3 of Arduino board, as Trigger (TRIG) and Echo
(ECHO) pins on the ultrasonic sensor module respectively,
then store the distance data into the variable distance
21
Code Explanation (PROCESS & OUTPUT 1)
For the case of distance is less
than the set near distance, which
means the object is too close to
the ultrasonic sensor module,
THEN switch on RED LED.
Add a delay of 0.2s after the
closing of ELSE statement to make
the program cycle at 0.2s rate.
22
Code Explanation (PROCESS & OUTPUT 2)
For the case of distance is more than near distance AND less than far
distance, which means the object is in range to the ultrasonic sensor
module, THEN switch on YELLOW LED.
23
Code Explanation (PROCESS & OUTPUT 3)
For the case of distance is more than the set far distance, which means
the object is too far from the ultrasonic sensor module, THEN switch on
GREEN LED.
24
Results 1
1. GREEN LED is ON when
object is not in range.
2. YELLOW LED is ON
when object is in range.
3. RED LED is ON when
object is too close.
25
Results
2 3
26
Project 2
Ultrasonic Auto Fan
27
Introduction
In this project, you will learn how to switch on a motorized fan using
the ultrasonic sensor module to sense the presence of user.
The logic of this program would be…
• IF there is person within 20cm range, THEN turn on the motor fan
• IF there is no person in range (e.g. >40cm), THEN turn off the motor
fan
You will need a battery pack as external power source for driving the
DC motor. Caution is advised when handling the DC motor!
28
Components Required
• Arduino Uno board
• Jumper Wires (F-M & M-M)
DC Motor 130 + Fan Blade
• Breadboard
• Ultrasonic Sensor Module (HC-SR04)
• DC Motor 130 + Fan Blade
• Transistor TIP122
• Resistor 1K
• Battery Holder AA 2-slot + Battery AA
Transistor TIP122
29
Program
Flowchart
Distance is
Yes Switch ON
less than
Motor Fan
20cm?
Read Distance
Start of No
from Ultrasonic
Program
Sensor Module
Distance is Yes Switch OFF
more than
Motor Fan
40cm?
Program cycle
1s
30
Breadboard
Diagram
Pin Arduino
VCC 5V
GND GND
TRIG Pin 2
ECHO Pin 3
1K resistor
Pin 11
(transistor)
31
Circuit
Diagram
32
What is Transistor?
Transistor, in simple words, can be used as an electronic switch to control the
state of device, such as the on and off state of device, as well as speed of
device when running with PWM.
It takes very small current at the base to switch on very large current across
the collector and emitter.
• Base (B) : This pin takes in very small current from electrical signal.
• Collector (C) : This pin is the output of transistor, very large current can
pass through this pin to switch on the device connected.
• Emitter (E) : This pin is the negative potential end of the transistor, usually
connects to circuit ground to allow large current pass through the device.
33
Identify the Pinout of Transistor
TIP122
B E
C 34
Why is Transistor used?
Arduino can only distribute small current from its pin to the device
connected. It is okay to directly use the current from its pin to control
devices such as LED, tone buzzer, because LED and tone buzzer require
very small current to switch on. However, each Arduino pin can only
output maximum of 20mA.
For high current demanding device like a motor, transistor can be used
to intake the small current from the pin to amplify the signal into the
device connected, thus switching on and off the device just like how
you do with LED!
Be VERY CAUTIOUS when handling high current devices,
Careless handlings may cause physical injuries! 35
Pictoblox Code
Setup
Input
Process
Output
Program
Cycle
36
Code Explanation (VARIABLE)
• distance
This is where the distance data from ultrasonic sensor is kept, in cm.
• farDistance
This is to define the far limit value for the distance for checking.
• nearDistance
This is to define the near limit value for the distance for checking.
• fanON
This is to define the value of speed of fan motor when switched ON.
• fanOFF
This is to define the value of speed of fan motor when switched OFF.
• fanSpeed
This is the data of speed value to be outputted to the fan motor
37
Code Explanation (SETUP)
Before starting the program, you will need to let Arduino board knows
the distance limits for the measured distance.
• Set near distance to 20cm by putting 20 into nearDistance.
• Set far distance to 40cm by putting 40 into farDistance.
38
Code Explanation (SETUP)
Then, you will also need to let Arduino board knows the speed of the
fan motor when it is OFF or ON.
• Set OFF speed at 0% by putting 0 into fanOFF.
• Set ON speed at 78% by putting 200 into fanON.
This is a PWM value, that (200/255) x 100% = 78%
39
Code Explanation (INPUT)
This is the most essential block in this project, which is to tell Arduino
board to get the distance by reading the ultrasonic sensor module,
connected at pin 2 and 3 of Arduino board, as Trigger (TRIG) and Echo
(ECHO) pins on the ultrasonic sensor module respectively,
then store the distance data into the variable distance
40
Code Explanation (PROCESS)
IF the distance value is less than
nearDistance, which means there
might be person detected in range,
THEN switch ON the fan motor.
IF the distance value is more than
the farDistance, which means the
person may have leave the range,
THEN switch OFF the fan motor.
41
Code Explanation (OUTPUT)
Output the value of speed of fan motor to the pin 11 of Arduino board
that connected to the transistor, which in turn will switch the fan motor
at the said speed in the process.
Lastly, cycle this program at the rate of 1 second.
42
Results
Person is absent, fan is OFF Person is present, fan is ON
43
Conclusions
Now, you should know how to use ultrasonic sensor module with
Arduino to measure distance and perform output tasks such as
switching on and off LEDs, and fan motor.
Congratulations, you have completed the Arduino workshop main
course!
End of Contents
44