Difference between revisions of "HC-SR04"

From Let's Control It
Jump to navigation Jump to search
m
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
 
= Introduction =
 
= Introduction =
The ESP can also be used to measure distance with the HC-SR04 Ultrasonic sensor.
+
<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 sensor has to be connected to two GPIO pins on the ESP module. The ESP uses an interrupt routine to retrieve the echo pulse from the HC-SR04.
+
= 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 =
[[File:HC-SR04.jpg|320px]]
 
  
The HC-SR04 can be found on eBay. Please note that the sensor uses 5 volts signals and they should not directly be connected to the ESP. Use a level shifter!
+
==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/>
  
  
== Connections ==
+
= ESPEasy =
<span style="color:red">
 
WARNING: The unit uses 5 volt signals that can not be used on the ESP module. You must use a levelshifter!!!
 
And do not connect VCC between HC-SR04 and the ESP. Power the HC-SR04 with a separate 5 volt supply!</span>
 
  
 +
==Settings for Distance==
  
    '''HC-SR04'''            '''ESP'''
+
Set up the ESPEasy task according to the screenshot.<br/>
    VCC      <-->     -
+
If you have chosen other GPIO pins set them accordingly.<br/>
    Trig      <-->    1st GPIO (via levelshifter!!!)
 
    Echo      <-->     2nd GPIO (via levelshifter!!!)
 
  
= ESP Easy =
+
[[File:HC-SR04 Mega Settings-Distance.jpg|500px]]
Use the device tab on the ESP Easy webinterface and create a new task by editing one of the available tasks. Select "Ultrasonic Sensor HC-SR04 from the dropdown box.
+
<br/>
 +
This configuration sets the HC-SR04 task to show the actual distance.<br/>
  
[[File:EasyConfigDistance.png]]
+
[[File:HC-SR04 Mega Tasklist.jpg|500px]]
 +
<br/>
 +
== Settings for Threshhold==
  
Enter the IDX found in the Domoticz device page. Also select the two GPIO pins that you have used to connect the HC-SR04 sensor. That should be all.
+
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 ==
 
== Optional settings ==
  
 
[[ EasyValueNames | Use of value names]]
 
[[ EasyValueNames | Use of value names]]

Latest revision as of 21:17, 11 January 2018

Introduction


HC-SR04.jpg
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.

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


ESPEasy

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