En esta entrada vamos a hacer dos cosas, leer datos de un sensor DHT11 y luego sacarlos por un display LCD.
Para ello necesitamos aparte de Arduino, cables y alimentación:
- Sensor DHT11, mide presión y temperatura
http://www.banggood.com/DHT11-Temperature-Relative-Humidity-Sensor-Module-For-Arduino-p-933153.html
- Display LCD con un conversor serie para facilitar el cableado, tengo uno parecido a este
https://www.sparkfun.com/products/10097
Para manejar el display contamos con esta información:
https://www.sparkfun.com/tutorials/246
Básicamente tenemos que conectar alimentación, masa y un pin de transmisión de información que va al digital numero 2.
Vamos a usar el codigo de ejemplo de hola mundo y ver si funciona (ojo hay que descargarse la librería en el ordenador e importarla desde la opcion Librerías de arduino).
Para escribir hay que mandar un write con el código 254 seguido de un write con la posición en la que queremos escribir
pos |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
line 1 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
line 2 |
192 |
193 |
194 |
195 |
196 |
197 |
198 |
199 |
200 |
201 |
202 |
203 |
204 |
205 |
206 |
207 |
#include <SoftwareSerial.h>
// Attach the serial display’s RX line to digital pin 2
SoftwareSerial mySerial(3,2); // pin 2 = TX, pin 3 = RX (unused))
void setup()
{
mySerial.begin(9600); // set up serial port for 9600 baud
delay(500); // wait for display to boot up
}
void loop()
{
mySerial.write(254); // move cursor to beginning of first line
mySerial.write(128);
mySerial.write(» «); // clear display
mySerial.write(254); // move cursor to beginning of second line
mySerial.write(192);
mySerial.write(» «); //clear second line
mySerial.write(254); // move cursor to beginning of first line
mySerial.write(128);
mySerial.write(«Hello, world!»);
mySerial.write(254); // move cursor to beginning of first line
mySerial.write(192);
mySerial.write(«Lo tengo!»);
while(1); // wait forever
}
Con esto tenemos funcionando el LCD, vamos con el sensor.
Haz clic para acceder a dht11.pdf
El sensor tiene 3 pines, alimentación, masa y un pin de datos.
Cuidado porque la placa a la que esta soldado el sensor puede confundirnos y no ser intuitiva la salida (yo casi lo quemo).
Aquí hay un par de tutoriales interesantes, de nuevo tenemos que copiar el archivo de Librería e importarlo para poder utilizarlos:
https://learn.adafruit.com/dht
http://arduino-info.wikispaces.com/DHT11-Humidity-TempSensor
El ejemplo es este y hemos usado el pin 3:
// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain
#include «DHT.h»
#define DHTPIN 3 // what digital pin we’re connected to
#define DHTTYPE DHT11 // DHT 11
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»);
}
Después de probar su funcionamiento vamos a integrar todo y sacar temperatura y humedad por el LCD:
// Input from DHT11 output to LCD
#include «DHT.h»
#include «SoftwareSerial.h»
#define DHTPIN 3 // what digital pin we’re connected to
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
// Attach the serial display’s RX line to digital pin 2
SoftwareSerial mySerial(3,2); // pin 2 = TX, pin 3 = RX (unused))
void setup() {
Serial.begin(9600);
Serial.println(«DHTxx test!»);
dht.begin();
mySerial.begin(9600); // set up serial port for 9600 baud
delay(500); // wait for display to boot up
}
void loop() {
// Wait a few seconds between measurements.
delay(2000);
//DTH11 sensor data input
float h = dht.readHumidity();
float t = dht.readTemperature();
//LCD data output
mySerial.write(254); // move cursor to beginning of first line
mySerial.write(128);
mySerial.write(» «); // clear display
mySerial.write(254); // move cursor to beginning of second line
mySerial.write(192);
mySerial.write(» «); //clear second line
mySerial.write(254); // move cursor to beginning of first line
mySerial.write(128);
//Conversion to string for humidity
mySerial.print («H: «);
mySerial.print (h);
mySerial.print (» %»);
mySerial.write(254); // move cursor to beginning of first line
mySerial.write(192);
//Conversion to string for temperature
mySerial.print («T: «);
mySerial.print (t);
mySerial.print (» C»);
}
Con esto esta abierta la puerta a leer desde cualquier sensor y sacar por un display.