Arduino and DHT22 Temperature and Humidity Sensor

Download as pdf or txt
Download as pdf or txt
You are on page 1of 36

https://www.halvorsen.

blog

Arduino and DHT22


DHT22 Temperature and Humidity Sensor

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

External Power Analog In ports (0-5)


Supply 1

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

Open Serial Monitor


Compile and Check
if Code is OK
Open existing Code
In this window
Creates a New Code Window you create your
Program
The software can be
downloaded for free:
Error Messages
www.arduino.cc can be seen here
https://www.halvorsen.blog

DHT11/22
Temperature and Humidity Sensor

Hans-Petter Halvorsen Table of Contents


DHT11/DHT22
DHT11/DHT22 are Breadboard friendly and easy to wire. They use a single-wire to send data.

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

Read Humidity and


Temperature Data
Hans-Petter Halvorsen Table of Contents
Equipment
Breadboard

Arduino
DHT11/22
Wires
Resistor 𝑅 = 10kΩ
DHT11/DHT22 Wiring
DI2
DHT

+5V 1 2 4 GND
GND
𝑅 = 10𝑘Ω

+5V

A Resistor (𝑅 = 10𝑘Ω) must be


connected between the wire
of pin1 and pin2 on the DHTxx
Sensor (“Pull-up Resistor)
DHT Library
Use the Arduino Library Manager in order
to download the DHT Arduino Library
DHT Library
The DHT Arduino Library depends on some
other libraries that you need to install as well
Arduino Code
#include "DHT.h"

#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

void setup() {
Serial.begin(9600);
dht.begin();
}

void loop() {
delay(2000);

float humidity = dht.readHumidity();


float temperature = dht.readTemperature();

// Check if any Errors


if (isnan(humidity) || isnan(temperature)) {
Serial.println("Error reading DHT sensor");
}

Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print("% Temperature: ");
Serial.print(temperature);
Serial.println("°C");
}
Serial Monitor
https://www.halvorsen.blog

Log Sensor Data to


ThingSpeak
Hans-Petter Halvorsen Table of Contents
Log Data to ThingSpeak
• In this Example we will read Humidity and
Temperature data from the DHT22 Sensor
(same as previous example)
• Then we will Write Humidity and Temperature
data to the ThingSpeak Cloud Service
ThingSpeak
• ThingSpeak is an IoT analytics platform service
that lets you collect and store sensor data in the
cloud and develop Internet of Things (IoT)
applications.
• ThingSpeak has a free Web Service (REST API) that
lets you collect and store sensor data in the cloud
and develop Internet of Things applications.
• It works with Arduino, Raspberry Pi, MATLAB and
LabVIEW, Python, etc.
https://thingspeak.com
ThingSpeak https://thingspeak.com
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.
ThingSpeak Library
Arduino Code
#include "DHT.h"
#include "ThingSpeak.h"
#include <WiFiNINA.h>
#include "secrets.h"

#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;

• CheckWiFi() void setup()


{
• ConnectWiFi() Serial.begin(9600);
dht.begin();
• ReadSensorData() CheckWiFi();

• 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();

// Check if any Errors


if (isnan(humidity) || isnan(temperature))
{
Serial.println("Error reading DHT sensor");
}

Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print("% Temperature: ");
Serial.print(temperature);
Serial.println("°C");
}
Secrets.h

Arduino Code #define SECRET_SSID "xxxxxx"


#define SECRET_PASS "xxxxxx"

#define SECRET_CH_ID xxxxxx

void ThingSpeakWrite() #define SECRET_WRITE_APIKEY "xxxxxx"


{
unsigned long myChannelNumber = SECRET_CH_ID;
const char * myWriteAPIKey = SECRET_WRITE_APIKEY;

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

You might also like