Feuchtigkeits- und Temperatursensor
Dieses Programm misst die Temperatur und die Luftfeuchtigkeit.
Hierfür muss ein Library (dht11.h) importiert werden.
Bauteile:
1. Arduinoboard
2. 3 Kabel
3. DHT11 Sensor
Die Schaltung:
Der dht11 sensor hat 3 Anschlüsse,der - (minus) Anschluss muss an GND angeschlossen werden, S an einen digitalen pin (hier pin 5) und der unbeschriftete Anschluss an 5V.
Tipp:
Für den folgenden Sketch muss ein Library eingebunden werden.
Legen sie sich im Verzeichnis des Editor im Ordner libraries einen Ordner mit dem Namen des Libraries an. Erstellen sie dies drei Dateien dht11.h, dht11.cpp und keywords.txt. Übernehmen sie nun die Inhalte dieser Seite.
Sketch:
dht11.h:
//
// dht11.h
//
//
// modified by Leonard Sandvoss on 26.11.15.
//
//
//
// FILE: dht11.h
// VERSION: 0.4.1
// PURPOSE: DHT11 Temperature & Humidity Sensor library for Arduino
// LICENSE: GPL v3 (http://www.gnu.org/licenses/gpl.html)
//
// DATASHEET: http://www.micro4you.com/files/sensor/DHT11.pdf
//
// URL: http://playground.arduino.cc/Main/DHT11Lib
//
// HISTORY:
// George Hadjikyriacou - Original version
// see dht.cpp file
//
#ifndef dht11_h
#define dht11_h
#if defined(ARDUINO) && (ARDUINO >= 100)
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#define DHT11LIB_VERSION "0.4.1"
#define DHTLIB_OK 0
#define DHTLIB_ERROR_CHECKSUM -1
#define DHTLIB_ERROR_TIMEOUT -2
class dht11 {
int pin;
int humidity;
int temperature;
public:
void attach(int _pin);
int read();
int getHumidity();
int getTemperature();
};
#endif
//
// END OF FILE
//
dht11.cpp:
//
// dht11.cpp
//
//
// modified by Leonard Sandvoss on 26.11.15.
//
//
//
// FILE: dht11.cpp
// VERSION: 0.4.1
// PURPOSE: DHT11 Temperature & Humidity Sensor library for Arduino
// LICENSE: GPL v3 (http://www.gnu.org/licenses/gpl.html)
//
// DATASHEET: http://www.micro4you.com/files/sensor/DHT11.pdf
//
// URL: http://playground.arduino.cc/Main/DHT11Lib
//
// HISTORY:
// George Hadjikyriacou - Original version
// see dht.cpp file
//
#include "dht11.h"
void dht11::attach(int _pin) {
this->pin = _pin;
this->temperature = 0;
this->humidity = 0;
}
int dht11::read() {
// BUFFER TO RECEIVE
uint8_t bits[5];
uint8_t cnt = 7;
uint8_t idx = 0;
// EMPTY BUFFER
for (int i=0; i< 5; i++) bits[i] = 0;
// REQUEST SAMPLE
pinMode(this->pin, OUTPUT);
digitalWrite(this->pin, LOW);
delay(18);
digitalWrite(this->pin, HIGH);
delayMicroseconds(40);
pinMode(this->pin, INPUT);
// ACKNOWLEDGE or TIMEOUT
unsigned int loopCnt = 10000;
while(digitalRead(this->pin) == LOW)
if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT;
loopCnt = 10000;
while(digitalRead(this->pin) == HIGH)
if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT;
// READ OUTPUT - 40 BITS => 5 BYTES or TIMEOUT
for (int i=0; i<40; i++)
{
loopCnt = 10000;
while(digitalRead(this->pin) == LOW)
if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT;
unsigned long t = micros();
loopCnt = 10000;
while(digitalRead(this->pin) == HIGH)
if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT;
if ((micros() - t) > 40) bits[idx] |= (1 << cnt);
if (cnt == 0) // next byte?
{
cnt = 7; // restart at MSB
idx++; // next byte!
}
else cnt--;
}
// WRITE TO RIGHT VARS
// as bits[1] and bits[3] are allways zero they are omitted in formulas.
this->humidity = bits[0];
this->temperature = bits[2];
uint8_t sum = bits[0] + bits[2];
if (bits[4] != sum) return DHTLIB_ERROR_CHECKSUM;
return DHTLIB_OK;
}
int dht11::getTemperature() {
return this->temperature;
}
int dht11::getHumidity() {
return this->humidity;
}
keywords.txt: