ESP8266 - Ultrasonic Sensor - Relay
This tutorial instructs you how to use ESP8266 and ultrasonic sensor to control relay. In detail:
- ESP8266 activates the relay when the object is near the ultrasonic sensor
- ESP8266 deactivates the relay when the object is distant from the ultrasonic sensor
By connecting a relay to a light bulb, LED strip, motor or actuator, we can use ESP8266 and an ultrasonic sensor to control the light bulb, LED strip, motor or actuator.
Hardware Preparation
Or you can buy the following kits:
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables .
Overview of Relay and Ultrasonic Sensor
If you are not familiar with relay and ultrasonic sensor (pinout, how it works, how to program ...), the following tutorials will help you:
Wiring Diagram

This image is created using Fritzing. Click to enlarge image
The diagram below shows the same connections without a breadboard:

This image is created using Fritzing. Click to enlarge image
See more in ESP8266's pinout and how to supply power to the ESP8266 and other components.
ESP8266 Code
Detailed Instructions
To get started with ESP8266 on Arduino IDE, follow these steps:
- Check out the how to setup environment for ESP8266 on Arduino IDE tutorial if this is your first time using ESP8266.
- Wire the components as shown in the diagram.
- Connect the ESP8266 board to your computer using a USB cable.
- Open Arduino IDE on your computer.
- Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
- Plug the USB cable into the ESP8266 and the PC.
- Launch the Arduino IDE, choose the correct board and port.
- Copy the code and open it in the Arduino IDE.
- Click the Upload button in the Arduino IDE to send the code to the ESP8266.

- Place your hand in front of the sensor and observe how the relay's state is altered.
Code Explanation
Check out the line-by-line explanation contained in the comments of the source code!
※ NOTE THAT:
The code shown above is intended for educational purposes. The ultrasonic sensor is highly susceptible to noise. If you plan to use it in a practical setting, you should apply noise filtering to the ultrasonic sensor. For more information on how to filter noise from ultrasonic sensor distance measurements, please see this tutorial.
Relay vs MOSFET Module
A relay is not the only way to switch a device. If your device runs on DC power, such as a 12V solenoid lock, a 12V pump or a 12V fan, a MOSFET module can do the same job.
| Relay | MOSFET Module | |
|---|---|---|
| What it can do | turn the device on or off | turn the device on or off, and set its power (PWM) |
| Noise when switching | a click every time | no sound |
| How fast it switches | slow, a few milliseconds | very fast |
| Life | the contacts wear out after many switches | nothing moves, so it lasts long |
| Type of power | AC or DC | DC only |
| Device power supply | any voltage the relay is rated for | about 6V or more |
| Isolation from the ESP8266 | yes, between coil and contacts | yes, by the PC817 optocoupler |
| Good for | AC devices, or a simple ON/OFF | DC devices, or dimming and speed control |
Both the relay and the MOSFET module turn on when the ESP8266 pin is HIGH. A 3.3V ESP8266 pin is enough, because it only pushes a few milliamps through the LED of the PC817 optocoupler. So for a simple ON/OFF, the code above stays the same. Only the wiring changes. For AC mains devices, always use the relay.
To learn the MOSFET module pinout, wiring, PWM code and safety tips, see the ESP8266 - MOSFET Module tutorial.