const int lm35Pin = 34; // Pin where the LM35 sensor is connected
const int buzzerPin = 19; // Pin where the buzzer is connected
const int ledPin = 23; // Pin where the LED is connected
// Temperature thresholds
const float tempLow = 19.0;
const float tempHigh = 25.0;
// Time tracking variables
unsigned long outOfRangeStartTime = 0;
const unsigned long thresholdTime = 10000; // 10 seconds
void setup() {
Serial.begin(115200); // Initialize serial communication
pinMode(lm35Pin, INPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(buzzerPin, LOW); // Turn off buzzer initially
digitalWrite(ledPin, LOW); // Turn off LED initially
}
void loop() {
float temperature = readTemperature(); // Read the temperature from the LM35 sensor
Serial.print("Temperature: ");
Serial.println(temperature);
if (temperature < tempLow || temperature > tempHigh) {
if (outOfRangeStartTime == 0) {
outOfRangeStartTime = millis(); // Start the timer if it wasn't already running
}
if (millis() - outOfRangeStartTime >= thresholdTime) {
digitalWrite(ledPin, HIGH); // Turn on LED
tone(buzzerPin, 1000); // Turn on buzzer with 1kHz tone
}
} else {
outOfRangeStartTime = 0; // Reset the timer
digitalWrite(ledPin, LOW); // Turn off LED
noTone(buzzerPin); // Turn off buzzer
}
delay(1000); // Wait for a second before next reading
}
float readTemperature() {
int analogValue = analogRead(lm35Pin); // Read the analog value from the LM35 sensor
float voltage = analogValue * (5.0 / 4095.0); // Convert the analog value to voltage
float celsius = voltage * 100.0; // Convert voltage to temperature in Celsius (LM35 provides 10mV per degree Celsius)
return celsius;
}