Ultrasonic sensors are useful little helpers that have become very present in today’s everyday life. Most people will know them from parking sensors, commonly found in practically all modern cars and trucks. They can, however, also be useful in many other projects, and this article serves as an introduction to ultrasonic sensors. It also explains how you can get it up and running in no time using an ESP8266 powered development board!
Product/Where to buy
The most troublesome part about this project is that the sensor board and the ESP8266 development board, which is an Adafruit Feather Huzzah in this case, use different voltage levels. The ultrasonic sensor requires a 5V power supply to function properly, and it also outputs a 5V signal. The ESP8266, on the other hand, only supplies 3.3V and the pins are, typically, also not 5V tolerant. On the Adafruit Feather Huzzah and many other ESP8266 development boards, there is, however, a 5V supply that comes directly from the USB plug. To solve the other problem, which is that the ultrasonic module outputs a 5V signal while the ESP8266 only tolerates up to 3.3V, the HC-SR04, linked above, includes two 10K resistors. You can connect them in series between the output pin of the module and GND to form a simple voltage divider. This reduces the output voltage to 2.5 Volts, which makes it safe to use with the ESP8266:
Luckily, it is easy to understand how the HC-SR04 works. The ESP8266 (or any other microcontroller) sends a 10µs pulse to the HC-SR04 ultrasonic sensor module over the trigger input pin. The module then sends out several short ultrasonic bursts and pulls the output pin high. Once all the pulses, sent out earlier, make their way back to the module, the HC-SR04 pulls the output pin low again. The microcontroller can measure the time between the output pin going high and it getting pulled low again. Once it knows the duration of the pulse, it can calculate the distance to an object. I implemented this behavior in the readDistance function in the following sketch