In this example we use the DHT22 (or AM2302) humidity/temperature sensor and the Arduino UNO board to read data and print it out to the serial monitor.
The DHT22 is better than the DHT11 because it has a wider range of measurement, 0 to 100% for humidity and -40°C to +125°C for temperature. Also it has a digital output that provides greater data accuracy.
The AM2302 is a wired version of the DHT22, in a large plastic body, so they are the same device. Connect the red 3.3V power, the yellow wire to your data input pin and the black wire to ground.
Schematic
Here are the pin connections
Device Pin – AM2302 colour | Espruino |
---|---|
1 (Vcc) – Red wire | 3.3 |
2 (S) – Yellow wire | B3 |
3 (GND) – Black wire | GND |
Here is a layout showing how to connect aDHT22
Code
This example simply logs out temperature and humidity to the console every second
[codesyntax lang=”javascript”]
require("DHT22"); setInterval(function() { var dht = require("DHT22").connect(B3); dht.read(function (a){ console.log("Temperature is "+a.temp.toString()); console.log("Humidity is "+a.rh.toString()); }); }, 1000);
[/codesyntax]
Testing
You should see something like this in the console window
Temperature is 25.2
Humidity is 65.1
Temperature is 25.3
Humidity is 65.9
Temperature is 25.5
Humidity is 67.9
Temperature is 25.6
Humidity is 68.6
Temperature is 25.7
Humidity is 69.2
Temperature is 25.9
Humidity is 70.4
Temperature is 26
Humidity is 72.1
Temperature is 26.2
Humidity is 72.7
Link