#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4); // I2C address 0x27, 20 columns and 4 rows
const int pirPin = 23; // PIR motion sensor output pin
const int ledPin = 2; // LED pin
const int ntcPin = 34; // Analog pin for NTC temperature sensor
void setup() {
Serial.begin(115200);
pinMode(pirPin, INPUT);
pinMode(ledPin, OUTPUT);
lcd.begin(20, 4); // initialize the lcd
lcd.init();
lcd.backlight();
lcd.clear();
}
void loop() {
int motionState = digitalRead(pirPin);
if (motionState == HIGH) {
// Motion detected
float temperature = readTemperature();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temperature: ");
lcd.print(temperature);
lcd.print(" C");
if (temperature < 10.0) {
digitalWrite(ledPin, HIGH);
lcd.setCursor(0, 1);
lcd.print("LED On");
} else {
digitalWrite(ledPin, LOW);
}
// Wait for 5 seconds (motion sensor delay time)
delay(5000);
} else {
// No motion
digitalWrite(ledPin, LOW);
lcd.clear();
}
}
float readTemperature() {
const float BETA = 3950; // should match the Beta Coefficient of the thermistor
// Read analog value from NTC temperature sensor
int analogValue = analogRead(ntcPin);
// Convert analog value to temperature in Celsius
float T2=273.15+25;
float vol=(float)(analogValue * 3.3 / 4096);
float Rt=vol*10000/(3.3-vol);
float Celsius=(float)1/(1/T2+log(Rt/10000)/BETA)-273.16;
return Celsius;
}