---
# System prepended metadata

title: Tutorial on how to build a temperature and humidity sensor

---

# Tutorial on how to build a temperature and humidity sensor
My project is about measuring the temperature and humidity in our waterpump facility in our local area.

Temperature and humidity sensor project
Håkan Carlsson (hc222ci)
Small project with a LoPy4 connected to a DHT11 sensor and a program running collecting sensor data and sending it to Pybytes.
It took about two hours to set it up.

Objective
I choose this project because we think we have problem with humidity in our water pump facility that leads to our circuit-breakers turning off from time to time. We want to correlate these events with data from sensors to see if we are right and can invest in equipment for getting rid of moisture.

Material
You need a LoPy4 device with expansion board and connections with the DHT11 sensor. I bought the parts from Electrokit i Sweden as part of the IoT course arranged by LNU in summer 2021.

Pictures of what I bought and what I use:

![](https://i.imgur.com/TECDucu.jpg)

List of material
LoPy4 and sensors bundle from electrokit is about 949 Skr
It came with LoPy4, expansion board, breadboard and jumper wires.
Digital temperature and humidity sensor DHT11 is about 49 Skr.

Computer setup
I'm using WS Code to program device with pymakr plugin. I'm using the dht.py library in the lib folder. I conncet my computer to LoPy4 through a USB cable.

Circuit diagram 

![](https://i.imgur.com/eqbErJR.jpg)


The code in main.py:

import time
from machine import Pin
from dht import DHT 

th = DHT(Pin('P23', mode=Pin.OPEN_DRAIN), 0)
time.sleep(2)

while True:
    result = th.read()
    while not result.is_valid():
        time.sleep(.5)
        result = th.read()
    print('Temp:', result.temperature)
    print('RH:', result.humidity)
    pybytes.send_signal(1,result.temperature)
    pybytes.send_signal(2,result.humidity)
    time.sleep(10)


The code import some libraries for specific functions and for the DHT 11 sensor. It sets pin 23 on Lopy4 extension board to input and then the while loop reads data and output to console and sends the data to Pybytes cloud service through WiFi connect at home.
The data is read and sent every 10 seconds.
I used WiFi because no other protocols where available where I live.

The code was uploaded through WS Code and pymakr plugin.

Final result of project in Pybytes


![](https://i.imgur.com/8Qbtolq.png)

/Håkan