MIDTERM REPORT
Subject: Computers and Peripherals
Topic:
MEASURING DISTANCE USING ULTRASONIC SENSOR
AND DISPLAYING ON LCD WITH ARDUINO
Lecturer: Prof. Nguyen Thanh Tung
Student name:
Dao Danh Thanh-20070879
Nguyen Le Tuan Anh-2000804
Table of Contents
I. Necessary components..............................................................................2
II. Concepts..................................................................................................2
1. Arduino Uno...........................................................................................2
2. HC-SR04 Ultrasonic Sensor..................................................................3
3. 16x2 LCD Display..................................................................................4
4. I2C LCD Module...................................................................................4
5. Breadboard.............................................................................................5
6. Connecting Wires...................................................................................5
7. LED (Lights)...........................................................................................5
8. Buzzer.....................................................................................................6
III. How to use..............................................................................................6
IV. Code........................................................................................................9
2
I. Necessary components
1. Arduino Uno
2. HC-SR04
3. 16x2 LCD Screen
4. I2C LCD Module
5. Breadboard
6. Connecting wires
7. LEDs
8. Buzzer
II. Concepts
1. Arduino Uno
Arduino Uno is a popular, easy-to-use microcontroller, suitable for beginners
to learn embedded programming.
Some main features of Arduino Uno:
Microcontroller: ATmega328P
Operating voltage: 5V
Number of Digital I/O pins: 14 (6 PWM pins)
Number of Analog pins: 6
Flash memory: 32KB
Clock speed: 16 MHz
Communication: UART, I2C, SPI
Arduino Uno can be programmed using Arduino IDE and communicate with
many different sensors and modules through I/O ports.
3
2. HC-SR04 Ultrasonic Sensor
The HC-SR04 ultrasonic sensor is used to measure distance by emitting
ultrasonic waves and receiving feedback signals from objects in front.
Specifications:
Operating voltage: 5V
Consumption current: ~15mA
Operating frequency: 40kHz
Measurement distance: 2cm - 400cm
Accuracy: ±3mm
Emission angle: ~15 degrees
Response time: ~750µs - 25ms
Communication: Using 2 Trigger (TRIG) and Echo (ECHO) pins
Operating principle of HC-SR04
Signal transmission:
Send a high-voltage pulse (HIGH) of 10µs to the TRIG pin.
The sensor will emit an ultrasonic wave with a frequency of 40kHz.
Receiving the echo signal:
If the ultrasonic wave encounters an obstacle, it will bounce back to the
sensor.
When the sensor receives the echo wave, the ECHO pin will turn HIGH.
Calculating the distance:
4
The time from transmitting to receiving the wave is used to calculate the
distance:
(Because the speed of sound in air is 343 m/s, that is 0.0343 cm/µs).
3. 16x2 LCD Display
A 16x2 LCD display is a display that can display 16 characters per line with a
total of 2 lines.
Specifications:
Operating voltage: 5V
Interface: Parallel (can use I2C for simplification)
Size: 80mm x 36mm
Color: Available in blue, green with white or black text
Usually, to use a 16x2 LCD, you need an I2C LCD Module to simplify the
connection.
4. I2C LCD Module
The I2C LCD Module makes 16x2 LCD communication easier by using the
I2C protocol instead of connecting the 16 pins directly.
Specifications:
Operating voltage: 5V
Communication: I2C (SDA, SCL)
Default address: 0x27 or 0x3F (depending on the type)
When using I2C LCD Module, you only need 2 wires (SDA, SCL) instead of
many wires like parallel communication.
5
5. Breadboard
Breadboard is a circuit board that helps connect electronic components
without soldering.
Some features:
There are two vertical rows for power (VCC, GND).
The horizontal rows are connected together to make it easy to connect
components.
Supports components such as microcontrollers, sensors, LEDs, etc.
6. Connecting Wires
Connecting wires are used to connect components on the Breadboard or to
Arduino Uno.
Commonly used wire types:
Male to Male jumper
Male to Female jumper
Female to Female jumper
7. LED (Lights)
LEDs are used to display signals or system status.
Common specifications:
Operating voltage: 2V - 3.3V (requires current limiting resistor if using 5V)
Current: 10-20mA
Colors: Red, green, blue, yellow, white
How to use:
Use a 220Ω - 1KΩ resistor to protect the LED when connecting to Arduino.
6
8. Buzzer
Buzzer is a sound emitting device, often used in warnings or notification
signals.
Specifications:
Operating voltage: 3V - 12V
Consumption current: ~30mA
Operating frequency: 2kHz - 4kHz
There are 2 types: Active Buzzer (generates sound) and Passive Buzzer (needs
PWM signal to generate sound).
Application:
Warning when there is an obstacle (used with SRF05)
Signaling in security system
III. How to use
- Arduino Uno
Setting up the development environment:
Installing Arduino IDE software
Install additional board definitions for the Arduino Uno within the Arduino
IDE.
Connecting the Arduino Uno: Use a cable to connect your Arduino Uno board
to your computer. The Arduino Uno should be recognized as a new COM port
on your computer. You can usually find this information in your device
manager.
Writing and Uploading Code:
7
The Arduino IDE allows you to write code for the Arduino Uno using the
Arduino programming language. There are many online resources and
example projects available to get you started with basic tasks like blinking an
LED.
Once you have written your code, select the appropriate Arduino Uno board
and COM port in the Arduino IDE tools menu. Finally, upload the code to
your Arduino Uno board using the upload button in the IDE.
- HC-SR04
Wiring the HC-SR04 to the Arduino Uno is really easy. The wiring
connections are as follows: Connect Trig to digital pin 7, connect Echo to
digital pin 8. Pin GND of the HC-SR04 goes into Ground Pin (GND) and pin
VCC goes into 5V of the Arduino Uno.
- LCD
Step 1: Add the Wire.h and LiquidCrystal_I2C.h to Your Arduino IDE
Step 2: Connect the LCD I2C with Arduino Uno and the LCD module have
clearly labeled pins, so connect as follow:
SCL – A4,
SDA – A5,
VCC – 5V,
GND - GND
Step 4: Scan the I2C Bus for the Correct Address I2C is a two wire protocol
that allows multiple devices to be used, with only two pins used on the
microcontroller. This is accomplished by setting an address on each device on
the bus. Not all I2C LCD's use the same address.
Uploading that sketch showed me in the serial monitor that I was using
address 0x27, so I loaded the following sketch and made sure that it was
8
trying to communicate at the correct address, and screen size. Common screen
sizes are 20x4, and 16x2.
LiquidCrystal_I2C lcd(0x27, 16, 2);
Step 5: Outputting Text on Your LCD I have included a sample sketch to show
you how to output text on your LCD.
The key to getting output where you want it is that the column is set first, then
the line number, both start at 0.
// Move the cursor 0 characters to the right and
// zero characters down (line 1).
lcd.setCursor(0, 0);
// Print distance to the screen.
lcd.print("khoangcach");
Buzzer:
A passive buzzer can make different tones, but the device that controls it has
to provide an output with an oscillating electronic signal at the desired
frequency. The supplied frequency will determine the tone.
Active Buzzer: An active buzzer has an additional circuit on it, which makes it
easier to use but limits the buzzer to only one type of sound. An active buzzer
only requires a DC power source for a beep to be generated.
Circuit Connection: Connect the passive buzzer to GPIO pin 9. They will be
powered directly from the GPIO pins. A resistor can be placed in series with
the positive leg of the buzzer to protect the board from loading too much
current.
Coding: This code will turn on the passive when the distance is less than
10cm. The passive is also connected to a pin set as digital output and PWM
compatible. To generate sound in the passive buzzer, we will use the Arduino
9
“digitalWrite(BUZZER_PIN, HIGH);” that generate sound.
Red and green led:
LED: A led only requires a DC power source for a light to be generated.
Circuit Connection: Connect the passive buzzer to GPIO pin 10 for red light
and pin 11 for green light. They will be powered directly from the GPIO pins.
Coding: This code will turn on the red light when the distance less than 10cm,
while green light is opposite. The leds is also connected to a pin set as digital
output and PWM compatible. To generate LEDs, we will use the Arduino
“digitalWrite(greenLed, HIGH);”, which enables the green light and
“digitalWrite(redLed, HIGH);” which enables the red light
IV. Code
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // LCD I2C 16x2
const int trig = 8;
const int echo = 7;
const int ledPin = 9; // Chân nối đèn LED
unsigned long thoigian;
int khoangcach;
void setup() {
Serial.begin(9600);
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
pinMode(ledPin, OUTPUT); // Cấu hình LED là OUTPUT
lcd.init();
lcd.backlight();
10
lcd.setCursor(0, 0);
lcd.print("Khoang cach: ");
}
void loop() {
// Gửi xung từ Trig
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
// Đọc thời gian Echo phản hồi
thoigian = pulseIn(echo, HIGH);
khoangcach = int(thoigian / 2 / 29.412); // Tính khoảng cách (cm)
// Hiển thị lên Serial
Serial.print("Khoang cach: ");
Serial.print(khoangcach);
Serial.println(" cm");
// Hiển thị lên LCD
lcd.setCursor(0, 1);
lcd.print(" "); // Xóa dòng cũ
lcd.setCursor(0, 1);
lcd.print(khoangcach);
lcd.print(" cm");
// Điều khiển LED cảnh báo
if (khoangcach < 6) {
digitalWrite(ledPin, HIGH); // Bật LED nếu vật quá gần
} else {
digitalWrite(ledPin, LOW); // Tắt LED nếu an toàn
}
delay(500);
}
11