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.

Hardware Preparation

1×ESP8266 NodeMCU
1×Micro USB Cable
1×LCD I2C
1×Ultrasonic Sensor
1×Jumper Wires
1×(Optional) 5V Power Adapter for ESP8266
1×(Optional) ESP8266 Screw Terminal Adapter

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.

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:

Wiring Diagram

  • If powering ESP8266 via USB port:
The wiring diagram between ESP8266 NodeMCU and distance sensor LCD

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.

  • If powering ESP8266 via USB port but power is not enough, use an external power source for LCD and sensor
The wiring diagram between ESP8266 NodeMCU and Ultrasonic Sensor LCD extra power

This image is created using Fritzing. Click to enlarge image

  • If powering ESP8266 via Vin pin:
The wiring diagram between ESP8266 NodeMCU and Ultrasonic Sensor LCD Vin pin

This image is created using Fritzing. Click to enlarge image

ESP8266 Code

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-ultrasonic-sensor-lcd */ #include <LiquidCrystal_I2C.h> LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27 (from DIYables LCD), 16 column and 2 rows #define TRIG_PIN D5 // The ESP8266 pin connected to Ultrasonic Sensor's TRIG pin #define ECHO_PIN D7 // The ESP8266 pin connected to Ultrasonic Sensor's ECHO pin float duration_us, distance_cm; void setup() { lcd.init(); // Initialize the LCD I2C display lcd.backlight(); // open the backlight pinMode(TRIG_PIN, OUTPUT); // config trigger pin to output mode pinMode(ECHO_PIN, INPUT); // config echo pin to input mode } void loop() { // Produce a 10-microsecond pulse to the TRIG pin. digitalWrite(TRIG_PIN, HIGH); delayMicroseconds(10); digitalWrite(TRIG_PIN, LOW); // Measure the pulse duration from the ECHO pin duration_us = pulseIn(ECHO_PIN, HIGH); // calculate the distance distance_cm = 0.017 * duration_us; lcd.clear(); lcd.setCursor(0, 0); // start to print at the first row lcd.print("Distance: "); lcd.print(distance_cm); delay(500); }

※ 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.

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.
  • 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.
ESP8266 NodeMCU LiquidCrystal I2C 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.
Arduino IDE Upload Code
  • Attach the sensor to both hot and cold water, or hold it in your hand.
  • Check out the outcome displayed on the LCD.
The wiring diagram between ESP8266 NodeMCU and Ultrasonic LCD

This image is created using Fritzing. Click to enlarge image

Code Explanation

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.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!