In this tutorial,we are going to interface ultrasonic sensor with Arduino.
Ultrasonic Library for proteus
CODE
#include<LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 10, 11, 12, 13);
#define trig 4
#define echo 5
int duration,distance=0;
void setup() {
lcd.begin(16, 2);
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
}
void loop() {
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
duration = pulseIn(echo, HIGH);
distance = (duration / 2) / 29.1;
lcd.setCursor(0, 0);
lcd.print("D:");
lcd.setCursor(3, 0);
lcd.print(distance);
if (distance < 10)
{
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("OBSTACLE");
}
delay(1000);
}
Comments
Post a Comment