// --- CONSTANTS & CONFIGURATION ---
const float BETA = 3950; // Beta Coefficient for the Wokwi NTC Thermistor
const int SENSOR_PIN = A0; // Temperature sensor connected to Analog A0
const int RELAY_PIN = 13; // Relay control pin connected to Digital 13
const float TEMP_THRESHOLD = 40.0; // The threshold temperature
void setup() {
// Set the relay pin as an output
pinMode(RELAY_PIN, OUTPUT);
// Start Serial communication for debugging
Serial.begin(9600);
}
void loop() {
// 1. READ SENSOR
int analogValue = analogRead(SENSOR_PIN);
// 2. CALCULATE TEMPERATURE (NTC BETA EQUATION)
// This formula converts the non-linear resistance of the NTC to Celsius
float celsius = 1 / (log(1 / (1023.0 / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
// 3. DEBUGGING (View this in the Serial Monitor)
Serial.print("Current Temp: ");
Serial.print(celsius);
Serial.println(" °C");
// 4. LOGIC CONTROL
// Requirement: Buzzer ON if temp is LESS than 40 (< 40)
if (celsius < TEMP_THRESHOLD) {
digitalWrite(RELAY_PIN, HIGH); // Relay ON (Circuit Closed -> Buzzer Sounds)
} else {
digitalWrite(RELAY_PIN, LOW); // Relay OFF (Circuit Open -> Buzzer Silent)
}
// Small delay to stabilize readings
delay(500);
}