I have some existing code that I've been using with an Uno and a HC-SR04. In order to make my project less expensive, I'm trying to port everything over to a ESP8266-01.

The last hang up I've been running into is that it appears that the ESP8266 doesn't support the pulseIn function.

duration = pulseIn(echoPin, HIGH);

Any idea on how I can change the pulseIn function with interrupts or something else?

Lastly I've done a lot of searching for using a HC-SR04 with a ESP8266 and while there are a couple examples out there, I haven't seen anyone do it with Arduino code...

  • This may be helpful, I have come across a library - newping.h that doesn't use pulseIn (bitbucket.org/teckel12/arduino-new-ping/wiki/Home). The code fails to compile when I include that whole library. I just want to be able to use the part to bypasses pulseIn and haven't figured that out yet. – Paul Masek Oct 2 '15 at 4:25
  • 1
    Can a ESP8266 and a HC-SR04 be friends? Have you tried food, wine, candles and romantic music? – Majenko Oct 2 '15 at 9:53

pulseIn() is actually a very simple function. As long as there is a microseconds counter of some form available you can easily enough roll your own.

Also, I don't know which implementation of the ESP8266 plugin you are using, but the one I use has pulseIn available.

  • I'm using the same version of ESP8266 plugin as you are, thanks for letting me know that pulseIn() is already supported! I didn't see any mention of it in the reference documentation - arduino.esp8266.com/versions/1.6.5-947-g39819f0/doc/…. I was also told that my code will compile even if all the functions aren't supported. – Paul Masek Oct 2 '15 at 17:35

I had trouble with pulseIn() until the proper initializing code was put in setup().
As follows:

digitalWrite(TRIGGER, LOW);
pinMode(ECHO, INPUT);

TRIGGER and ECHO were initially defined at top of code with other variables as follows:
#define TRIGGER 13 // Data pin D7
#define ECHO 12 // Data pin D6

My HC-SR04 board was wired to D7 and D6, but D1 and D2 are often used.

Project was a 'Tide' reporting system. A work in progress. Also found HC-SR04 board was unstable if using 3.3vdc as VCC. Works better if the HC-SR04 board VCC is tide to Vin on the ESP8266 board.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.