Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "DHT.h"
- #define DHTPIN 2 // Pin connected to the DHT sensor
- #define DHTTYPE DHT11 // DHT 22 (AM2302)
- #define MOTORPIN 8 // Pin connected to the motor
- // Initialize DHT sensor
- DHT dht(DHTPIN, DHTTYPE);
- void setup() {
- Serial.begin(9600);
- Serial.println();
- Serial.println("Status\tHumidity (%)\tTemperature (C)\t(F)");
- // Initialize the DHT sensor
- dht.begin();
- // Set motor pin as OUTPUT
- pinMode(MOTORPIN, OUTPUT);
- digitalWrite(MOTORPIN, LOW); // Ensure motor is off initially
- }
- void loop() {
- // Read humidity and temperature
- float hum = dht.readHumidity();
- float temp = dht.readTemperature();
- // Check if any reads failed
- if (isnan(hum) || isnan(temp)) {
- Serial.println("Failed to read from DHT sensor!");
- return;
- }
- // Print the values to the serial monitor
- Serial.print("Humidity: ");
- Serial.print(hum);
- Serial.print(" %, Temp: ");
- Serial.print(temp);
- Serial.println(" Celsius");
- // Control the motor based on temperature
- if (temp > 30) {
- digitalWrite(MOTORPIN, HIGH); // Turn the motor ON
- Serial.println("Motor: ON");
- } else {
- digitalWrite(MOTORPIN, LOW); // Turn the motor OFF
- Serial.println("Motor: OFF");
- }
- delay(2000); // Wait a few seconds between readings
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement