ESP8266 - Ultrasonic Sensor - LCD
This tutorial instructs you how to use ESP8266 to obtain the distance from an ultrasonic sensor and show it on an LCD I2C display.
|Micro USB Cable
|(Optional) 5V Power Adapter for ESP8266
|(Optional) ESP8266 Screw Terminal Adapter
Or you can buy the following sensor kit:
|DIYables Sensor Kit 30 types, 69 units
Overview of Ultrasonic Sensor and LCD
If you are unfamiliar with Ultrasonic sensor and LCD (pinout, how it works, how to program ...), the following tutorials will provide you with the necessary information:
- If powering ESP8266 via USB port:
See more in ESP8266's pinout and how to supply power to the ESP8266 and other components.
- If powering ESP8266 via USB port but power is not enough, use an external power source for LCD and sensor
- If powering ESP8266 via Vin pin:
※ NOTE THAT:
The I2C address of LCD can differ depending on the manufacturer. In our code, we used 0x27, which is specified by DIYables manufacturer.
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.
- Attach the ESP8266 to your computer using a USB cable.
- Launch the Arduino IDE and select the correct board and port.
- Click to the Libraries icon on the left bar of the Arduino IDE.
- Search for “LiquidCrystal I2C” and locate the LiquidCrystal_I2C library by Frank de Brabander.
- Then, press the Install button to install the library.
- Copy the code and open it with the Arduino IDE.
- Click the Upload button in the Arduino IDE to compile and upload the code to the ESP8266.
- Attach the sensor to both hot and cold water, or hold it in your hand.
- Check out the outcome displayed on the LCD.
Check out the line-by-line explanation contained in the comments of the source code!
※ NOTE THAT:
- If the LCD screen is not displaying anything, please refer to Troubleshooting on LCD I2C.
- The code provided is only for educational purposes. The ultrasonic sensor is very sensitive to noise, so if you plan to use it in a practical setting, you should filter out any noise. For more information on how to do this, please see how to filter noise for ultrasonic sensor.