Delivery throughout Europe

Ordered before 16:00 = Shipped today

Fast delivery with DHL

XNUMX days return *


Country

Raspberry Pi project: Weather station

At this Raspberry Pi project we are going to make a weather station. We use the DHT11 for this. This is a Temperature and humidity sensor. The Temperature is indicated in degrees Celsius (°C) and the humidity in percentages (%).

This manual covers: 

  • Level - Medium 55% 55%
  • Duration 30 min 35% 35%
  • Cost - € 70,30 50% 50%

Step 2: Install Libraries

To get started, we need to install some libraries first. We need these libraries to read the DHT11 sensor. We install the libraries using the terminal.

First of all, we install the library “Adafruit_CircuitPython_DHT”:

sudo pip3 install Adafruit_CircuitPython_DHT

Then we install the library “libgpiod2”:

sudo apt install libgpiod2

Step 3: Connecting

Once the necessary libraries are installed, we can proceed to create the connection circuit and python code. We connect the DHT11 directly to the Raspberry Pi. We connect the ground of the DHT11 to the GND of the Raspberry Pi (blue wire). We connect the power supply of the sensor to the 3.3V of the Raspberry Pi (thread). We connect the data signal to the GPIO pin 13 of the Raspberry Pi.

Step 4: Programming

 1 import time
 2 imports board
 3 import adafruit_dht
 4
 5 # Initialize the DHT11 with GPIO pin 13:
 6 dhtDevice = adafruit_dht.DHT11(board.D13)
 7

In the Python code we first import the necessary libraries at the top. Then we write in the code that the signal from the DHT11 on the digital pin 13 of the Raspberry Pi is connected.

  8whileTrue:
 9 try:
10 # Print the values ​​(temperature and humidity) to the serial port
11 temperature_c = dhtDevice.temperature
12 humidity = dhtDevice.humidity
13 print (
14 “Temperature: {: .1f} * C Humidity: {}%“ .format (
Temperature_c, humidity
16)
17)
18
19 except RuntimeError as error:
20 # Errors are quite common, DHTs are hard to read, just keep going
21 print (error.args [0])
22 time.sleep (2.0)
23 continuous
24 except Exception as error:
25 dhtDevice.exit ()
26 raise error
27
28 time.sleep (2.0)

Now we make an infinite loop, in which the temperature and humidity are always printed. In the loop we use the try ... except function. This is because DHT sensors are always difficult to read, which sometimes causes errors. With the try block you can test a code block for errors. And with the except block you can handle the error. If no errors occur, the try block reads the temperature and humidity. Subsequently, this data is printed to the serial port, so that you can read these values ​​on your screen.

For those who want to copy the entire code, this is stated below. However, we recommend that you copy it, because yes will learn a lot more from that. 

import time
import board
import adafruit_dht

# Initialize the DHT11 with GPIO pin 13:
dhtDevice = adafruit_dht.DHT11(board.D13)

while True:
    Try:
        # Print the values ​​(temperature and humidity) to the serial port
        temperature_c = dhtDevice.temperature
        humidity = dhtDevice.humidity
        print (
            “Temperature: {: .1f} * C Humidity: {}%“ .format (
                temperature_c, humidity
            )
        )

    except RuntimeError as error:
        # Errors are quite common, DHTs are hard to read, just keep going
        print (error.args [0])
        time.sleep (2.0)
        keep on going
    Exception as error:
        dhtDevice.exit ()
        raise error

    time.sleep (2.0)

Step 5: Result

When you have connected and programmed everything correctly, you can click on “Run” to start the code. When you start the code, you will see that the measured temperature and humidity are printed in the “Shell”.

Now you know how to make a weather station using the Raspberry Pi.

The rating of www.elektronicavoorjou.nl at WebwinkelKeur Reviews is 9.3/10 based on 4989 reviews.