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
This is the pin connections
Device Pin – AM2302 colour | Espruino Pico |
---|---|
1 (Vcc) – Red wire | 3.3 |
2 (S) – Yellow wire | B3 |
3 (GND) – Black wire | GND |
Code
This example simply logs out temperature and humidity to the console every second
[codesyntax lang=”javascript”]
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]
Link