Skip to content

Instantly share code, notes, and snippets.

@AB-Project
Last active February 5, 2025 09:23
/*
Mini Project ตลับเมตรไร้สาย แสดงบนจอ LCD พัฒนาด้วย Arduino พร้อม Code โปรแกรม ตัวอย่าง
จากบทความ : https://www.ab.in.th/b/5
ภาพการต่อวงจร : https://dx.lnwfile.com/_/dx/_raw/3l/3d/y5.png
Download Library : https://download.เอบี.ไทย/d.php?file=Arduino-LiquidCrystal-I2C-library-master.zip
*/
#include <LiquidCrystal_I2C2.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); //Module IIC/I2C Interface บางรุ่นอาจจะใช้ 0x3f
#define trigPin D5
#define echoPin D6
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
lcd.begin();
lcd.backlight();
lcd.home();
lcd.print("Distance ");
}
void loop() {
long duration, distance;
digitalWrite(trigPin, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin, HIGH);
delayMicroseconds(10); // Added this line
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
lcd.setCursor(0, 1);
if (distance >= 200 || distance <= 0){
Serial.println("Out of range");
lcd.print("Out of range");
} else {
Serial.print(distance);
Serial.println(" cm");
lcd.print(distance);
lcd.print(" CM ");
}
delay(500);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment