Arduino Nano - Ultrasonic Sensor - LCD

This tutorial instructs you how to use Arduino Nano to obtain the distance from an ultrasonic sensor and show it on an LCD I2C display.

Hardware Preparation

1×Arduino Nano
1×USB A to Mini-B USB cable
1×LCD I2C
1×Ultrasonic Sensor
1×Jumper Wires
1×(Optional) 9V Power Adapter for Arduino Nano
1×(Recommended) Screw Terminal Adapter for Arduino Nano

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 (including pinout, how it works, and how to program), the following tutorials can help you:

Wiring Diagram

The wiring diagram between Arduino Nano and Ultrasonic Sensor LCD

This image is created using Fritzing. Click to enlarge image

Arduino Nano Code

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-ultrasonic-sensor-lcd */ #include <LiquidCrystal_I2C.h> #define TRIG_PIN 4 // The Arduino Nano pin connected to TRIG pin of ultrasonic sensor #define ECHO_PIN 3 // The Arduino Nano pin connected to ECHO pin of ultrasonic sensor LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27 (from DIYables LCD), 16 column and 2 rows 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 was specified by DIYables manufacturer.

Detailed Instructions

  • Connect your Arduino Nano to your computer using a USB cable.
  • Launch the Arduino IDE and select the appropriate 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 created by Frank de Brabander.
  • Then, click the Install button to install the library.
Arduino Nano LiquidCrystal I2C library
  • Copy the code and open it with the Arduino IDE.
  • Then, press the Upload button on the Arduino IDE to compile and upload the code to the Arduino Nano.
Arduino IDE Upload Code
  • Attach the sensor to both hot and cold water, or hold it in your hand.
  • Then, observe the outcome displayed on the LCD.

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 for help.
  • The code provided above is for educational purposes only. The ultrasonic sensor is very sensitive to noise, so if you want to use it in a real-world application, you should filter out the noise. For more information on how to do this, please refer to how to filter noise for ultrasonic sensor.

Video Tutorial