Component: Ultrasound sensor [HC‐SR04] - Albin-Tenghagen/The-Fellowship-of-the-Code-Flood-checker GitHub Wiki

"Ultrasonic Ranging Module [HC-SR04]"


Short Description:

The HC-SR04 is a low-costing, non-contact distance sensor that uses ultrasonic sound waves to measure the distance to an object. It is ideal for precise, short-to-mid range measurements with a wide range of applications.

image

Source: handsontec.com

image

Source: edn.com

Example Applications:

  • Water level monitoring (e.g. tanks, flood detection)
  • Obstacle detection in robotics
  • Parking assist systems
  • Distance-triggered lighting or automation

Features:

  • Non-contact distance measurement using ultrasonic waves
  • Measures from 2 cm to 400 cm with high accuracy
  • Compatible with Arduino, Raspberry Pi, and other microcontrollers
  • Simple 4-pin interface (TRIG, ECHO, VCC, GND)
  • Real-time readings with microsecond resolution

Pin Layout:

The HC-SR04 ultrasonic sensor has 4 pins:

  • VCC
    • Provides power to the sensor’s internal circuitry.
    • Typically connected to the 5V pin on your microcontroller.
  • GND
    • Ground reference for the circuit.
    • Connects to the GND pin on the microcontroller or power source.
  • TRIG
    • Trigger input pin.
    • Send a 10 µs HIGH pulse to initiate a distance measurement.
  • ECHO
    • Output pin.
    • Stays HIGH for a duration proportional to the time it takes for the ultrasonic pulse to bounce back.
Pin Name Function Typical Connection
VCC Power supply input (5V) 5V on Arduino
GND Ground Pin GND on Arduino
TRIG Trigger input (send 10 µs pulse) Digital pin
ECHO Echo output (pulse duration response) Digital pin

image

Source: handsontec.com

Specifications:

Module specification:

Electrical Parameters Value
Operating Voltage 3.3Vdc ~ 5Vdc
Quiescent Current <2mA
Operating Current 15mA
Operating Frequency 40KHz
Operating Range & Accuracy 2cm ~ 400cm ( 1in ~ 13ft) ± 3mm
Sensitivity -65dB min
Sound Pressure 112dB
Effective Angle 15°
Connector 4-pins header with 2.54mm pitch
Dimension 45mm

Source: handsontec.com

Sequence Chart:

image

Source: handsontec.com

Example Code:

Arduino Connection Schematic:

(Connect the circuit as shown below using an Arduino Uno controller board)

image

Source: handsontec

Arduino Code Example:

(Copy and paste the following code into Arduino IDE and upload to your Arduino Uno Board)

/*==========================================================================
// Author : Handson Technology
// Project : HC-SR04 Ultrasonic Sensor with Arduino Uno
// Description : HC-SR04 Distance Measure with Arduino and display
// on Serial Monitor.
// LiquidCrystal Library - Special Chars
// Source-Code : HC-SR04.ino
//==========================================================================
*/

int trig = 9;
int echo = 8;
int duration;
float distance;
float meter;

void setup()
{
    Serial.begin(9600);
    pinMode(trig, OUTPUT);
    digitalWrite(trig, LOW);
    delayMicroseconds(2);
    pinMode(echo, INPUT);
    delay(6000); // Initial delay
    Serial.println("Distance:");
}

void loop()
{
    digitalWrite(trig, HIGH);
    delayMicroseconds(10); 
    digitalWrite(trig, LOW);

    duration = pulseIn(echo, HIGH);

    if (duration >= 38000) {
        Serial.print("Out of range");
    } else {
        distance = duration / 58.0;
        Serial.print(distance);
        Serial.print(" cm");

        meter = distance / 100.0;
        Serial.print("\t");
        Serial.print(meter);
        Serial.println(" m");
    }

    delay(1000);
}

Source: handsontec

Resources: