Arduino and DHT22 Temperature and Humidity Sensor
Arduino and DHT22 Temperature and Humidity Sensor
Arduino and DHT22 Temperature and Humidity Sensor
blog
Hans-Petter Halvorsen
Contents
• Introduction to Arduino
• DHT11/DHT22
– Temperature and Humidity Sensor
• Arduino Examples
– Read Data from DHT22 Sensor
– Write Data to ThingSpeak
https://www.halvorsen.blog
Arduino
Hans-Petter Halvorsen Table of Contents
Arduino
• Arduino is an open-source electronics platform
based on easy-to-use hardware and software.
• It's intended for anyone making interactive projects,
from kids to grown-ups.
• You can connect different Sensors, like Temperature,
etc.
• It is used a lots in Internet of Things (IoT) projects
• Homepage:
https://www.arduino.cc
Arduino
• Arduino is a Microcontroller
• Arduino is an open-source platform
with Input/Output Pins (Digital
In/Out, Analog In and PWM)
• Price about $20
• Arduino Starter Kit ~$40-80
with Cables, Wires, Resistors, Sensors, etc.
Arduino UNO Digital ports (2-13)
6
Reset button
3
USB for PC
2
connection
5V, GND 4 5
Arduino UNO WiFi Rev 2
The Arduino Uno
WiFi is functionally
the same as the
Arduino Uno Rev3,
but with the
addition of WiFi /
Bluetooth and
some other
enhancements.
Arduino Software
Upload Code to Arduino Board Save
DHT11/22
Temperature and Humidity Sensor
DHT11 DHT22
• Good for 20-80% DHT22 is more precise,
humidity readings with more accurate and works
5% accuracy in a bigger range of
• Good for 0-50°C temperature and
temperature readings humidity, but its larger
±2°C accuracy and more expensive
• 1 Hz sampling rate • 0-100% RH
(once every second) • -40-125°C
• Price: a few bucks
Typically you need a 10K resistor, which you will want to use as a pullup
from the data pin to Vcc. This is included in the package.
DHT11/DHT22
DHT11 DHT22
VCC 1 2 4
VCC 1 2 4
3.3/5V DATA GND 3.3/5V DATA GND
Pin 3 is not in use Pin 3 is not in use
DHT22
1.5cm
3.5cm
DHTxx Resources
• https://learn.adafruit.com/dht
• https://www.sparkfun.com/datasheets/Senso
rs/Temperature/DHT22.pdf
https://www.halvorsen.blog
Arduino Examples
Hans-Petter Halvorsen Table of Contents
https://www.halvorsen.blog
Arduino
DHT11/22
Wires
Resistor 𝑅 = 10kΩ
DHT11/DHT22 Wiring
DI2
DHT
+5V 1 2 4 GND
GND
𝑅 = 10𝑘Ω
+5V
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
dht.begin();
}
void loop() {
delay(2000);
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print("% Temperature: ");
Serial.print(temperature);
Serial.println("°C");
}
Serial Monitor
https://www.halvorsen.blog
#define DHTPIN 2
#define DHTTYPE DHT22
Here you see the main code structure: DHT dht(DHTPIN, DHTTYPE);
WiFiClient client;
int wait = 20000;
float humidity;
We have created separate Functions for: float temperature;
• ThingSpeakWrite() }
ThingSpeak.begin(client);
void loop()
The Functions are presented on the next pages. {
ConnectWiFi();
ReadSensorData();
ThingSpeakWrite();
delay(wait);
}
Arduino Code
void CheckWiFi()
{
// check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
// don't continue
while (true);
}
String fv = WiFi.firmwareVersion();
if (fv != "1.0.0") {
Serial.println("Please upgrade the firmware");
}
}
void ConnectWiFi()
Secrets.h {
char ssid[] = SECRET_SSID;
#define SECRET_SSID "xxxxxx" char pass[] = SECRET_PASS;
#define SECRET_PASS "xxxxxx" if(WiFi.status() != WL_CONNECTED)
{
Serial.print("Attempting to connect to SSID: ");
#define SECRET_CH_ID xxxxxx Serial.println(SECRET_SSID);
while(WiFi.status() != WL_CONNECTED)
#define SECRET_WRITE_APIKEY "xxxxxx" {
WiFi.begin(ssid, pass);
Serial.print(".");
delay(5000);
}
Serial.println("\nConnected.");
}
}
Arduino Code
void ReadSensorData()
{
humidity = dht.readHumidity();
temperature = dht.readTemperature();
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print("% Temperature: ");
Serial.print(temperature);
Serial.println("°C");
}
Secrets.h
ThingSpeak.setField(1, humidity);
ThingSpeak.setField(2, temperature);
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if(x == 200){
Serial.println("Channel update successful.");
}
else{
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
}
Serial Monitor
ThingSpeak
Summary
• In this Tutorial we have been using a DHT22
Humidity and Temperature Sensor
• We connected the Sensor to Arduino and was able
to read both Humidity and Temperature Data from
the Sensor
• Finally, we also Logged the Humidity and
Temperature Data to the ThingSpeak Cloud
Service
References
• https://create.arduino.cc/projecthub/MinukaThes
athYapa/dht11-dht22-sensors-temperature-using-
arduino-b7a8d6
• https://create.arduino.cc/projecthub/mafzal/tem
perature-monitoring-with-dht22-arduino-15b013
• https://create.arduino.cc/projecthub/MisterBotBr
eak/how-to-use-temperature-and-humidity-dht-
sensors-
9e5975?ref=similar&ref_id=386990&offset=0
Hans-Petter Halvorsen
University of South-Eastern Norway
www.usn.no
E-mail: [email protected]
Web: https://www.halvorsen.blog