This circuit is designed to measure temperature and humidity using a DHT11 sensor interfaced with an Arduino UNO. The DHT11 sensor is a digital temperature and humidity sensor that provides calibrated digital output. A pull-up resistor is used on the data line of the DHT11 sensor to ensure a stable data transfer to the Arduino UNO. The Arduino UNO is programmed to read the sensor data, process it, and output the temperature and humidity readings to the serial monitor.
The code for the Arduino UNO is written in C++ and is designed to interface with the DHT11 sensor using the Adafruit DHT sensor library. The code initializes the sensor, reads temperature and humidity data, and prints the values to the serial monitor.
#include "Adafruit_Sensor.h"
#include "DHT.h"
#include "DHT_U.h"
#define DHTPIN 2 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11 // DHT 11
DHT_Unified dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
dht.begin();
Serial.println(F("DHTxx Unified Sensor Example"));
// Print temperature sensor details.
sensor_t sensor;
dht.temperature().getSensor(&sensor);
Serial.println(F("------------------------------------"));
Serial.println(F("Temperature Sensor"));
// Print the sensor details.
}
void loop() {
// Delay between measurements.
delay(dht.temperature().min_delay);
// Get temperature event and print its value.
sensors_event_t event;
dht.temperature().getEvent(&event);
if (!isnan(event.temperature)) {
Serial.print(F("Temperature: "));
Serial.print(event.temperature);
Serial.println(F("°C"));
}
// Get humidity event and print its value.
dht.humidity().getEvent(&event);
if (!isnan(event.relative_humidity)) {
Serial.print(F("Humidity: "));
Serial.print(event.relative_humidity);
Serial.println(F("%"));
}
}
The code includes the necessary libraries for the DHT sensor and defines the pin to which the sensor is connected. It initializes the sensor in the setup()
function and continuously reads the temperature and humidity in the loop()
function, printing the results to the serial monitor. The isnan()
function checks if the value read is 'not a number', which is a way to handle potential errors in reading the sensor.
The additional code files DHT.h
, DHT_U.h
, DHT.cpp
, DHT_U.cpp
, Adafruit_Sensor.h
, and Adafruit_Sensor.cpp
are part of the Adafruit DHT sensor library and provide the necessary functions and definitions to interface with the DHT11 sensor. These files should be included in the Arduino project to compile and upload the code successfully to the Arduino UNO.