A Pico Temperature Sensor Project

MadLab Manchester Digital Laboratory on Flickr

Time to warm up the Raspberry pi for another delicious project!

The Pico from Raspberry pi is an affordable microcontroller board for electronics projects that require a “brain” we’ve looked at it a little before in this article https://kidscodecs.com/pico-board-get-started/ where we got the board connected and up and running using some software called “Thonny” to program the device.

If you haven’t worked through that tutorial it’s worth quickly blasting through it to get up to speed and to make sure that Thonny is communicating with your Pico board correctly. In this project we are going to use another affordable little device called a “DHT11 sensor”. The DHT11 sensor is a very common and again affordable sensor that when connected to a microcontroller can be used to sense temperature and humidity.

The DHT11 sensor has 3 pins and we are going to use an electronics breadboard and some jumper wires to connect the pins to our Pico microcontroller. The three pins on the DHT11 are a positive and negative power pin connector and the center pin is the data output from the sensor. Connect the positive pin to pin 36 on the Pico and connect the negative pin on the sensor to pin 38. Finally connect the data out pin on the DHT11 sensor to pin 28 on the Pico.

Connect your Pico USB cable and connect it to your computer and open Thonny. To set up the code for our sensor we are going to use 2 code files saved to the pico. The first one is going to a file which contains quite complex code that enables the DHT11 to work. The second file will be a simpler file that has a few instructions to read some sensor code and print it out. The first file with the complex code can be referred to as either a “module” or sometimes a “library”. This library file contains lots of functions which can be called into action by our shorter program file. So for example we can use a single line of code to call a function to read the temperature, but that line of code, behind the scenes, uses many many more lines of code which it reads in the library/module file. There is a micropython library for the DHT11 sensor which we found online and is available to download here.

Download that file and open it in Thonny and save it with the same name, dht.py, to the pico.
Open a new file and copy the code in carefully.

from machine import Pin, I2C
import utime as time
from dht import DHT11, InvalidChecksum
while True:
time.sleep(5)
pin = Pin(28, Pin.OUT, Pin.PULL_DOWN)
sensor = DHT11(pin)
print("Temperature: {}C".format(round(sensor.temperature)))
print("Humidity: {}%".format(round(sensor.humidity)))

If you have trouble copying that code in correctly we have also uploaded this code for you to load into Thonny and save to your Pico. The file is called dht_datalogging.py and can be found here.

In the first line of this code you can see it begins with “from” and then states a module name “machine” this is a built in module that is included in micropython and therefore we don’t need to save it, like our dht .py module, to the pico as it is already there. The next work is “import” followed by a list of two functions Pin and I2C that the code is asking to be imported for use in this program/script we are writing. On the 3rd line you can see we have asked to import various functions from the DHT11 module we downloaded and added to the pico.

We next move to the main bit of our program. Let’s look at what the code is doing in simple terms. The line “while True:” essentially sets up a loop, so whilst the pico is powered up and this program is loaded the instructions inside the loop (indicated as inside the loop by being indented lines) will keep looping and running in sequence. The next line “time.sleep(5)” essentially tells the program to pause for 5 seconds at this point. This means that our sensor reading will be returned to us every 5 seconds. If you want them faster or slower, change this number.

The next line “pin = Pin(28, Pin.OUT, Pin.PULL_DOWN)” basically defines a code object called “pin” and sets up that it is attached to pin number 28 and that pin is defined as an output pin and when not in use that pin will be pulled down to a logical zero. Meaning in an over simplified way that when not being used we will turn off that pin.

The next line defines the sensor as a DHT11 and shows where the sensor is attached too by pointing it at the “pin” object we defined in the last line. The last two lines look complex but they are pretty straightforward. The first part that reads “print(“Temperature: {}C”” is simple enough, its saying we want to print something back to the repl in Thonny and the first parts that are green in color and inside quote marks are simple text that will surround our data readings from the sensor. the temperature is in. Everything after the period on that line is describing how and where to draw the sensor data from. It begins with “format” which instructs that everything following is the format for the reading, the next instruction is “round” that is because our library/module we added returns values ​​to 5 decimal places and we don’t need that level of accuracy (and also we doubt the DHT11 is that accurate anyway)! to the sensor object we made some lines earli er and then .temperature is a function called from our library that will return the sensor temperature value, to be rounded and then to be printed into our print statement. The final line is exactly the same as the line above except targeting the relative humidity data from the sensor shown as a percentage.

Save this code to your pico giving it a name “dht_datalogging.py” and click the green play button. You should get a readout of the temperature and relative humidity in the shell window of Thonny every 5 seconds!

Learn More

Pico Environment Sensor

https://www.waveshare.com/wiki/Pico-Environment-Sensor

DHT11 Sensor

https://www.adafruit.com/product/386

DHT sensor

https://learn.adafruit.com/dht

Set Up Humidity Sensor on Arduino

How to Set Up the DHT11 Humidity Sensor on an Arduino

Temperature Humidity Sensor with Raspberry Pi

Interfacing DHT11 Temperature Humidity Sensor with Raspberry Pi Pico

Measuring Humidity Using DHT11

https://www.instructables.com/Measuring-Humidity-Using-Sensor-DHT11/

Leave a Comment