HC-SR04 Ultrasonic Distance Sensor

Ultrasonic Distance Sensor

The HC-SR04 Ultrasonic distance sensor measures distance by sending out a 40kHz pulse of sound and then listening for the echo. It's got just 4 pins: Vcc, GND, Trig and Echo. Trig triggers a pulse and Echo gives a pulse whenever an echo is heard.

Connect as follows:

HC-SR04 Espruino
GND GND
Vcc VBAT (5v)
Trig A0
Echo A1

Note that Trig and Echo can be on any GPIO pins.

It's easy to use with the HC-SR04 (About Modules) module:

var sensor = require("HC-SR04").connect(A0,A1,function(dist) {
  console.log(dist+" cm away");
});
setInterval(function() {
  sensor.trigger(); // send pulse
}, 500);

Using

Buying

This page is auto-generated from GitHub. If you see any mistakes or have suggestions, please let us know.