Ultrasonic Sensor Module (HC-SR04)

../_images/01_ultrasonic.png

Introduction

The Ultrasonic Module(HC-SR04) is a sensor that can measure distances between 2cm and 400cm using ultrasonic waves. It is commonly used in robotics and automation projects to detect objects and measure distances. The module consists of an ultrasonic transmitter and receiver, which work together to send and receive ultrasonic waves.

Principle

The module includes ultrasonic transmitters, receiver and control circuit. The basic principles are as follows:

  1. Use an IO flip-flop to process a high level signal of at least 10us.

  2. The module automatically sends eight 40khz and detects if there is a pulse signal return.

  3. If the signal returns, passing the high level, the high output IO duration is the time from the transmission of the ultrasonic wave to the return of it. Here, test distance = (high time x sound speed (340 m / s) / 2.

The timing diagram is shown below.

../_images/01_ultrasonic_principle.png

You only need to supply a short 10us pulse for the trigger input to start the ranging, and then the module will send out an 8 cycle burst of ultrasound at 40 kHz and raise its echo. You can calculate the range through the time interval between sending trigger signal and receiving echo signal.

Note

It is recommended to use measurement cycle over 60ms in order to prevent signal collisions of trigger signal and the echo signal.

Formula:
  • us / 58 = centimeters

  • us / 148 = inch

  • distance = high level time * speed of sound (340m/s) / 2;

Usage

Hardware components

  • Arduino Uno R4 or R3 board * 1

  • Ultrasonic Sensor Module * 1

  • Jumper Wires

Circuit Assembly

../_images/01-ultrasonic_circuit.png

Code



Code explanation

  1. Pin declaration:

    Start by defining the pins for the ultrasonic sensor. echoPin and trigPin are declared as integers and their values are set to match the physical connection on the Arduino board.

    const int echoPin = 3;
    const int trigPin = 4;
    
  2. setup() function:

    The setup() function initializes the serial communication, sets the pin modes, and prints a message to indicate the ultrasonic sensor is ready.

    void setup() {
      Serial.begin(9600);
      pinMode(echoPin, INPUT);
      pinMode(trigPin, OUTPUT);
      Serial.println("Ultrasonic sensor:");
    }
    
  3. loop() function:

    The loop() function reads the distance from the sensor and prints it to the serial monitor, then delays for 400 milliseconds before repeating.

    void loop() {
      float distance = readDistance();
      Serial.print(distance);
      Serial.println(" cm");
      delay(400);
    }
    
  4. readDistance() function :

    The readDistance() function triggers the ultrasonic sensor and calculates the distance based on the time it takes for the signal to bounce back.

    float readDistance() {
      digitalWrite(trigPin, LOW);   // Set trig pin to low to ensure a clean pulse
      delayMicroseconds(2);         // Delay for 2 microseconds
      digitalWrite(trigPin, HIGH);  // Send a 10 microsecond pulse by setting trig pin to high
      delayMicroseconds(10);
      digitalWrite(trigPin, LOW);  // Set trig pin back to low
      float distance = pulseIn(echoPin, HIGH) / 58.00;  // Formula: (340m/s * 1us) / 2
      return distance;
    }
    

Additional Ideas

  • Display the distance on an LCD screen instead of serial monitor

  • Add LEDs that light up when object is within a threshold distance

More Projects