The DHT22 (also known as AM2302) is a more accurate and versatile temperature and humidity sensor compared to the DHT11, offering a wider temperature and humidity range, better accuracy, and a wider temperature range, but at a higher cost.
DHT-11 comes in 3-pin and 4 pin versions
#include <DHT11.h>
#define DHT11_PIN 2
void setup(){
Serial.begin(9600);
}
void loop(){
int chk DHT.read(DHT11_PIN);
Serial.println("temp:");
Serial.print(DHT.temperature);
Serial.print(" humi:");
Serial.println(DHT.humidity);
delay(1000);
}
DHT-22 Code Sample:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>// Liquid Crystal by Arduino, Adafruit
#include <DHT22.h> //DHT22 library by dvarrel
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
//Connect to 5V, GND, SDA to A4 , SCL to A5
#define pinDATA 2
//Connect DHT22 to +5V, GND, and Data to Pin 2
DHT22 dht22(pinDATA); //define pin data
void setup()
{
// Initialize the LCD
lcd.begin();
lcd.backlight();
Serial.begin(9600); //1bit=10µs
Serial.println("\ntest capteur DTH22");
}
void loop()
{
float t = dht22.getTemperature();
float h = dht22.getHumidity();
if (dht22.getLastError() != dht22.OK) {
Serial.print("last error :");
Serial.println(dht22.getLastError());
}
Serial.print("h=");Serial.print(h,1);Serial.print("\t");
Serial.print("t=");Serial.println(t,1);
delay(2000); //Collecting period should be : >1.7 second
lcd.clear();
// Display humidity on line 0
lcd.setCursor(0, 0); // First row
lcd.print("Humidity= ");
lcd.print(h);
// Display temperature on line 1
lcd.setCursor(0, 1); // Second row
lcd.print("Temperature= ");
lcd.print(t);
delay(5000); // Wait 5 seconds
}
Thingspeak:
Write API Key: AC702TNVD5X4CCO9
Read API Key. 3V2UUA1Y3TBDWVNU
Channel ID. 2414066
#define motorPin 9
//------------------------------
#include <DHT22.h>
#define pinDATA 3 // Digital pin D3
DHT22 dht22(pinDATA);
//-----------------------------------
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH110X.h>
// I2C address for most SH1106 displays
#define I2C_ADDRESS 0x3C
// Create display object (128x64 SH1106)
Adafruit_SH1106G display(128, 64, &Wire);
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
// Most 0.96" OLEDs are address 0x3C. If yours is 0x3D, change it.
#define OLED_ADDR 0x3C
#include <WiFi.h>
#include "secrets.h"
#include "ThingSpeak.h" // always include thingspeak header file after other header files and custom macros
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password
int keyIndex = 0; // your network key Index number (needed only for WEP)
WiFiClient client;
unsigned long myChannelNumber = SECRET_CH_ID;
const char * myWriteAPIKey = SECRET_WRITE_APIKEY;
// Initialize our values
int number1 = 20;
int number2 = 20;
int number3 = 20;
String myStatus = "";
void setup() {
Serial.begin(115200); //Initialize serial
Serial.println("\ntest capteur DTH22");
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo native USB port only
}
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client); // Initialize ThingSpeak
Wire.begin();
// Initialize display
if (!display.begin(I2C_ADDRESS, true)) {
while (1); // Stop if display fails
}
display.clearDisplay();
}
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
sensorValue=map(sensorValue, 40, 1000, 0, 200); //change range of value
Serial.println(sensorValue);
analogWrite(motorPin, sensorValue);
delay(1000); // delay in between reads for stability
//-------------------------------------
float t = dht22.getTemperature();
float h = dht22.getHumidity();
if (dht22.getLastError() != dht22.OK) {
Serial.print("last error :");
Serial.println(dht22.getLastError());
}
//------------------------------------------------------
Serial.print("h=");Serial.print(h,1);Serial.print("\t");
Serial.print("t=");Serial.println(t,1);
delay(2000); //Collecting period should be : >1.7 second
// nothing needed
// Connect or reconnect to WiFi
if(WiFi.status() != WL_CONNECTED){
Serial.print("Attempting to connect to SSID: ");
Serial.println(SECRET_SSID);
while(WiFi.status() != WL_CONNECTED){
WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network. Change this line if using open or WEP network
Serial.print(".");
delay(5000);
}
Serial.println("\nConnected.");
}
display.clearDisplay();
// Set text properties
display.setTextSize(2);
display.setTextColor(SH110X_WHITE);
display.setCursor(0,0);
display.print("T= "); // x, y (top-left)
display.println(t);
display.print("H= ");
display.println(h);
display.print("L= ");
display.println(sensorValue);
display.display(); // push to screen
// set the fields with the values
ThingSpeak.setField(1, t);
ThingSpeak.setField(2, sensorValue);
ThingSpeak.setField(3, h);
// set the status
ThingSpeak.setStatus(myStatus);
// write to the ThingSpeak channel
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));
}
delay(20000); // Wait 20 seconds to update the channel again
}