/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-cooling-system-using-ds18b20-temperature-sensor */ //Modified by Kishore Gaikwad, 23-Apr-2022 #include #include #define TEMP_UPPER_THRESHOLD 36 // upper temperature threshold //#define TEMP_LOWER_THRESHOLD 34 // lower temperature threshold #define SENSOR_PIN 12 // ESP32 pin connected to DS18B20 sensor's DQ pin #define RELAY_FAN_PIN 26 // ESP32 pin connected to relay OneWire oneWire(SENSOR_PIN); DallasTemperature DS18B20(&oneWire); void setup() { Serial.begin(115200); // initialize serial DS18B20.begin(); // initialize the DS18B20 sensor pinMode(RELAY_FAN_PIN, OUTPUT); } void loop() { DS18B20.requestTemperatures(); // send the command to get temperatures float temperature = DS18B20.getTempCByIndex(0); // read temperature in Celsius Serial.print(temperature); Serial.println("ÂșC"); if (temperature > TEMP_UPPER_THRESHOLD) { Serial.println("Turn the fan on"); digitalWrite(RELAY_FAN_PIN, LOW); // turn on } else if (temperature < TEMP_UPPER_THRESHOLD) { Serial.println("Turn the fan off"); digitalWrite(RELAY_FAN_PIN, HIGH); // turn off } delay(500); }