Overview
In this tutorial, we will send the DHT11 Humidity Temperature Sensor Data to Thingspeak Server using Raspberry Pi Pico W Board. The latest Raspberry Pi Pico W brings WiFi to the Pico platform while retaining complete pin compatibility with its older sibling Raspberry Pi Pico.
Earlier we learned Getting Started with Raspberry Pi Pico W in detailed & discussed various method to connect the Pi Pico W to any Wi-Fi Access point. For testing the Wi-Fi capability and working with sensors, Thingspeak is the best platform. ThingSpeak is open-source software that allows users to communicate with internet-enabled devices. It facilitates data access, retrieval, and logging of data by providing an API to both the devices and social network websites.
The DHT11/22 Humidity Temperature sensor is the best sensor for beginners for getting started with any new microcontroller or new programming language. This guide will explain the method to interface the DHT11 Sensor with Raspberry Pi Pico W with MicroPython Code & post the Sensor Data on Thingspeak Server.
Components Required
You need following components for this project. You can buy the Raspberry Pi Pico W Kit from SunFounder which consist the Lipo Charger Module along with other 200+ components.
- Raspberry Pi Pico W – 1
- DHT11 Sensor – 1
- Lipo Battery Charger – 1
- Samsung 18650 Battery with Holder – 1
- Jumper Wires – 3
- Breadboard – 1
- Micro-USB Cable.
DHT11 Temperature & Humidity Sensor
DHT11 Temperature & Humidity sensor is a temperature and humidity composite sensor with calibrated digital signal output. It includes a resistive humidity sensing element and an NTC temperature measuring element and is connected to a high-performance 8-bit microcontroller.
Its application-specific digital module acquisition technology and temperature and humidity sensing technology ensure that the product has extremely high reliability and excellent long-term stability.
There is a dedicated post about DHT11 Raspberry Pi Pico interfacing in one of our previous article. You can go through it for more info.
Interfacing DHT11 Sensor with Raspberry Pi Pico W
Now lets interface the DHT11 Humidity and Temperature Sensor with Raspberry Pi Pico W. The connection is fairly simple.
Connect the VCC, GND & Output pin of DHT11 Sensor to Raspberry Pi Pico W 3.3V, GND & GP0 Pin respectively.
The Raspberry Pi Pico W has a buck-boost converter IC. Hence you can power the Raspberry Pi Pico W via a Lipo Battery. The Lipo Battery Charger Module which consists of LTC4054 IC has been modified and specially designed for powering Raspberry Pi Pico W using Lipo Battery.
Using the Lipo Charger Module and 3.7V Samsung 18650 Rechargeable Lithium-Ion Battery, you can power the Raspberry Pi Pico W & DHT11 Sensor & make the project portable.
Setting up Thingspeak
ThingSpeak provides very good tool for IoT based projects. By using the ThingSpeak site, we can monitor our data and control our system over the Internet, using the Channels and web pages provided by ThingSpeak. So first you need to sign up for ThingSpeak. So visit https://thingspeak.com and create an account.
Then create a new channel and create two variables as shown in the image below.
Then create the API keys. This key is required for programming modifications and receiving data from Raspberry Pi Pico W.
Now click on channels so that you can see the online data streaming.
MicroPython Code
Now let us write a MicroPython code for Raspberry Pi Pico W to send the DHT11 Humidity Temperature Sensor Data to the Thingspeak Server.
To upload the data to Thingspeak Server, the Raspberry Pi Pico W needs to connect to the Wi-Fi network. Then using the API Key the Raspberry Pi Pico W uploads the humidity and temperature data to Thingspeak. You can observe the graphical data in visualized form in Thingspeak dashboard.
The MicroPython Code is has two parts as the DHT11 Sensor requires a library for retrieving the humidity temperature data.
dht.py
Copy the following code as paste it on your Thonny editor. Save the file in Raspberry Pi Pico as dht.py.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
import array import micropython import utime from machine import Pin from micropython import const class InvalidChecksum(Exception): pass class InvalidPulseCount(Exception): pass MAX_UNCHANGED = const(100) MIN_INTERVAL_US = const(200000) HIGH_LEVEL = const(50) EXPECTED_PULSES = const(84) class DHT11: _temperature: float _humidity: float def __init__(self, pin): self._pin = pin self._last_measure = utime.ticks_us() self._temperature = -1 self._humidity = -1 def measure(self): current_ticks = utime.ticks_us() if utime.ticks_diff(current_ticks, self._last_measure) < MIN_INTERVAL_US and ( self._temperature > -1 or self._humidity > -1 ): # Less than a second since last read, which is too soon according # to the datasheet return self._send_init_signal() pulses = self._capture_pulses() buffer = self._convert_pulses_to_buffer(pulses) self._verify_checksum(buffer) self._humidity = buffer[0] + buffer[1] / 10 self._temperature = buffer[2] + buffer[3] / 10 self._last_measure = utime.ticks_us() @property def humidity(self): self.measure() return self._humidity @property def temperature(self): self.measure() return self._temperature def _send_init_signal(self): self._pin.init(Pin.OUT, Pin.PULL_DOWN) self._pin.value(1) utime.sleep_ms(50) self._pin.value(0) utime.sleep_ms(18) @micropython.native def _capture_pulses(self): pin = self._pin pin.init(Pin.IN, Pin.PULL_UP) val = 1 idx = 0 transitions = bytearray(EXPECTED_PULSES) unchanged = 0 timestamp = utime.ticks_us() while unchanged < MAX_UNCHANGED: if val != pin.value(): if idx >= EXPECTED_PULSES: raise InvalidPulseCount( "Got more than {} pulses".format(EXPECTED_PULSES) ) now = utime.ticks_us() transitions[idx] = now - timestamp timestamp = now idx += 1 val = 1 - val unchanged = 0 else: unchanged += 1 pin.init(Pin.OUT, Pin.PULL_DOWN) if idx != EXPECTED_PULSES: raise InvalidPulseCount( "Expected {} but got {} pulses".format(EXPECTED_PULSES, idx) ) return transitions[4:] def _convert_pulses_to_buffer(self, pulses): """Convert a list of 80 pulses into a 5 byte buffer The resulting 5 bytes in the buffer will be: 0: Integral relative humidity data 1: Decimal relative humidity data 2: Integral temperature data 3: Decimal temperature data 4: Checksum """ # Convert the pulses to 40 bits binary = 0 for idx in range(0, len(pulses), 2): binary = binary << 1 | int(pulses[idx] > HIGH_LEVEL) # Split into 5 bytes buffer = array.array("B") for shift in range(4, -1, -1): buffer.append(binary >> shift * 8 & 0xFF) return buffer def _verify_checksum(self, buffer): # Calculate checksum checksum = 0 for buf in buffer[0:4]: checksum += buf if checksum & 0xFF != buffer[4]: raise InvalidChecksum() |
main.py
Here is the main code for Raspberry Pi Pico which establishes WiFi Connections and post the DHT11 Sensor Data to Thinspeak Server.
From the following line, change the WiFi SSID, password and also the Thingspeak API key.
|
1 2 3 |
THINGSPEAK_WRITE_API_KEY = 'Y4VSS02J3VFSY0UK' ssid = 'BELL685' password = '644642755D2F' |
Here is the final code for the project. You can save this file with any name like main.py.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
import machine import urequests from machine import Pin import network, time from dht import DHT11, InvalidChecksum HTTP_HEADERS = {'Content-Type': 'application/json'} THINGSPEAK_WRITE_API_KEY = 'Y4VSS02J3VFSY0UK' ssid = 'BELL685' password = '644642755D2F' # Configure Pico W as Station sta_if=network.WLAN(network.STA_IF) sta_if.active(True) if not sta_if.isconnected(): print('connecting to network...') sta_if.connect(ssid, password) while not sta_if.isconnected(): pass print('network config:', sta_if.ifconfig()) while True: time.sleep(5) pin = Pin(0, Pin.OUT, Pin.PULL_DOWN) sensor = DHT11(pin) t = (sensor.temperature) h = (sensor.humidity) print("Temperature: {}".format(sensor.temperature)) print("Humidity: {}".format(sensor.humidity)) dht_readings = {'field1':t, 'field2':h} request = urequests.post( 'http://api.thingspeak.com/update?api_key=' + THINGSPEAK_WRITE_API_KEY, json = dht_readings, headers = HTTP_HEADERS ) request.close() print(dht_readings) |
Monitor DHT11 Sensor Data on Thingspeak with Raspberry Pi Pico W
Run the library and also the main code.
The Raspberry Pi Pico W will connect to the WiFi network and establish connection with the Thingspeak Server. The Python Shell will display the event.
To have a visual representation of Temperature Humidity Data, you can go to the private view of Thingspeak Server Dashboard. The dashboard will have an entry of humidity temperature latest data after an interval of every 15 seconds.
This is how you can upload any sensor data to Thingspeak Server using Raspberry Pi Pico W, WiFi chip.

















3 Comments
I thought I could substitute a DHT22 in place of DHT11 into Your instructions. When I run main.py I get a response that says “connecting to network…” and nothing happens. I’m sure my ThingSpeak Key is accurate, I’m using ThingSpeak and it runs fine with input from a similar Arduino project. I’m trying to put new data from my Pico W into fields 7 and 8.
I think I don’t get how to “Run the Library and the main code”. I’m new to MicroPython and Pico W and I don’t know what is meant by Run Library, what library? There are two files in these instructions, dht.py and main.py. How do I run two files?
Any help is greatly appreciated, Thank You!
Unfortunately I can’t delete previous comments which are not very helpful.
The following is a link to my DHT22 solution that is working well:
https://github.com/dlwiener/Pico-W-DHT22-ThingSpeak/blob/main/PicoW%20DHT22%20ThingSpeak
I deleted it dont worry. And thank you for the code.