Difference between revisions of "HC-SR04"
Line 6: | Line 6: | ||
at a resolution of about 0,3 cm.<br/> | at a resolution of about 0,3 cm.<br/> | ||
− | = Precautions | + | = Precautions = |
<br/> | <br/> | ||
The HC-SR04 must be run on 5V. Running it with 3,3V will show very weak results or nothing at all.<br/><br/> | The HC-SR04 must be run on 5V. Running it with 3,3V will show very weak results or nothing at all.<br/><br/> | ||
Line 17: | Line 17: | ||
then soft, sound absorbing surfaces.<br/> | then soft, sound absorbing surfaces.<br/> | ||
<br/> | <br/> | ||
+ | |||
= Hardware = | = Hardware = | ||
Latest revision as of 21:17, 11 January 2018
Contents
Introduction
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.
Precautions
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.
Hardware
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.
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.
Connecting using a Zener Diode
A second possible way is using a zener diode to cut voltages above 3.3V:
ESPEasy
Settings for Distance
Set up the ESPEasy task according to the screenshot.
If you have chosen other GPIO pins set them accordingly.
This configuration sets the HC-SR04 task to show the actual distance.
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".