Skip to Content
This is the Beta version of our new Learning Paths approach. Please email feedback.

Temperature and Humidity Sensor

temperature_humidity_sensor.ino
/* Project: Smart Home Circuit: Arduino Ek R4 Project Description: This is the code to detect temperature and humidity using a DHT22 sensor. Author: STEMVentor Educonsulting This code is copyrighted. Please do not reuse or share for any purpose other than for learning with subscribed STEMVentor programs. This code is for educational purposes only and is not for production use. */ // LIBRARIES // You will need to install the library named "DHT sensor library" by Adafruit. // This will also need you to install the library named "Adafruit Unified Sensor" by Adafruit. #include <DHT.h> //for DHT11/DHT22 sensor // PIN DEFINITIONS #define DHT_PIN 2 // GLOBAL CONSTANTS // GLOBAL VARIABLES // INITIALIZE OBJECTS /* Libraries usually follow an object-oriented approach that requires * an instance of the class to call its methods. */ // DHT configuration #define DHT_TYPE DHT22 // if using DHT 22 // #define DHTTYPE DHT11 // if using DHT 11 DHT dht(DHT_PIN, DHT_TYPE); //DHT object /* LOCAL FUNCTIONS */ // Setup DHT22 sensor. void DHTSetup() { // Initialize the DHT object. dht.begin(); Serial.println("The DHT22 sensor is ready!"); printToLCDByRow(1, "Temperature: -", true); printToLCDByRow(2, "Humidity: -", false); } // Read DHT22 sensor values. void readDHTValues() { // Reading temperature or humidity takes about 250 milliseconds! // Sensor readings may also be up to 2 seconds 'old' (it's a very slow sensor) float humidity_reading = dht.readHumidity(); // Read temperature as Celsius (parameter: false, the default) float temperature_reading_celsius = dht.readTemperature(false); // Read temperature as Fahrenheit (parameter: true) float temperature_reading_fahrenheit = dht.readTemperature(true); // Check if any reads failed and exit early (to try again). if (isnan(humidity_reading) || isnan(temperature_reading_celsius) || isnan(temperature_reading_fahrenheit)) { Serial.println(F("Failed to read from DHT sensor!")); return; } // The library provides a function to calculate the heat index which // is value derived with a formula with temperature and humidity values. // Compute heat index with temperature in Celsius (last parameter: false) float heat_index_celsius = dht.computeHeatIndex(temperature_reading_celsius, humidity_reading, false); // Compute heat index with temperature in Fahrenheit (last parameter: true) float heat_index_fahrenheit = dht.computeHeatIndex(temperature_reading_fahrenheit, humidity_reading, true); // Serial.print(F("Humidity: ")); // Serial.println(humidity_reading); // Serial.print(F("% Temperature: ")); // Serial.print(temperature_reading_celsius); // Serial.print(F("°C ")); // Serial.print(F("/")); // Serial.print(temperature_reading_fahrenheit); // Serial.print(F("°F Heat index: ")); // Serial.print(heat_index_celsius); // Serial.print(F("°C ")); // Serial.print(heat_index_fahrenheit); // Serial.println(F("°F")); // Print the temperature in °C and °F Serial.print("Temperature:" + String(temperature_reading_celsius) + "°C"); Serial.print(F("/")); Serial.println(String(temperature_reading_fahrenheit) + "°F"); // Print the humidity in % Serial.println("Humidity:" + String(humidity_reading) + "%"); // Print the heat index in °C and °F Serial.print("Heat Index:" + String(heat_index_celsius) + "°C"); Serial.print("/"); Serial.println(String(heat_index_fahrenheit) + "°F"); // printToLCDByRow(1, "Temperature:" + String(temperature_value) +"deg C", true); // printToLCDByRow(2, "Humidity:" + String(humidity_value) + "%", false); }