Arduino UNO R4 - Ultrasonic Sensor - LCD

This tutorial instructs you how to use Arduino UNO R4 to measure distance by using the ultrasonic sensor and display the distance on LCD I2C.

Arduino UNO R4 ultrasonic Sensor LCD I2C

Hardware Preparation

1×Arduino UNO R4 WiFi
1×Arduino UNO R4 Minima (Alternatively)
1×USB Cable Type-C
1×LCD I2C
1×Ultrasonic Sensor
1×Jumper Wires
1×(Recommended) Screw Terminal Block Shield for Arduino UNO R4
1×(Recommended) Breadboard Shield For Arduino UNO R4
1×(Recommended) Enclosure For Arduino UNO R4

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
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.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of Ultrasonic Sensor and LCD

Learn about the Ultrasonic sensor and LCD, including their pinout, functioning, and programming, in these tutorials:

Wiring Diagram

The wiring diagram between Arduino UNO R4 Ultrasonic LCD

This image is created using Fritzing. Click to enlarge image

Arduino UNO R4 Code

/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-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 2 // The Arduino UNO R4 pin connected to the ultrasonic sensor's TRIG pin #define ECHO_PIN 3 // The Arduino UNO R4 pin connected to the ultrasonic sensor's ECHO pin float duration_us, distance_cm; void setup() { lcd.init(); // initialize the lcd 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() { // generate 10-microsecond pulse to TRIG pin digitalWrite(TRIG_PIN, HIGH); delayMicroseconds(10); digitalWrite(TRIG_PIN, LOW); // measure duration of pulse from 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 for the LCD might differ from one manufacturer to another. In our example, we used the address 0x27, as recommended by the manufacturer DIYables.

Detailed Instructions

Follow these instructions step by step:

  • If this is your first time using the Arduino Uno R4 WiFi/Minima, refer to the tutorial on setting up the environment for Arduino Uno R4 WiFi/Minima in the Arduino IDE.
  • Connect the Arduino Uno R4 to the ultrasonic sensor and LCD I2C according to the provided diagram.
  • Connect the Arduino Uno R4 to your computer using a USB cable.
  • Launch the Arduino IDE on your computer.
  • Select the appropriate Arduino Uno R4 board (e.g., Arduino Uno R4 WiFi) and COM port.
  • Click on the Libraries icon on the left side of the Arduino IDE
  • Search for LiquidCrystal I2C and find the LiquidCrystal_I2C library by Frank de Brabander
  • Click the Install button to add the LiquidCrystal_I2C library.
Arduino UNO R4 LiquidCrystal I2C library
  • Copy the above code and open it in the Arduino IDE
  • Click the Upload button in the Arduino IDE to upload the code to the Arduino UNO R4
Arduino IDE Upload Code
  • Place the sensor in hot or cold water, or hold it in your hand.
  • Check the LCD for the result.

Code Explanation

Check the explanation in the comments within the code for each line.

※ NOTE THAT:

  • If nothing shows on the LCD, check here: Troubleshooting LCD I2C
  • The code provided is for educational use. The ultrasonic sensor can pick up a lot of unwanted signals. If you want to use this sensor effectively, you need to reduce this extra noise. Learn more here: Reducing 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!