// Define constants
const int temperaturePin = A0; // Analog pin for temperature sensor
const int fanControlPin = 2; // Digital pin for relay control switch which turns on and of the fan
const float BETA = 3950; // should match the Beta Coefficient of the thermistor, took it from wokwi docs
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Set relay control pin as output and temperature sensor pin
pinMode(fanControlPin, OUTPUT);
pinMode(temperaturePin, INPUT);
}
void loop() {
// Read temperature from the sensor
int analogValue = analogRead(A0);
//here converting data from the sensor to temperature
float celsius = 1 / (log(1 / (1023. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
//Printing the temperature on a sensor
Serial.println(celsius);
if (celsius > 27)// if temperature on the sensor is high
{
digitalWrite(fanControlPin, HIGH);// Turn on the fan
}
else if (celsius <= 25)
{
digitalWrite(fanControlPin, LOW);// Turn off the fan
}
// Add a delay to avoid rapid switching
delay(100);
}