Temperature sensor - mako

Mako
Mako
nome
Go to content
Arduino > Tutorials
Temperature and humidity sensor
Let's finish (for now) with a tutorial using a temperature and humidity sensor with Arduino UNO. In this tutorial a DHT11 sensor is used (with a breakboard) to read the two values and print them on the serial monitor. The DHT11 is a digital sensor for humidity and air temperature. These two parts are managed by a microcontroller which is an integral part of the sensor. The DHT 11 is pre-calibrated at the factory and the calibration data are stored in a read-only memory (OPT Memory).


Sensor Charateristics
Before moving on the sketch it is fair to specify some charateristics of the DHT11.
  • Temperature range: 0°C - 50°C/32°F - 122°F       +/-2°
  • Humidity range: 20% - 90%      +/-5%
  • Supply voltage: 3VDC - 5.5VDC
  • Current consumption: 0.5mA - 2.5mA
  • sampling period: 1sec

This type of sensors are really easy to use, so let's see the sketch right away.

p.s. when the length of the connection cable is less than 20 meters, and your sensor does not have a breakborad, it is advisable to insert a 4.7 Kohm pull-up resistor on the data line (pin 2).


The sketch

#include <DHT.h> 
#define DHTPIN 2 
#define DHTTYPE DHT11   
DHT dht(DHTPIN, DHTTYPE);  
void setup() {   
Serial.begin(9600);   
Serial.println(F("DHTxx test!"));    
dht.begin(); 
}  
void loop() {   
   delay(2000);      
float h = dht.readHumidity();      
float t = dht.readTemperature();      
float f = dht.readTemperature(true);    
 
if (isnan(h) || isnan(t) || isnan(f)) {     
Serial.println(F("Failed to read from DHT sensor!"));     
return;   
}    
float hif = dht.computeHeatIndex(f, h);      
float hic = dht.computeHeatIndex(t, h, false); 
Serial.print(F("Humidity: "));   
Serial.print(h);   
Serial.print(F("%  Temperature: "));   
Serial.print(t);   
Serial.print(F("°C "));   
Serial.print(f);   
Serial.print(F("°F  Heat index: "));  
Serial.print(hic);   
Serial.print(F("°C "));   
Serial.print(hif);   
Serial.println(F("°F")); }





Sketch Analysis
#define DHTPIN 2 = Digital pin connected to the DHT sensor
float h = dht.readHumidity(); = Read temperature as Celsius (default)
float t = dht.readTemperature(); = Read temperature as Fahrenheit (isFahrenheit = true)   
float f = dht.readTemperature(true); = Check if any reads failed and exit early (to try again).  
float hif = dht.computeHeatIndex(f, h); = Compute heat index in Fahrenheit (the default)      
float hic = dht.computeHeatIndex(t, h, false); = Compute heat index in Celsius (isFahreheit = false)
There are no reviews yet.
0
0
0
0
0
Back to content