Skip to main content

IoT Lab - Humidity Sensor Example

The sensor used for measuring temperature and humidity of the surrounding environment. The box contains AM2302 DHT22 sensor which are the temperature and humidity which outout calibrated digital signal for the data.

dht22-

When you want to try using each sensor, the common properties of every sensor is

  • VCC (aka. 5V, 3.3V, +, etc.) is the pin for the higher voltage power input
  • GND (aka. Ground, -, etc.) is the pin for lower voltage (ground) power input
  • DAT (aka. DATA, GPIO, etc.) is the pin for trasmitting data, sometimes in Digital, Analog, or both. It may contains one or more data pins.

Then, let's try googling the information of sensor, as we want to know how to connect these sensors to the controller board (Ardiono). The suggested keywords are

  • <sensor model (AM2302 DHT22)> datasheet
  • <sensor model> pinout / pinmap
  • <sensor model> library arduino
  • <sensor model> tutorial
  • <sensor model> wiring
  • <sensor model> data output type
  • <sensor model> arduino code example
  • <sensor model> with esp32 example

And you'll get some code example:

#include "DHT.h"

#define DHTPIN 2     // what pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302)

int maxHum = 60;
int maxTemp = 40;

DHT dht(DHTPIN, DHTTYPE);

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

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

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius
  float t = dht.readTemperature();
  
  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  
  Serial.print("Humidity: "); 
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: "); 
  Serial.print(t);
  Serial.println(" *C ");
}

(Copied from https://www.electroschematics.com/arduino-dht22-am2302-tutorial-library/)

In the example code above, you will see HDTPIN (LINE 3) that you need to specify your own pin number the retrieve data from the sensor which can be varies depend on your pin mapping.