Banner Ausblenden
Kleines Logo

Willkommen auf PSI-Online

Sie befinden sich in: Unterrichtsangebote Fächer Fachbereich III Informatik Arduino Bauteile und Sensoren Feuchtigkeits- und Temperatursensor

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.IMG_3070.JPG

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:


	
#include  "dht11.h"     // Libary importieren

	
dht11 dht; // dht11 istanz
 
 
void setup() {
  dht.attach(5); // Sensor auf pin 5
  Serial.begin(9600);
}
 
void loop() {
  const int error = dht.read(); // Error abfangen und Sensor auslesen
 
  if (error == DHTLIB_ERROR_CHECKSUM) { // unbekannter Error
    Serial.println("DHTLIB_ERROR_CHECKSUM");
    return;
  }
  if (error == DHTLIB_ERROR_TIMEOUT) { // Timeout
    Serial.println("DHTLIB_ERROR_TIMEOUT");
    return;
  }
 
  Serial.print("temperature: ");
  Serial.print(dht.getTemperature()); // Temperatur auslesen
  Serial.println("° C");
  Serial.print("humidity: ");
  Serial.print(dht.getHumidity()); // Luftfeuchte auslesen
  Serial.println(" %");
  
  delay(1000);
}
 
 

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:

#######################################
# Syntax Coloring Map For dh11
#######################################
 
#######################################
# Datatypes (KEYWORD1)
#######################################
 
dht11 KEYWORD1
 
#######################################
# Methods and Functions (KEYWORD2)
#######################################
 
attach KEYWORD2
read KEYWORD2
getTemperature    KEYWORD2
getHumidity    KEYWORD2
 
 
#######################################
# Instances (KEYWORD2)
#######################################
 
 
#######################################
# Constants (LITERAL1)
#######################################
 
 
DHTLIB_OK    LITERAL1
DHTLIB_ERROR_TIMEOUT LITERAL1
DHTLIB_ERROR_CHECKSUM LITERAL1

 

 

Erstellt: Andreas Hecker (08.12.2015) Letzte Änderung: Leonard Sandvoß (19.01.2016)