Skip to content

Short Range Ultrasonic Radar - A simple radar using the ultrasonic sensor, this radar works by measuring a range from 3cm to 40 cm as non-contact distance, with angle range between 15˚ and 165˚.

License

Notifications You must be signed in to change notification settings

hantbk/shortrangeradar

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Short Range Ultrasonic Radar

Cover

📝 Table of Contents:

A. Introduction

B. User Guide

C. Component List

D. Schematic Diagram

E. Software Design

F. Authors

🏁 A. Introduction

  • 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.

HC-SR04 Ultrasonic Sensor Technical Data

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

How Does the HC-SR04 Ultrasonic Sensor Work?

The ultrasonic sensor uses sonar to determine the distance to an object. Here’s how it works:

  1. The ultrasound transmitter (trig pin) emits a high-frequency sound (40 kHz).
  2. The sound travels through the air. If it finds an object, it bounces back to the module.
  3. The ultrasound receiver (echo pin) receives the reflected sound (echo).

HC-SR04 Ultrasonic Sensor

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

🧾 B. User Guide

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

Connection diagram: Connection diagram

Actual image:

Actual circuit

Demo video:

short-range-radar-demo.mp4

🧰 C. Component List

  1. HC-SR04 Ultrasonic Sensor
  • Quantity: 1
  • Purchase link: HC-SR04
  1. SG90 Servo Motor
  • Quantity: 1
  • Purchase link: SG90
  1. 1.3-inch OLED Screen
  1. ESP32 Board
  • Quantity: 1
  • Purchase link: ESP32
  1. SYB-170 Test Board
  1. Connecting Wires

🚀 D. Schematic Diagram

Schematic diagram of the project:

Schematic diagram

💻 E. Software Design

  • 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

✍️ F. Authors

⭐ If you use the project, please give us a star. Thank you!

About

Short Range Ultrasonic Radar - A simple radar using the ultrasonic sensor, this radar works by measuring a range from 3cm to 40 cm as non-contact distance, with angle range between 15˚ and 165˚.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages