#include <DHT.h>
#include <Servo.h>
#define DHTPIN 2 // Pin where the DHT22 data is connected
#define DHTTYPE DHT22 // Define DHT sensor type as DHT22
DHT dht(DHTPIN, DHTTYPE); // Create a DHT object
Servo myServo; // Create a Servo object
void setup() {
Serial.begin(9600);
dht.begin(); // Initialize the DHT sensor
myServo.attach(9); // Attach the servo to pin 9
}
void loop() {
delay(2000); //Time between readings
// Read temperature as degrees Celsius
float temperature = dht.readTemperature();
// Print the temperature for debugging
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
// Map temperature (-40 to 80) to servo angle (0 to 180)
int angle = map(temperature, -40, 80, 0, 180);
// Constrain the angle to make sure it's within valid servo range
angle = constrain(angle, 0, 180);
// Move the servo to the mapped angle
myServo.write(angle);
}