// include the library for the temperature sensor
#include <OneWire.h>
#include <DallasTemperature.h>
// set up the pin for the temperature sensor
#define ONE_WIRE_BUS 2
// create an instance of the OneWire and DallasTemperature libraries
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
// set up the pin for the fan
int fanPin = 3;
void setup() {
// start serial communication
Serial.begin(9600);
// start the temperature sensor
sensors.begin();
// set the fan pin to output mode
pinMode(fanPin, OUTPUT);
}
void loop() {
// call the sensors.requestTemperatures() function to get the temperature reading
sensors.requestTemperatures();
// get the temperature reading in degrees Celsius
float temperatureC = sensors.getTempCByIndex(0);
// print the temperature reading to the serial monitor
Serial.print("Temperature: ");
Serial.print(temperatureC);
Serial.println(" C");
// check if the temperature is above 90 degrees Celsius
if (temperatureC >= 90) {
// turn on the fan
digitalWrite(fanPin, HIGH);
}
// check if the temperature is below 70 degrees Celsius
else if (temperatureC <= 70) {
// turn off the fan
digitalWrite(fanPin, LOW);
}
// wait for 1 second before checking the temperature again
delay(1000);
}