Skip to content

hantbk/shortrangeradar

Repository files navigation

Short Range Ultrasonic Radar

📝 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 50cm.
  • 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.

🧾 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!

Releases

No releases published

Packages

No packages published

Languages