Arduino en español
Circuitos con Arduino - Juan Antonio Villalpando
-- Tutorial de iniciación a Arduino --
Volver al índice del tutorial
____________________________
44.- Sensor de humedad y temperatura DHT 11.
- Mide la temperatura y la humedad relativa en %.
Hoja de datos.
Rango de medición de humedad: 20%-95%.
Error de medición de humedad: ±5%
Librería que vamos a utilizar: DHT11.zip
- Como se ve en la figura, conectamos el terminal DATA con el DIGITAL 2 del Arduino. La señal llega estableciendo un bus digital de una línea.
DHT11 uses a simplified single-bus communication. Single bus that only one data line, the system of data exchange, control by a single bus to complete.
Device (master or slave) through an open-drain or tri-state port connected to the data line to allow the device does not send data to release the bus, while other devices use the bus; single bus usually require an external one about 5.1kO pull-up resistor, so that when the bus is idle, its status is high.
Because they are the master-slave structure, and only when the host calls the slave, the slave can answer, the host access devices must strictly follow the single-bus sequence, if the chaotic sequence, the device will not respond to the host.
____________________________________
1.- Medición de temperatura y humedad.
- Librería que vamos a utilizar: DHT11.zip
- El código es sencillo gracias a la librería.
Código |
#include "DHT.h"
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
dht.begin();
}
void loop(){
float humedad = dht.readHumidity();
float temperatura = dht.readTemperature();
Serial.print("Humedad: ");
Serial.println(humedad);
Serial.print("Temperatura :");
Serial.println(temperatura);
delay(200);
} |
|
|
- Si además quieres ver el índice de calor, agrega esta línea:
float idc = dht.computeHeatIndex(temperatura, humedad, false);
____________________________________
2.- Medición de temperatura y humedad. Pantalla LCD.
- Utilizamos una pantalla LCD con bus I2C como hemos visto en otro capítulo de este tutorial.
- Estas pantallas LCD tienen por detrás un módulo adaptador del bus I2C, con este módulo nos podemos comunicar con la pantalla utilizando solo 4 cables.
- El Arduino UNO tiene el bus I2C (SDA y SCL) en las ANALOG IN A4 y A5.
-
La versión R3 del Arduino UNO también tiene bus I2C en los terminales indicado en el dibujo, se puede utilizar indistintamente cualquiera de los dos.
Código |
// Juan Antonio Villalpando
// kio4.com
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
#include "DHT.h"
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
lcd.begin(16,2);// Columnas y filas de LCD
Serial.begin(9600);
dht.begin();
}
void loop(){
float humedad = dht.readHumidity();
float temperatura = dht.readTemperature();
lcd.clear(); // Borra pantalla
lcd.setCursor(0,0); // Inicio del cursor
lcd.print("Temper. = ");
lcd.print(temperatura);
lcd.setCursor(0,1); // Siguiente renglón.
lcd.print("Humedad = ");
lcd.print(humedad);
delay(200);
} |
____________________________________
3.- Código de ejemplo de la librería.
Código |
// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain
#include "DHT.h"
#define DHTPIN 2 // what pin we're connected to
// Uncomment whatever type you're using!
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 21 (AM2302)
//#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 procs.
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
Serial.println("DHTxx test!");
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 (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// 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;
}
// 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);
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" *C ");
Serial.print(f);
Serial.print(" *F\t");
Serial.print("Heat index: ");
Serial.print(hic);
Serial.print(" *C ");
Serial.print(hif);
Serial.println(" *F");
} |
________________________________
|