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
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables .
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
This image is created using Fritzing. Click to enlarge image
See The best way to supply power to the Arduino Nano and other components.
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.
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.
- 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.
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.