Skip to main content

hcsr04

Measure distance using HC-SR04 ultrasonic sensor for proximity detection and level monitoring.

Overview

The hcsr04 node measures distance using ultrasonic sensors like the HC-SR04. It sends ultrasonic pulses and measures the echo return time to calculate distance. Ideal for level monitoring, parking sensors, and proximity detection.

2-400cm
Range
3mm
Resolution
15°
Angle
5V
Power

Voltage Level Warning

HC-SR04 ECHO pin outputs 5V. Use a voltage divider (1kΩ + 2kΩ) to protect the Pi's 3.3V GPIO pins!

Wiring with Voltage Divider

HC-SR04 Connection

VCC → 5V

TRIG → GPIO 23

ECHO → Voltage Divider → GPIO 24

GND → GND

Voltage Divider

ECHO → 1kΩ → GPIO 24

GPIO 24 → 2kΩ → GND

This drops 5V to ~3.3V (safe for Pi)

Properties

Property Type Default Description
trigPin number 23 GPIO pin for TRIG
echoPin number 24 GPIO pin for ECHO
interval number 1000 Measurement interval (ms)
unit string "cm" "cm", "m", or "inch"
median number 3 Number of readings to average

Output

{
  "payload": 45.2,
  "unit": "cm",
  "raw": 45234,
  "timestamp": 1640000000000
}

Example: Water Tank Level Monitor

Monitor water tank level and alert when low.

// Function node: Calculate tank level
const TANK_HEIGHT = 100; // cm from sensor to bottom
const TANK_CAPACITY = 500; // liters

var distance = msg.payload; // distance to water surface
var waterHeight = TANK_HEIGHT - distance;
var percentFull = (waterHeight / TANK_HEIGHT) * 100;
var liters = (percentFull / 100) * TANK_CAPACITY;

msg.payload = {
    distance: distance,
    waterHeight: waterHeight.toFixed(1),
    percentFull: percentFull.toFixed(0),
    liters: liters.toFixed(0),
    status: percentFull < 20 ? "LOW" : percentFull < 50 ? "MEDIUM" : "OK"
};

return msg;

Related Nodes