HC-SR04 distance sensor value via Serial proxy

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
abhelou
Normal user
Posts: 19
Joined: 22 Apr 2018, 21:29

HC-SR04 distance sensor value via Serial proxy

#1 Post by abhelou » 12 Jan 2023, 01:25

Hii
I installed distance sensor on espeasy for water tank level. as tank level on roof, espeasy can't connect to wifi, so i am trying to send the HC-SR04 distance to another ESP at home which is connected to the wifi.
i connected both ESP via rx-tx (D9-D10), it is sending to each other all the log as commands. so i turned off the serial from advanced menu.
now i enabled plugin Serial proxy, but i need someone's help to know how to send that distance from ESP on roof water tank, and parsing the data received on ESP at home.

TD-er
Core team member
Posts: 8728
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: HC-SR04 distance sensor value via Serial proxy

#2 Post by TD-er » 12 Jan 2023, 09:07

SerialProxy mainly receives data only to forward it to a MQTT controller.
It does not parse the received data.

SerialProxy is capable to send commands via serial.

But perhaps it is more practical to extend the wiring from the ESP to the sensor?
After all, you only need to send a pulse to trigger a measurement and receive a pulse from the echo pin.
So the tricky part here is to make it so that the wires won't pick up a lot of noise.
You apparently already have some cable for the serial data, so why not use that cable to extend the sensor?

For this I think it is best to use some ethernet cable as those have twisted pairs to eliminate noise.

As first test you could wire it like this:
- 1 pair for GND & trigger pulse
- 1 pair for GND & Echo pulse
- 1 pair for GND & power to the sensor

Then on the sensor side you may want to add some capacitor over the Vcc and GND pin to eliminate some noise.
For example a 10uF to keep the voltage stable + a 100nF to filter out the higher frequency noise.

You also need some level converters to convert the signal from the ESP to 5V TTL level and on the receiving end back to 3V3 for the ESP.
I would do this on the ESP side to let the level converter also act as an extra 'cleanup' step for the signal.

abhelou
Normal user
Posts: 19
Joined: 22 Apr 2018, 21:29

Re: HC-SR04 distance sensor value via Serial proxy

#3 Post by abhelou » 12 Jan 2023, 09:46

I have already tried to extend the sensor cable and added a one capacitor to stabilize the voltage over a cable of 30 meters, and it worked well, but not sure about its reliability without signal converter.
I have on the shelf some MAX485, does it do the job or I need different type of converters?

TD-er
Core team member
Posts: 8728
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: HC-SR04 distance sensor value via Serial proxy

#4 Post by TD-er » 12 Jan 2023, 10:00

I think using 2 of those may even be better as this allows to make a differential signal.
Then you could have both the trigger and the echo be connected to the RO and DI respectively (on the sensor side) and the DE & /RE could then be tied to the sending pin on both ends.

Just keep an eye on the MAX485 on the ESP side as not all work reliable when powered by 3V3.

But I do think using the MAX485 is by far the most elegant solution here.

abhelou
Normal user
Posts: 19
Joined: 22 Apr 2018, 21:29

Re: HC-SR04 distance sensor value via Serial proxy

#5 Post by abhelou » 12 Jan 2023, 12:09

I have both the trigger and the echo be connected to the RO and DI respectively.
crossed the A and B pins between 2 MAX485
what do you mean tie RE & DE to the sending pin ?
if D5 and D6 are used for the sensor on the ESP, which pins from second MAX485 should i connect to it?

TD-er
Core team member
Posts: 8728
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: HC-SR04 distance sensor value via Serial proxy

#6 Post by TD-er » 12 Jan 2023, 12:43

The "Data enable" and "Receive Enable" pins DE and /RE (inverted logic, hence the '/') are used in the MAX485 to select either "sending mode" or "listening" mode.
For this the inverted logic of the /RE pin makes it a lot simpler.

To send, you need to have the DE "high" and the RE "low" -> /RE (inverted state) is thus "high".
So the DE and /RE pin are often connected to each other as you need to be sending or listening and not both at the same time.
There are exceptions of course, that's why there are 2 separate pins.

Since we only have to 'send' a high value and are listening during the rest of the time, we can connect the DE, /RE and trigger pin all together.
So when sending out a trigger signal, we also enable sending and disable listening.

When done sending, the trigger pin is low, as is the DE and /RE, thus we're in listening mode.
N.B. the trigger pin on the ESP is connected to DI (Data in) on the MAX485 and the RO on the MAX485 to the trigger in GPIO on the ESP (via level converter or resistors)

The same applies on the other side.
The DI, DE and /RE pin on the MAX485 at the sensor side are tied together along with the "echo" pin of the sensor.
The RO of the MAX485 is connected to the trigger pin of the sensor.

TD-er
Core team member
Posts: 8728
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: HC-SR04 distance sensor value via Serial proxy

#7 Post by TD-er » 12 Jan 2023, 12:49

By the way, see the datasheet on what resistor to use between the "A" and "B" pins on each end.
https://www.analog.com/media/en/technic ... MAX491.pdf
Page 7 for some example schematic.

A typical value for these resistors is 120 Ohm.
This resistor value and the length of the cable will affect the delay you may see.
Since for this use case delay is affecting the measurement, you may need to re-calibrate the measurements slightly.
The delay will be in the order of magnitude of <1 usec, so it isn't that much. But this delay occurs twice in this use case.

abhelou
Normal user
Posts: 19
Joined: 22 Apr 2018, 21:29

Re: HC-SR04 distance sensor value via Serial proxy

#8 Post by abhelou » 12 Jan 2023, 13:29

"N.B. the trigger pin on the ESP is connected to DI (Data in) on the MAX485 and the RO on the MAX485 to the trigger in GPIO on the ESP (via level converter or resistors)" ?

you meant RO on the MAX485 to echo in GPIO on the ESP (via level converter or resistors)
am I right ?

abhelou
Normal user
Posts: 19
Joined: 22 Apr 2018, 21:29

Re: HC-SR04 distance sensor value via Serial proxy

#9 Post by abhelou » 12 Jan 2023, 14:06

ESP side:
DI, DE and /RE, tied three pins and connected to echo GPIO via voltage divider
RO pin connected to trigger GPIO

Sensor side:
DI, DE and /RE, tied three pins and connected to echo pin
RO pin connected to trigger pin

used 120ohm between A and B, using short cable for testing. connection between MAX485 should be straight ? A to A and B to B ?

i'm still not receiving the signal

Post Reply

Who is online

Users browsing this forum: No registered users and 17 guests