/* * Lab 1 - Task 6: Digital Sensor (DHT22)
* Objective: Read temperature and humidity using the DHT library.
*/
#include "DHT.h"
#define DHTPIN 15 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22 // Define the sensor type: DHT22 (AM2302)
// Initialize DHT sensor
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
Serial.println("Task 6 Ready: DHT22 Sensor Test!");
dht.begin(); // Start the DHT sensor
}
void loop() {
// Wait a few seconds between measurements.
// The DHT22 requires about 2 seconds to process new data.
delay(2000);
// Reading temperature or humidity takes about 250 milliseconds!
// Read humidity as percentage
float humidity = dht.readHumidity();
// Read temperature as Celsius (the default)
float temperature = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor! Check wiring.");
return; // Stop the rest of the loop and start over
}
// Display the results on the Serial Monitor
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
}