Skip to content

Instantly share code, notes, and snippets.

@stupid-kid-af
Created March 21, 2022 14:02
Show Gist options
  • Save stupid-kid-af/747dea8bbd0757c3f5a083e4398d85ba to your computer and use it in GitHub Desktop.
Save stupid-kid-af/747dea8bbd0757c3f5a083e4398d85ba to your computer and use it in GitHub Desktop.
#include <LiquidCrystal_I2C.h>
const int trigPin = 9;
const int echoPin = 10;
long duration;
int distanceCm, distanceInch;
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
lcd.begin();
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(13, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
digitalWrite(13, HIGH);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distanceCm= duration*0.034/2;
distanceInch = duration*0.0133/2;
lcd.setCursor(0,0);
lcd.print("Distance: ");
lcd.print(distanceCm);
lcd.print(" cm");
delay(10);
lcd.setCursor(0,1);
lcd.print("Distance: ");
lcd.print(distanceInch);
lcd.print(" inch");
Serial.println(distanceCm);
Serial.println(distanceInch);
delay(10);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment