- This project uses an ultrasonic sensor to measure the distance from the sensor to obstacles within a range of 2cm to 400cm (0.8inch to 157inch) with an accuracy of 0.3cm (0.1inch), which is good for most applications.
- Uses a servo motor to sweep a 180-degree angle.
- The measured distance will be displayed on a 1.3-inch OLED screen in the form of a horizontal scanning radar.
- The project is implemented on the ESP32 board.
- The project is suitable for use in small spaces such as rooms, corridors, etc.
The following table shows the key features and specs of the HC-SR04 ultrasonic sensor. For more information, you should consult the sensor’s datasheet.
Feature | Description |
---|---|
Power Supply | 5V DC |
Working Current | 15mA |
Working Frequency | 40kHz |
Maximum Range | 4 meters |
Minimum Range | 2 cm |
Mesuring Angle | 15º |
Resolution | 0.3 cm |
Trigger Input Signal | 10uS TTL pulse |
Echo Output Signal | TTL pulse proportional to the distance range |
Dimensions | 45mm x 20mm x 15mm |
The ultrasonic sensor uses sonar to determine the distance to an object. Here’s how it works:
- The ultrasound transmitter (trig pin) emits a high-frequency sound (40 kHz).
- The sound travels through the air. If it finds an object, it bounces back to the module.
- The ultrasound receiver (echo pin) receives the reflected sound (echo).
Taking into account the sound’s velocity in the air and the travel time (time passed since the transmission and reception of the signal) we can calculate the distance to an object. Here’s the formula:
distance to an object = ((speed of sound in the air)*time)/2
- where: speed of sound in the air at 20ºC (68ºF) = 343m/s
Steps to implement:
- Connect the ultrasonic sensor to the circuit according to the schematic diagram.
- Connect the servo motor to the circuit according to the schematic diagram.
- Connect the OLED screen to the circuit according to the schematic diagram.
- Upload the code to the circuit.
- Place the circuit in a fixed position, do not move it.
- When an obstacle moves past the sensor, the screen will display a horizontal scanning radar image.
Pin connection convention:
SR04 Ultrasonic Pin | Servo Pin | OLED Pin | ESP32 Pin |
---|---|---|---|
VCC | VCC | VCC | 3.3V |
Trig | 23 | ||
Echo | 18 | ||
GND | GND | GND | GND |
SDA | 21 | ||
SCK | 22 | ||
PWM | D5 |
Actual image:
Demo video:
short-range-radar-demo.mp4
- HC-SR04 Ultrasonic Sensor
- Quantity: 1
- Purchase link: HC-SR04
- SG90 Servo Motor
- Quantity: 1
- Purchase link: SG90
- 1.3-inch OLED Screen
- Quantity: 1
- Purchase link: 1.3-inch OLED
- ESP32 Board
- Quantity: 1
- Purchase link: ESP32
- SYB-170 Test Board
- Quantity: 1
- Purchase link: Test Board
- Connecting Wires
- Quantity: multiple
- Purchase link: Connecting Wires
Schematic diagram of the project:
- The code is written in C++.
- Uses the
Ultrasonic.h
library to read data from the ultrasonic sensor. - Uses the
ESP32Servo.h
library to control the servo motor. - Uses the
U8g2lib.h
library to control the OLED screen. - The code is divided into subroutines such as
setup()
,loop()
,GetDistance()
. - The
GetDistance()
function will return the distance from the sensor to the obstacle. - The
loop()
function will sweep the 180-degree angle of the servo motor and display the distance on the OLED screen.
The HC-SR04 ultrasonic sensor operates on the following principle:
- The sensor sends an ultrasonic signal to the obstacle through the
Trig
pin at a low level for 2uS within 10uS. - The ultrasonic signal will reflect from the obstacle and return to the sensor.
- The sensor will read the returned value through the
Echo
pin with values ranging from 10 - 38 corresponding to physical distances from 2cm - 50cm.
// Determine distance
int GetDistance() {
digitalWrite(SR04_TRIG_PIN, LOW); // Set Trig pin to low level for 2uS
delayMicroseconds(2);
digitalWrite(SR04_TRIG_PIN, HIGH); // Send ultrasonic wave for 10uS
delayMicroseconds(10);
digitalWrite(SR04_TRIG_PIN, LOW); // Turn off ultrasonic wave
unsigned int microseconds = pulseIn(SR04_ECHO_PIN, HIGH, 30000); // Wait for response, limit waiting time
return microseconds / 58; // Calculate distance from travel time
}
The SG90 servo motor operates on the following principle:
- The servo motor will sweep an angle from 0 to 180 degrees.
- The servo motor will sweep the angle according to the angle we have set in the code.
// Initialize Servo
radarServo.attach(SERVO_PIN);
radarServo.write(angle);
The 1.3-inch OLED screen operates on the following principle:
- The OLED screen uses I2C communication.
- The OLED screen will display a horizontal scanning radar image.
- The OLED screen will display the distance on the OLED screen.
dis = GetDistance();
Serial.println(dis);
// Draw radar line
if (dis >= MAX_DISTANCE) {
int x = centerX - radius * cos(currentAngle * PI / 180);
int y = centerY - radius * sin(currentAngle * PI / 180);
u8g2.drawLine(centerX, centerY, x, y);
} else {
int x = centerX - radius * dis * cos(currentAngle * PI / 180) / MAX_DISTANCE;
int y = centerY - radius * dis * sin(currentAngle * PI / 180) / MAX_DISTANCE;
u8g2.drawLine(centerX, centerY, x, y);
}
u8g2.sendBuffer();
radarServo.write(currentAngle);
Full code: Code
- Nguyen Thanh Ha - 20210298
- Do Thanh Dat - 20215560
- Vu Van Hao - 20215572
- Quach Dinh Duong - 20215558
⭐ If you use the project, please give us a star. Thank you!