A HC-SR04, HY-SRF05 driver and example for the esp8266.
#include "ping/ping.h"
....
static Ping_Data pingA;
....
// setup
int triggerPin = 0;
int echoPin = 2;
ping_init(&pingA, triggerPin, echoPin, PING_MM); // set the trigger and echo pin to the same number for one-pin mode
....
// loop:
float distance = 0;
float maxDistance = 500;
if (ping_pingDistance(&pingA, maxDistance, &distance) ) {
os_printf("Response ~ %d mm \n", (int)distance);
} else {
os_printf("Failed to get any response.\n");
}
Makefile:
MODULES = driver/stdout driver/easygpio driver/ping user
##Circuit The HC-SR04 is a 5V device, so you will (at the very least) need a voltage divider on the echo pin. Or even better: a logic level shifter.
If you want to run this in 'single pin mode' behind a logic level shifter you can connect the echo pin on the 3V3 side to the trigger GPIO via a 4KΩ:ish resistor. This works for a a classic MOSFET level shifter built out of two BSS138 and four 10KΩ resistors. Make sure that the esp GPIO reads low (input mode) when HC-SR04 echo pin is driven low. If it reads high the R2 resistor needs lower resistance.
Wiring echo & trigger together on the 5V side does not work with a classic mosfet level shifter.
Another working solution is a voltage divider (5V to 3.3V) between HC-SR04 echo and ground. Connect HC-SR04 trigger and esp GPIO to the middle of the divider.
The arduino library newping supports a whole range of ultrasonic sensors: SR04, SRF05, SRF06, DYP-ME007 & Parallax PING™. This without making any special hardware considerations in the code. So this library should work with those sensors as well.
inchessingle pin modemultiple sensorssdk v0.9.5 compabilitycheck compability SRF05SRF05 works w/o any modifications- check compability SRF06
- check compability DYP-ME007
- check compability Parallax PING™
GPL V3
The makefile is copied from esp_mqtt.
###Building and installing:
First you need to install the sdk and the easy way of doing that is to use esp_open_sdk.
You can put that anywhere you like (/opt/local/esp-open-sdk, /esptools etc etc)
Then you could create a small setenv.sh
file, containing the location of your newly compiled sdk and other platform specific info;
export SDK_BASE=/opt/local/esp-open-sdk/sdk
export PATH=${SDK_BASE}/../xtensa-lx106-elf/bin:${PATH}
export ESPPORT=/dev/ttyO0
(or setup your IDE to do the same)
To make a clean build, flash and connect to the esp console you just do this in a shell:
source setenv.sh # This is only needed once per session
make clean && make test
You won't be needing esptool, the makefile only uses esptool.py (provided by esp_open_sdk)
I have tested this with sdk v1.5.2, v0.9.4 and v0.9.5 (linux & mac)