Luka Code

Code ya Arduino mpe Video ya Senseur ya Température mpe Humidité DHT22

Code ya Arduino mpe Video ya Senseur ya Température mpe Humidité DHT22

Na boyekoli oyo, tokoyekola ndenge ya kosalela sensor ya température mpe ya malili ya DHT22 na Arduino. DHT22, oyo eyebani mpe na kombo ya AM2302, ekoki komeka température kobanda -40 kino 80 degré Celsius mpe malili kobanda 0 kino 99 pourcent. Na kolanda malakisi oyo, okokoka kolakisa température na Celsius, Fahrenheit, to Kelvin, mpe mpe niveau ya malili.

DHT22 sensor with PCB-1

Tokosalela bibliothèque ya sensor ya DHT mpo na kotanga kutu ba données na sensor. Bibliothèque oyo ekokitisa mosala ya kosolola na DHT22 mpe epesa biso nzela ya kozwa ba valeurs ya température mpe ya malili na ba lignes ya code moke. Mpo na ndimbola ya malamu koleka, nalingi okende kotala vidéo oyo elandi (na vidéo na 00:00).

Matériel Explicado

Élément principal na projet oyo ezali module ya sensor ya DHT22, oyo ezali sensor ya numérique ya température mpe ya malili. Esalelaka élément ya capacitance mpo na malili mpe thermistor mpo na komeka mopepe ya zingazinga. Bimeli ezali signal ya numérique oyo Arduino ekoki kotanga.

DHT22 esalaka na voltage kobanda 3.3 kino 5 volts mpe esololaka na nzela ya fil unique, oyo esalaka ete ezala pete mpo na kosangisa na ba projets na yo. Ezali mpe na distansi ya molai ya kopesa message kino na mètres 20, oyo epesaka nzela ya kotia sensor na esika oyo esengeli.

Détails ya Datasheet

FabricantAosong
Numéro ya pièceDHT22 (AM2302)
Voltage ya logique/IO3.3 - 5 V
Voltage ya alimentation3.3 - 5 V
Étendue ya komeka (Température)-40 kino +80 °C
Étendue ya komeka (Malili)0 kino 99 %
Précision (Température)±0.5 °C
Précision (Malili)±2 % na 25 °C
Résolution0.1 °C / 0.1 %
Distansi ya kopesa messagekino 20 m
PaquetModule ya ba pins 4

  • Kokamba ete alimentation ya malamu ezala kati na 3.3V mpe 5V.
  • Salela résistance ya pull-up ya 10K kati na pin ya données mpe alimentation.
  • Kokamba ba fils ya sensor mpo na ba lectures ya malamu.
  • Kokima kotuna mbala na mbala na lombangu; pesa ntango kati na ba lectures.
  • Keba na ba facteurs ya environnement oyo ekoki kobongola ba lectures.

Malakisi ya Wiring

Arduino wiring for DHT22 sensor
Arduino wiring for DHT22 sensor

Mpo na kosangisa sensor ya DHT22, banda na kosangisa pin ya puissance (pin 1) ya sensor na sortie ya 5V na Arduino. Sima, sangisa pin ya terre (pin 4) na moko ya ba pins ya GND na Arduino. Pin ya données (pin 2) esengeli kosangisama na pin ya digitale 2 na Arduino mpo na kosolola. Libanda na yango, tia résistance ya 10K kati na pin ya données mpe pin ya puissance mpo na kozwa ba lectures ya malamu.

Soki ozali kosalela pin mosusu mpo na données, kokanisa ete obongola code na ndenge ya esengeli na kobongola valeur ya #define DHTPIN mpo na kopona pin oyo oponi. Mise en place esengeli kozala pete, kolanda ba connexions oyo ekolakisa ete sensor na yo esalaka malamu.

Ba Exemples ya Code & Explication

Code oyo elandi ebandisaka sensor ya DHT22 mpe etangaka ba valeurs ya température mpe ya malili. Liboso, tozali kosangisa bibliothèque ya DHT mpe tozali kokanisa pin oyo sensor esangisami na yango:

#include "DHT.h"
#define DHTPIN 2     // pin ya digitale oyo tozali kosangisa na yango
#define DHTTYPE DHT22   // DHT 22  (AM2302)
DHT dht(DHTPIN, DHTTYPE);

Awa, variable DHTPIN esili kotiama na 2, oyo elakisi ete pin ya données ya sensor esangisami na pin ya digitale 2 ya Arduino. DHTTYPE elakisi lolenge ya sensor oyo esalelami, oyo na likambo oyo ezali DHT22.

Na sima, na fonction setup(), tozali kobandisa sensor mpe moniteur ya série:

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

Code oyo ebandisaka communication ya série mpe ebongisaka sensor ya DHT mpo na kotanga. Serial.begin(9600) etiaka taux ya baud mpo na communication ya série.

Na fonction loop(), tokoki kotanga mpe kolakisa ba valeurs ya température mpe ya malili:

