Arduino
초음파센서 거리를 I2C 문자 LCD에 표시하기
작성자 임베디드코리아
작성일25-10-28 11:31
조회135회
댓글0건
초음파센서 거리를 I2C 문자 LCD에 표시하기
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
int trigPin = 7;
int echoPin = 8;
long duration, distance;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
lcd.begin();
lcd.backlight();
lcd.print("Ultrasonic ");
}
void loop() {
long duration, cm;
lcd.clear();
lcd.print("Ultrasonic ");
digitalWrite(trigPin,LOW);
delayMicroseconds(2);
digitalWrite(trigPin,HIGH);
delayMicroseconds(10);
digitalWrite(trigPin,LOW);
duration = pulseIn(echoPin,HIGH);
cm = microsecondsToCentimeters(duration);
lcd.setCursor(5,1);
lcd.print(cm);
lcd.print("cm");
delay(1000);
}
long microsecondsToCentimeters(long microseconds){
return microseconds/29/2;
}