// Define the pin numbers for the LED and PIR sensor
const int ledpin = 27; // Pin number for the LED
const int PIRinput = 26; // Pin number for the PIR sensor
// Variable to store the current state of the PIR sensor
int pirState = LOW;
// Analog input pin for the NTC thermistor
const int NTC_PIN = 25;
// Temperature threshold to trigger the alarm (adjust as needed)
const float TEMPERATURE_THRESHOLD = 30.0;
#define LIGHT_SENSOR_PIN 33 // ESP32 pin GIOP36 (ADC0)
// Setup function: runs once when the device starts up
void setup() {
// Set the LED pin as an output
pinMode(ledpin, OUTPUT);
// Set the PIR sensor pin as an input
pinMode(PIRinput, INPUT);
// Begin serial communication at a baud rate of 9600
Serial.begin(9600);
pinMode(NTC_PIN, INPUT);
}
// Loop function: runs repeatedly as long as the device is powered on
void loop() {
// Read the state of the PIR sensor
const int IP = digitalRead(PIRinput);
// Delay for stability
delay(100);
// If motion is detected by the PIR sensor
if (IP == 1) {
// Turn on the LED
digitalWrite(ledpin, HIGH);
// If this is the first time motion is detected since the last reset
if (pirState == LOW) {
// Print "Motion detected!" to the serial monitor
Serial.println("Motion detected!");
// Update pirState to indicate motion detection
pirState = HIGH;
}
// Delay for 1 second
delay(1000);
} else {
// If no motion is detected by the PIR sensor
// Turn off the LED
digitalWrite(ledpin, LOW);
// If motion was detected previously
if (pirState == HIGH) {
// Print "Motion ended!" to the serial monitor
Serial.println("Motion ended!");
// Update pirState to indicate no motion
pirState = LOW;
}
// Delay for 1 second
delay(1000);
}
int rawValue = analogRead(NTC_PIN);
float voltage = (rawValue / 4095.0) * 3.3; // Assuming ESP32 ADC has a 12-bit resolution (4096 levels) and operates at 3.3V
// Calculate resistance of NTC thermistor using voltage divider formula
float resistance = (3.3 * 10000) / voltage - 10000;
// Assuming a 10KΩ NTC thermistor, use Steinhart-Hart equation to calculate temperature
float A = 0.003354016; // Steinhart-Hart coefficients
float B = 0.000256985;
float C = 0.000002620131;
float tempK = 1 / (A + B * log(resistance) + C * pow(log(resistance), 3));
float tempC = tempK - 273.15;
Serial.print("Temperature: ");
Serial.print(tempC);
Serial.println("C");
if (tempC > TEMPERATURE_THRESHOLD) {
// Alarm condition
// You can add code here to trigger an external alarm or take other actions
}
delay(1000); // Wait for 1 second before updating temperature
// reads the input on analog pin (value between 0 and 4095)
int analogValue = analogRead(LIGHT_SENSOR_PIN);
Serial.print("Analog Value = ");
Serial.print(analogValue); // the raw analog reading
// We'll have a few threshholds, qualitatively determined
if (analogValue < 40) {
Serial.println(" => Dark");
} else if (analogValue < 800) {
Serial.println(" => Dim");
} else if (analogValue < 2000) {
Serial.println(" => Light");
} else if (analogValue < 3200) {
Serial.println(" => Bright");
} else {
Serial.println(" => Very bright");
}
delay(500);
}