Serial.print("Température: ");
Serial.print(getTemp("c")); // Celsius
Serial.print(" *C ");
Serial.print(getTemp("h")); // Malili
Serial.println(" % ");

Partie oyo ya code epesaka température na Celsius mpe pourcentage ya malili na moniteur ya série. Fonction getTemp() esalelami mpo na kozwa données oyo esengeli kolandana na paramètre oyo epesami.

Démonstration / Nini Okoki Kozela

Ntango nyonso esili kobongisama mpe code esili kotiama, okomona ba lectures ya température mpe ya malili na moniteur ya série. DHT22 ekoki kozwa mwa ntango mpo na kozala stable, boye keba ete opesa ba secondes moke kati na ba lectures (na vidéo na 15:00). Soki ezali na mikakatano na ba lectures, tala wiring na yo mpe keba ete ba connexions ezali ya makasi. Ba erreurs ya mbala mingi ezali ba pin oyo esengeli te to mikakatano ya alimentation.

Ba Timestamps ya Vidéo

  • 00:00 - Introduction na Sensor ya DHT22
  • 01:30 - Wiring ya Sensor
  • 03:00 - Explication ya Code
  • 04:30 - Démonstration ya Code
  • 06:00 - Conclusion

Images

Arduino wiring for DHT22 sensor
Arduino wiring for DHT22 sensor
DHT22 sensor with PCB-1
DHT22 sensor with PCB-1
DHT22 with PCB red
DHT22 with PCB red
DHT22 sensor no PCB
DHT22 sensor no PCB
54-Arduion code DHT22 Temperature and Humidity Sensor Module
Langue: C++
++
/*
 * This is the Arduino code for the DHT22 module to read temperature and humidity.
 * This code can display temperature in:
 * getTemp("c") is used to get Celsius.
 * getTemp("f") is used to get Fahrenheit.
 * getTemp("k") is used for Kelvin.
 * getTemp("hif") is used to get Fahrenheit.
 * getTemp("hic") is used to get Celsius.
 * getTemp("h") is used to get humidity.
 * Watch the video https://youtu.be/oi_GPSLjgBY
 * Other Arduino library and videos https://robojax.com
 * 
 * Written/updated by Ahmad Shamshiri for Robojax.com Video
 * Date: Jan 08, 2018, in Ajax, Ontario, Canada
 * Permission granted to share this code given that this
 * note is kept with the code.
 * Disclaimer: This code is "AS IS" and for educational purposes only.
 * 
 * original source code : https://github.com/adafruit/DHT-sensor-library
 */

// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain


#include "DHT.h"

#define DHTPIN 2     // what digital pin we're connected to

// Uncomment whatever type you're using!
//#define DHTTYPE DHT11   // DHT 11
#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

// Connect pin 1 (on the left) of the sensor to +5V
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
// to 3.3V instead of 5V!
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors.  This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster processors.
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  Serial.println("DHTxx Robojax test!");

  dht.begin();
}

void loop() {
  // Wait a few seconds between measurements.
  delay(2000);


  // Robojax.com test video
  Serial.print("Temperature: ");
  
  Serial.print(getTemp("c"));
  
  Serial.print(" *C ");
  Serial.print(getTemp("f"));
  Serial.println (" *F");
  Serial.println("-----------------");  
  Serial.print("Heat index: ");
  Serial.print(getTemp("hic"));
  Serial.print(" *C ");
  Serial.print(getTemp("hif"));
  Serial.println(" *F");
  Serial.print(getTemp("k"));
  Serial.println(" *K");
  Serial.println("-----------------");    
  Serial.print("Humidity: ");
  Serial.print(getTemp("h"));
  Serial.println(" % ");
  Serial.println("===========================");
}


/*
 * getTemp(String req)
 * returns the temperature related parameters
 * req is string request
 * This code can display temperature in:
 * getTemp("c") is used to get Celsius.
 * getTemp("f") is used to get Fahrenheit.
 * getTemp("k") is used for Kelvin.
 * getTemp("hif") is used to get Fahrenheit.
 * getTemp("hic") is used to get Celsius.
 * getTemp("h") is used to get humidity.
 */
float getTemp(String req)
{

  // 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 h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

  // Compute heat index in Fahrenheit (the default)
  float hif = dht.computeHeatIndex(f, h);
  // Compute heat index in Celsius (isFahreheit = false)
  float hic = dht.computeHeatIndex(t, h, false);

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
    return 0.0;
  }
  // Compute heat index in Kelvin 
  float k = t + 273.15;
  if(req =="c"){
    return t;//return Celsius
  }else if(req =="f"){
    return f;// return Fahrenheit
  }else if(req =="h"){
    return h;// return humidity
  }else if(req =="hif"){
    return hif;// return heat index in Fahrenheit
  }else if(req =="hic"){
    return hic;// return heat index in Celsius
  }else if(req =="k"){
    return k;// return temperature in Kelvin
  }else{
    return 0.000;// if no request found, return 0.000
  }
 
}

Fichiers📁

Maktabat ya Arduino (zip)

Fritzing File

Manuel de l'utilisateur