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.
|USB A to Mini-B USB cable
|(Optional) 9V Power Adapter for Arduino Nano
|(Recommended) Screw Terminal Adapter for Arduino Nano
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 (including pinout, how it works, and how to program), the following tutorials can help you:
This image is created using Fritzing. Click to enlarge image
Arduino Nano Code
※ 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.
- 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.
- 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.
- Attach the sensor to both hot and cold water, or hold it in your hand.
- Then, observe 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 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.