Free shipping from € 74,95 NL (€ 99,95 BE / DE)

Ordered before 16:00 tomorrow at home

Express ordered before 11:30 am tonight at home*

14 days free return *

Logo Electronics For You


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 import 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.

  8 while True:
 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
    except 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.

Ask about the possibilities
We will contact you asap

SEND
close-link
Shipping options

package
Orders placed before 16:00 on weekdays are shipped the same day. (Delivery Tuesday to Saturday)Free from €74,95

Letterbox – With track&trace
Ordered before 16:00 on weekdays will be shipped today. (Delivery Tuesday to Saturday)Free from €74,95

Letterbox – Without track&trace
Delivery time 1 - 3 working days
Express delivery
Ordered before 11:30 on weekdays = delivered tonight – Ordered before 23:59 = delivered tomorrow. (Delivery Monday to Friday)
Free from €124,95

Pick up in Leusden
Pick up your order from us: Fokkerstraat 12 - Leusden
close-link
The rating of www.elektronicavoorjou.nl at webwinkelkeurmerk.com is 9.2/10 based on 1135 reviews.