Make your own distance measuring equipment using an ultrasonic sensor and an LCD. Here we use wokwi Arduino simulator

You can easily measure the distance between an object and the Arduino using an ultrasonic sensor. this project uses ultrasonic sensor and

Basics

The Ultrasonic sensor uses the shout and listens to the method. it transmits the signal and monitors the time it took to reach back. by using this principle, you can measure distance, diameter, depth and various linear parameters.

The ultrasonic sensor (or transducer) works on the same principles as a radar system. An ultrasonic sensor can convert electrical energy into acoustic waves and vice versa. The acoustic wave signal is an ultrasonic wave travelling at a frequency above 18kHz. The famous HC SR04 ultrasonic sensor generates ultrasonic waves at 40kHz frequency.

Typically, a microcontroller is used for communication with an ultrasonic sensor. To begin measuring the distance, the microcontroller sends a trigger signal to the ultrasonic sensor. The duty cycle of this trigger signal is 10µS for the HC-SR04 ultrasonic sensor. When triggered, the ultrasonic sensor generates eight acoustic (ultrasonic) wave bursts and initiates a time counter. As soon as the reflected (echo) signal is received, the timer stops. The output of the ultrasonic sensor is a high pulse with the same duration as the time difference between transmitted ultrasonic bursts and the received echo signal.

Source: https://www.microcontrollertips.com/principle-applications-limitations-ultrasonic-sensors-faq/

Project source: https://wokwi.com/arduino/projects/290043622233997832

Wokwi Arduino Simulator - Ultrasonic sensor

Wokwi Arduino Simulator - Ultrasonic sensor and LCD

Connection diagram

Connection diagram for the ultrasonic sensor with Arduino and an LCD display. Please leave a comment if you have any doubts.

Hint: You can also look at the code to understand the connections as well ;)

2021-05-17_18h12_21.png

Code

The complete code for the project is given below

        // YWROBOT
// Compatible with the Arduino IDE 1.0
// Library version:1.1
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,20,4);
// set the LCD address to 0x27 for a 16 chars and 2 line display

float cm;
float inches;

long readUltrasonicDistance(int triggerPin, int echoPin)
{
	pinMode(triggerPin, OUTPUT);  // Clear the trigger
	digitalWrite(triggerPin, LOW);
	delayMicroseconds(2);
	// Sets the trigger pin to HIGH state for 10 microseconds
	digitalWrite(triggerPin, HIGH);
	delayMicroseconds(10);
	digitalWrite(triggerPin, LOW);
	pinMode(echoPin, INPUT);
	// Reads the echo pin, and returns
	// the sound wave travel time in microseconds
	return pulseIn(echoPin, HIGH);
}

void setup()
{
  Serial.begin(9600);
  lcd.init();	// initialize the lcd
  // Print a message to the LCD.
  lcd.backlight();
  lcd.print("--> Distance <--");
	delay(3000);
	lcd.clear();
}

void loop()
{
  cm = 0.0344/2 * readUltrasonicDistance(3, 2);
	inches = (cm / 2.54);
  /*
	Serial.print("Inches ");
	Serial.print(inches, 1);
	Serial.print("\t");
  Serial.print("cm ");
	Serial.println(cm, 1);
	*/
	lcd.setCursor(0,0);
	lcd.print("Inches");
	lcd.setCursor(4,0);
	lcd.setCursor(12,0);
	lcd.print("cm");
	lcd.setCursor(1,1);
	lcd.print(inches, 1);
	lcd.setCursor(11,1);
	lcd.print(cm, 1);
	lcd.setCursor(14,1);
	delay(2000);
	lcd.clear();
}
    

Support

If you have any questions please leave a comment or hop on to Discord server to meet a welcoming group :)

Please visit the Electrotantra YouTube channel for more videos on the Arduino and Raspberry Pi Pico simulators.

share Open-Tech
Hardware enthusiast with ample interest in Arduino projects