Reading the DHT11 with raspberry pi and python

Reading the DHT11 with raspberry pi and python Titelbild

Folgende Teile braucht ihr für die Anleitung

Smart Plant kit for the Raspberry Produktbild

Smart Plant kit for the Raspberry | 19.95€

DHT11 - Measuring humidity and temperature

Let us get started.

The first step is to upgrade your Raspberry Pi and install a library for the sensor. Because not everyone uses the Raspberry Pi to measure temperature, the library we need to measure data with the DHT11 does not come preinstalled. You will need to download the library from internet. For the purposes of this kit, we will assume that your Pi is already connected to the internet. If this is not the case, you can either connect your pi via an ethernet connection or via wifi.

You can find out more here: cw42.co/wifi

Once you are connected, open up a terminal window and type the following command to upgrade your Raspberry Pi:

sudo apt-get update && sudo apt-get upgrade -y

The apt-get commands are used to manage the programs on the Raspberry Pi. With the command above you are updating the program packages on your Raspberry Pi and making sure that you are using the latest version of every single program.

In order to install the necessary program and libraries for Python3, type the following command into a terminal window. We will discuss its purpose and functionality later in this text.

sudo apt-get install build-essential python3-dev

Next enter the following command to download the python library needed to read the temperature and humidity sensor data.

git clone 
      https://github.com/coding-world/Python_DHT.git

Currently, the library is located in the Python_DHT folder. Your Raspberry Pi and Python environment don’t know that this library exists yet. To create this connection we must install it with the following commands:

First, change into the Python_DHT directory.

cd Python_DHT

Next you will enter the following command to install the library to the Python3 environment.

sudo python3 setup.py install

Once the installation is complete, we can use the Python_DHT Library with the import command in python.

Next we will discuss how to connect the DHT11 sensor to the GPIO Pins on the Raspberry Pi. You can recognise the front side of the DHT11 sensor by the grid.

Connecting

Connecting the DHT11 to the Raspberry Pi

Connections Pi Connections DHT11
3.3V 1. Pin DHT11/VCC
GPIO4 2. Pin DHT11/Data
Resistor 4.7KOhm Connect 1. Pin and 2. Pin
GND (Ground) 4. Pin DHT11 GND
  • You need to connect a resistor between the first (VCC) and second Pin (Data). This is necessary for the energy supply and accurate measurements. The best resistor for this is the 4.7Kohm resistor. You can distinguish the resistance values by the colored code on the resistor. The 4.7KOhm Resistor has the following color code: yellow, purple, black, brown and gold. Find out more: cw42.co/resistors *

Please pay close attention to the connections as you could accidentally short circuit and damage the Raspberry Pi and sensors. Bad connections may also cause the sensor to run really hot!

Once everything is connected properly and all libraries are installed, we can get started with measuring the temperature and humidity.

To do this we will create our first program. From a terminal window, we can create a new file with the following command:

sudo nano dht11_simple.py

Fill the new text file with the following code:

import Python_DHT

sensor = Python_DHT.DHT11
pin = 4
humidity, temperature = Python_DHT
       .read_retry(sensor, pin)
print("Temperature = "+str(temperature)+ 
       "C Humidity = "+str( humidity)+"%")

In the first line we are importing the Python_DHT library that we installed earlier. In the third line we create a variable called sensor and make it equal to the type of sensor we want to read the data from. In our case it is the DHT11.

In line four we are creating the variable pin and setting it to the pin number we will be utilizing.

In line 5 we utilize an alternate method for creating and assigning variables. You can create multiple variables by separating them by commas , and they can be assigned values at the same time. In this case we are using the .read_retry() function. This functions takes the sensor type and the pin as a parameter.

In line 6 we are printing all the information obtained by the sensor.

Please note the line break in line 6 and 8, we need them for the book.

Now it is your turn: Can you change the code for the temperature sensor so it’s printing a special statement if the temperature is higher than 25°c?

Hint: You can use your breath for the temperature and logical operators.

Noch Fragen oder Feedback?

Bevor du eine Frage stellen kannst musst du dich zuerst Anmelden oder Regestrieren!