Difference between revisions of "HC-SR04"
(5 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
= Introduction = | = Introduction = | ||
− | The | + | <br/> |
+ | [[File:HC-SR04.jpg|500px]] | ||
+ | <br/> | ||
+ | The HC-SR04 is a ultrasonic distance sensor. It's able to indicate distances between 2 cm up to about 300 cm<br/> | ||
+ | at a resolution of about 0,3 cm.<br/> | ||
− | The | + | = Precautions = |
+ | <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/> | ||
+ | |||
+ | Beware: Do not connect 5V directly to the ESP! | ||
+ | <br/> | ||
+ | It should be mentioned that this sensor has a small metering angle of +/- 30°.<br/> | ||
+ | Best results are reached with objects in a straight line in front of the sensor.<br/> | ||
+ | Due to laws of acoustics objects with a hard surface give better results<br/> | ||
+ | then soft, sound absorbing surfaces.<br/> | ||
+ | <br/> | ||
= Hardware = | = Hardware = | ||
− | |||
− | The HC-SR04 | + | ==Connecting via Levelshifter== |
+ | <br/> | ||
+ | There are several ways of connecting the HC-SR04 to the ESP.<br/> | ||
+ | The absolutely correct way is using a level converter to cleanly separate 5V and 3,3V circuits.<br/> | ||
+ | <br/> | ||
+ | [[File:HC-SR04 with Levelshifter.jpg|500px]] | ||
+ | <br/> | ||
+ | ==Connecting with a Voltage Divider== | ||
+ | <br/> | ||
+ | This is a bit of an overkill anyways. A level converter is a bidirectional device converting signals from<br/> | ||
+ | higher voltage to lower and vice versa. The "Trigger" input works quite well with 3.3V. The output goes<br/> | ||
+ | only from higer voltage (5V) to lower voltage (3.3V) so a voltage divider will do the job.<br/> | ||
+ | <br/> | ||
+ | Beware: Do not connect the "Echo" output directly to the ESP! | ||
+ | <br/> | ||
+ | For setting up follow the given schematics as shown in the pictures.<br/> | ||
+ | <br/> | ||
+ | [[File:HC-SR04 simple.jpg|500px]] | ||
+ | <br/> | ||
+ | ==Connecting using a Zener Diode== | ||
+ | <br/> | ||
+ | A second possible way is using a zener diode to cut voltages above 3.3V:<br/> | ||
+ | |||
+ | [[File:HC-SR04 zener.jpg|500px]] | ||
+ | <br/> | ||
− | == | + | = ESPEasy = |
− | |||
− | |||
− | |||
− | + | ==Settings for Distance== | |
− | |||
− | |||
− | |||
− | |||
− | + | Set up the ESPEasy task according to the screenshot.<br/> | |
+ | If you have chosen other GPIO pins set them accordingly.<br/> | ||
− | + | [[File:HC-SR04 Mega Settings-Distance.jpg|500px]] | |
− | + | <br/> | |
+ | This configuration sets the HC-SR04 task to show the actual distance.<br/> | ||
− | == | + | [[File:HC-SR04 Mega Tasklist.jpg|500px]] |
− | + | <br/> | |
+ | == Settings for Threshhold== | ||
− | + | Sometimes it might be necessary to switch something if a person or object<br/> | |
+ | gets near below a certain distance or if a water level raised to a limit.<br/> | ||
+ | This can be done by programming a rule. But here is an easier way:<br/> | ||
+ | Change the "Value" into "State" and submit. An additional field "Threshold"<br/> | ||
+ | opens up. Enter the distance you want for switching, in the shown example it is 30 cm.<br/> | ||
+ | Now the sensor shows "0" as result if nothing is near. If something gets <br/> | ||
+ | near below 30 cm it shows "1".<br/> | ||
− | + | [[File:HC-SR04 Mega Settings-State.jpg|500px]] | |
+ | <br/> | ||
+ | == Optional settings == | ||
− | + | [[ EasyValueNames | Use of value names]] |
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".