In this tutorial, you will learn how to make a Distance Meter using Ultrasonic sensor with Ardu-X.
For this, you will need Ardu-X board, Ultrasonic Sensor and LCD Display. Connect Ultrasonic sensor with D2 Pin of Ardu-X and LCD with LCD Pin of Ardu-X as show in below image.

Ultrasonic sensor is connected on D2 Pin, means Trig Pin of Ultrasonic sensor is connected to D2 and Echo with D7. And LCD is connected with SDA and SCL Pins. So we have to code accordingly.
Code is given below, you can make your own logic too. To use block coding method click here or you can directly copy C++ code.

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
long readUltrasonicCM(int trig, int echo) {
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
long duration = pulseIn(echo, HIGH, 30000);
if (duration == 0) return -1;
return duration / 58;
}
void setup() {
lcd.init();
lcd.backlight();
}
void loop() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Distance:");
lcd.setCursor(10, 0);
lcd.print(readUltrasonicCM(2, 7));
delay(500);
}
Upload this code in Ardu-X. Now it will show distance in centemeter.