KY-015 Combi-Sensor Temperature+Humidity

From SensorKit X40 Wiki

Revision as of 17:52, 20 February 2017 by Webmaster Wiki (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Picture

none

Technical data / Short description

Chipset: DHT11 | Communication protocol: 1-wire measuring range Humidity 20-90%RH measuring termperature: 0-50°C

Advantages of the sensor are the combination between temperature measurment and humidity measurements in one compact design - the disadvantage is the low sampling rate of the measurement, you only have a new result every 2 seconds - so the sensor is better to use for long period measurements.

Pinout

none

Software example Arduino

This sensor does not send it's measurements analog via pin, it sends the signal digital.

You have various opportunities to control the sensor - the best opportunity is to use the Adafruit_DHT library from the company Adafruit which is published under the following Link with the OpenSource MIT-Lizenz.

The example below is using this library - for this we advise to download it from Github, to copy and to unzip it in the Arduino-library folder which you can find under the path C:\user\[Username]\documents\arduino\libraries by default. You can use it then for this and the following project examples. It is also included in the download package below.

// Adafruit_DHT library will be included
#include "DHT.h"

// You can declare the input pin here
#define DHTPIN 2     

// The sensor will be initialized here
#define DHTTYPE DHT11   // DHT 11
DHT dht(DHTPIN, DHTTYPE);

void setup() 
{
  Serial.begin(9600);
  Serial.println("KY-015 test - temperature and humidity-test:");

  // Mearsurement will be started
  dht.begin();
}

// Main program loop
// The program will be started and the measurements will be read by it.
// It takes a break between every measurement to take new values.
void loop() {

  // Two second break between measurements
  delay(2000);

  // Measurement of humidity
  float h = dht.readHumidity();
  // Measurement of temperature
  float t = dht.readTemperature();
  
  // The measurements will be tested of errors here
  // If an error is detected, an error message will be displayed
  if (isnan(h) || isnan(t)) {
    Serial.println("Error while reading the sensor");
    return;
  }

  // Output at the serial console
  Serial.println("-----------------------------------------------------------");
  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("temperature: ");
  Serial.print(t);
  Serial.print(char(186)); //Output <°> symbol
  Serial.println("C ");
  Serial.println("-----------------------------------------------------------");
  Serial.println(" ");
}

Please notice, the sensor will only get a new measurement value after 2 seconds. Because of that it is better to use for long period measurements.

Example program download:

KY-015-Kombi-Sensor_Temperatur_Feuchtigkeit.zip

Connections Arduino:

GND = [Pin GND]
+V = [Pin 5V]
Signal = [Pin D2]

Software example Raspberry Pi

To control the DHT11-sensor which is on the sensor module, the Adafruit_python_library from the company Adafruit will be used by the program. This library is published under the following Link with the MIT OpenSource-Lizenz.

The library has to be installed before using:

First you have to install the Github-sofware on the Raspberry Pi if it is not already done:

sudo apt-get install git

For this you have to connect the Raspberry Pi with the internet. You can download and unzip the current version of the Adafruit_BM085 library with the command...

git clone https://github.com/adafruit/Adafruit_Python_DHT.git

After that we move to the downloaded folder with...

cd Adafruit_Python_BMP/

... and install the library with ...

sudo python setup.py install

After this the library can be used.

In order that the Raspberry Pi can communicate with the sensor via I2C-bus, you have to activate the I2C function from the Raspberry Pi.

For this, you have to append the following line to the end of the file "/boot/config.txt":

    dtparam=i2c_arm=on

You can edit the file with the command:

sudo nano /boot/config.txt

You can save and close the file with the key sequence [ctrl+x -> y -> enter].

Furthermore you need additional libraries to use I2C with python. To install it use the following command:

sudo apt-get install python-smbus i2c-tools -y

You can use the following python code example now. The program starts the measurement at the sensor and shows the measurements of air pressure, temperature and the highness above sea level.

#!/usr/bin/python
# coding=utf-8

# Needed modules will be imported
import RPi.GPIO as GPIO
import Adafruit_DHT
import time

# The break of 2 seconds will be configured here
sleeptime = 2

# Sensor should be set to Adafruit_DHT.DHT11,
# Adafruit_DHT.DHT22, or Adafruit_DHT.AM2302.
DHTSensor = Adafruit_DHT.DHT11

# The pin which is connected with the sensor will be declared here
GPIO_Pin = 23

print('KY-015 sensortest - temperature and humidity')

try:
    while(1):
        # Measurement will be started and the result will be written into the variables
        humid, temper = Adafruit_DHT.read_retry(DHTSensor, GPIO_Pin)

        print("-----------------------------------------------------------------")
        if humid is not None and temper is not None:

            # The result will be shown at the console
            print('temperature = {0:0.1f}°C  | rel. humidity = {1:0.1f}%'.format(temper, humid))
        
        # Because of the linux OS, the Raspberry Pi has problems with realtime measurements.
        # It is possible that, because of timing problems, the communication fails.
        # In that case, an error message will be displayed - the result should be shown at the next try.
        else:
            print('Error while reading - please wait for the next try!')
        print("-----------------------------------------------------------------")
        print("")
        time.sleep(sleeptime)

# Scavenging work after the end of the program
except KeyboardInterrupt:
    GPIO.cleanup()

Please notice, the sensor will only get a new measurement value after 2 seconds. Because of that it is better to use for long period measurements.

Connections Raspberry Pi:

GND = GND [Pin 06]
+V = 3,3V [Pin 01]
Signal = GPIO23 [Pin 16]

Example download

KY-015-RPi_Kombi-Sensor_Temperatur_Feuchtigkeit.zip

To start with the command:

sudo python KY-015-RPi_Kombi-Sensor_Temperatur_Feuchtigkeit.py