#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define IR_SENSOR_PIN 34 // Analog pin connected to IR sensor output
#define THRESHOLD 50 // Threshold for detecting disconnection
// LCD address and size
LiquidCrystal_I2C lcd(0x27, 16, 2); // Change address to 0x3F if 0x27 doesn't work
void setup() {
Serial.begin(115200);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("IR Sensor Readings");
}
void loop() {
int sensorValue = analogRead(IR_SENSOR_PIN); // Read the analog value from IR sensor
// Simple distance estimation (not exact, depends on your sensor)
float distance = map(sensorValue, 0, 4095, 1, 25); // Maps sensor value to 1mm to 25mm range
// Display readings on the LCD
lcd.clear();
lcd.setCursor(0, 0);
if (sensorValue < THRESHOLD) { // Check if sensor value is below threshold
lcd.print("No input detected");
lcd.setCursor(0, 1);
lcd.print("Connect sensor");
} else {
lcd.print("Distance: ");
lcd.print(distance, 1);
lcd.print(" mm");
lcd.setCursor(0, 1);
}
delay(1000); // Update every second
}