ESP8266 Peripherals: 5$ RCW-0001 Ultrasonic Range Sensor

There are many applications for distance measuring: in robotic as a cheap way to sense the environment and detect obstacles, in IoT applications to measure fill levels of containers containing food and in alarm systems to detect intruders – to name just a few.

I have been trying to use the popular HC-SR04 ultrasonic sensor together with the ESP8266 in the past but I wasn’t succesful. I guess this sensor needs 5V for proper operation and in order to do that I would have to add a voltage divider for the response pin to accommodate for the 3.3V of the ESP8266. So I went looking for a distance sensor working easily with the ESP8266 and I found it on Banggood for less than USD $5.

RCW-0001: 3.3V compatible distance sensor

Get it to work

To get it to work with the ESP8266 in the Arduino IDE you need a library or write the code yourself. The first library that I tried worked almost immediately. Just download the library from here:

Then go to Sketch > Include Library > Add Zip library.. and you are good to go. Sadly the library doesn’t seem to be listed in the Arduino Library manager but the download and import by ZIP is easy enough. Now go to File > Examples > NewPing > New Ping Example which opens a new Sketch window. Now just change the pins according to your wiring. I think most GPIO pins will work but I used D4 and D3 on the NodeMCU. Connect

  • VCC with 3.3V
  • GND and GND,
  • TRIG and D4 and
  • ECHO and D3

My Sketch looks like this:

#include <NewPing.h>

#define TRIGGER_PIN D4 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN D3 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 400 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {
 Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.

void loop() {
 delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
 Serial.print(&quot;Ping: &quot;);
 Serial.print(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)

Then open the serial console and you get a wonderful stream of distance measurements!

Where to buy?

Order it now from Banggood

Posted by Daniel Eichhorn

Daniel Eichhorn is a software engineer and an enthusiastic maker. He loves working on projects related to the Internet of Things, electronics, and embedded software. He owns two 3D printers: a Creality Ender 3 V2 and an Elegoo Mars 3. In 2018, he co-founded ThingPulse along with Marcel Stör. Together, they develop IoT hardware and distribute it to various locations around the world.

Leave a Reply