-->
Page 1 of 2

HC-SR04 driver

PostPosted: Wed Feb 04, 2015 11:37 am
by EadF
Hi

I've made an esp8266 driver for the HC-SR04 ultrasonic sensor: https://github.com/eadf/esp8266_ping

It uses two, user defined, pins for the sensor. As such it seems to work fine (at least under sdk 0.9.4)

I've seen that the arduino libraries are able to use just one pin for this sensor - basically trigger and echo are wired together. And that's where i got a problem. The HC-SR04 is a 5V device, so i use a level shifter to protect the esp.
It seems like level shifter prevents the HC-SR04 from working in single pin mode. When i wire the pins together the 5V side (HC-SR04) is a flat line (low). - Yes: the esp has disabled output after the inital pulse.

Arduino one pin:
Image

Esp two pins (i need to merge these pins to one)
Image

Could anyone of you help me with this small (electrical?) problem?

I jut got an idea.. maybe i instead should wire the pins together at the 3V3 side..

http://elinux.org/RPi_GPIO_Interface_Circuits#Level_Shifters

Re: HC-SR04 driver

PostPosted: Wed Feb 04, 2015 2:01 pm
by EadF
Yes, to connect the trigger and echo pins together with a 1kΩ resistor on the 3V3 side worked just fine.

These are the logic analyzer images i tried to display:
Arduino one pin
Esp two pins

Re: HC-SR04 driver

PostPosted: Wed Feb 04, 2015 3:03 pm
by alonewolfx2
i didnt see onewire hc-sr04 on your github where is it :)

Re: HC-SR04 driver

PostPosted: Wed Feb 04, 2015 4:54 pm
by EadF
Hi there Alonewolf!

Maybe the API is a bit cryptic, but you simply set trigger pin == echo pin. Like this:

Code: Select allint triggerPin = 0;
int echoPin = 0;
ping_init(triggerPin,echoPin); // sets one-pin mode on GPIO0


I could make a separate ping_initOnePinMode(int) function if anyone needs it.