From Let's Control It
Jump to: navigation, search


The HC-SR04 is a ultrasonic distance sensor. It's able to indicate distances between 2 cm up to about 300 cm
at a resolution of about 0,3 cm.


The HC-SR04 must be run on 5V. Running it with 3,3V will show very weak results or nothing at all.

Beware: Do not connect 5V directly to the ESP! 

It should be mentioned that this sensor has a small metering angle of +/- 30°.
Best results are reached with objects in a straight line in front of the sensor.
Due to laws of acoustics objects with a hard surface give better results
then soft, sound absorbing surfaces.


Connecting via Levelshifter

There are several ways of connecting the HC-SR04 to the ESP.
The absolutely correct way is using a level converter to cleanly separate 5V and 3,3V circuits.

HC-SR04 with Levelshifter.jpg

Connecting with a Voltage Divider

This is a bit of an overkill anyways. A level converter is a bidirectional device converting signals from
higher voltage to lower and vice versa. The "Trigger" input works quite well with 3.3V. The output goes
only from higer voltage (5V) to lower voltage (3.3V) so a voltage divider will do the job.

 Beware: Do not connect the "Echo" output directly to the ESP!

For setting up follow the given schematics as shown in the pictures.

HC-SR04 simple.jpg

Connecting using a Zener Diode

A second possible way is using a zener diode to cut voltages above 3.3V:

HC-SR04 zener.jpg


Settings for Distance

Set up the ESPEasy task according to the screenshot.
If you have chosen other GPIO pins set them accordingly.

HC-SR04 Mega Settings-Distance.jpg
This configuration sets the HC-SR04 task to show the actual distance.

HC-SR04 Mega Tasklist.jpg

Settings for Threshhold

Sometimes it might be necessary to switch something if a person or object
gets near below a certain distance or if a water level raised to a limit.
This can be done by programming a rule. But here is an easier way:
Change the "Value" into "State" and submit. An additional field "Threshold"
opens up. Enter the distance you want for switching, in the shown example it is 30 cm.
Now the sensor shows "0" as result if nothing is near. If something gets
near below 30 cm it shows "1".

HC-SR04 Mega Settings-State.jpg

Optional settings

Use of value names