In this post I will write how to use libraries repository. As example we may use temperature and humidity measurement. Sensor that is common in this area calls DHT11 module. It is cheaper version of DHT22 which allows to get temperature below zero degree and humidity in 0-100% range.
The most important parameters of DHT11 module is temperature measurment above zero degree and humidity in 20-80% range. It is cheaper than DHT22 and perfect for indoor use.
Detailed specification from Adafruit website:
DHT11
- Ultra low cost
- 3 to 5V power and I/O
- 2.5mA max current use during conversion (while requesting data)
- Good for 20-80% humidity readings with 5% accuracy
- Good for 0-50°C temperature readings ±2°C accuracy
- No more than 1 Hz sampling rate (once every second)
- Body size 15.5mm x 12mm x 5.5mm
- 4 pins with 0.1″ spacing
DHT22
- Low cost
- 3 to 5V power and I/O
- 2.5mA max current use during conversion (while requesting data)
- Good for 0-100% humidity readings with 2-5% accuracy
- Good for -40 to 125°C temperature readings ±0.5°C accuracy
- No more than 0.5 Hz sampling rate (once every 2 seconds)
- Body size 15.1mm x 25mm x 7.7mm
- 4 pins with 0.1″ spacing
DHT11 wiring
We may get DHT11 module board ready to use, with soldered resistors already. If you have only sensor then you have to add resistors yourself as below:
Additional plugins
After connecting, we have to find libraries which to get support for this sensor and install the most recent version. After that we should get new element in Arduino IDE menu.
Wybieramy Sketch>>Add library>>manage libraries
Install SimpleDH:
After installing the most recent version, click menu File>>Examples>> SimpleDHT>>DHT11Default
Review code and change pinout where sensor DHT11 is connected to. Youa re ready to upload sketch to Arduino.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
#include <SimpleDHT.h> // for DHT11, // VCC: 5V or 3V // GND: GND // DATA: 2 int pinDHT11 = 2; SimpleDHT11 dht11; //pin where we connect DHT11 to void setup() { Serial.begin(115200); } void loop() { // start working... Serial.println("================================="); Serial.println("Sample DHT11..."); // read without samples. byte temperature = 0; byte humidity = 0; if (dht11.read(pinDHT11, &temperature, &humidity, NULL)) { Serial.print("Read DHT11 failed."); return; } Serial.print("Sample OK: "); Serial.print((int)temperature); Serial.print(" *C, "); Serial.print((int)humidity); Serial.println(" %"); // DHT11 sampling rate is 1HZ. // DHT11 take data every 1s delay(1000); } |
After start a program select: Tools >> Serial monitor. You should get latest results like here:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
Sample DHT11... Sample OK: 23 *C, 21 % ================================= Sample DHT11... Sample OK: 23 *C, 10 % ================================= Sample DHT11... Sample OK: 24 *C, 21 % ================================= Sample DHT11... Sample OK: 24 *C, 21 % ================================= Sample DHT11... Sample OK: 24 *C, 21 % ================================= Sample DHT11... Sample OK: 24 *C, 21 % ================================= Sample DHT11... Sample OK: 24 *C, 10 % ================================= Sample DHT11... Sample OK: 24 *C, 21 % |