The purpose of this experiment is to interface the DHT11 humidity sensor with a Raspberry Pi Pico microcontroller to measure and display humidity values. The DHT11 sensor provides digital output that can be read using GPIO pins. This experiment demonstrates both MicroPython and Embedded C implementations to read humidity data and display it on the console.
Scope:
This experiment aims to provide an understanding of how to interface the DHT11 sensor with a Raspberry Pi Pico to measure environmental humidity. It covers both software and hardware integration, providing implementations in both MicroPython and Embedded C. The result is displayed on the console for real-time monitoring.
Prerequisite:
3.1. Hardware:
Pico microcontroller
DHT11 sensor module
Breadboard
Jumper wires
USB cable (to connect the computer)
3 .2. Software:
Thonny Software(For micro python)
MicroPython firmware installed on Raspberry Pi Pico
ubuntu Terminal(For embedded C)
CMake,Pico SDK,GNU Arm Embedded Toolchain,WSL.
Connection Details:
DHT11 Sensor Pinout:
VCC → 3.3V on Pico
GND → GND on Pico
Data → GPIO 16 on Pico
UART Serial Console: Use a micro-USB cable to connect the Pico to the PC, then open PuTTY and select the correct COM port and baud rate (115200).

Working Principle:
The DHT11 sensor measures the relative humidity using the humidity sensing components and sends the digital signal to the microcontroller. The sensor provides a digital signal via its data pin, which the Raspberry Pi Pico reads through a GPIO pin. The humidity data is then processed and displayed on the serial console.
MicroPython: The dht library in MicroPython is used to interface with the DHT11 sensor, retrieving the temperature and humidity values.
Embedded C: In the C implementation, the DHT11 data is read using specific GPIO manipulation functions in the Pico SDK to handle the sensor’s one-wire protocol.
DHT11 Humiture Sensor
The digital temperature and humidity sensor DHT11 is a composite sensor that contains a calibrated digital signal output of temperature and humidity. The technology of a dedicated digital modules collection and the temperature and humidity sensing technology are applied to ensure that the product has high reliability and excellent long-term stability.
The sensor includes a resistive sense of wet component and an NTC temperature measurement device, and is connected with a high-performance 8-bit microcontroller.
Only three pins are available for use: VCC, GND, and DATA. The communication process begins with the DATA line sending start signals to DHT11, and DHT11 receives the signals and returns an answer signal. Then the host receives the answer signal and begins to receive 40-bit humiture data (8-bit humidity integer + 8-bit humidity decimal + 8-bit temperature integer + 8-bit temperature decimal + 8-bit checksum).

Features
Humidity measurement range: 20 - 90%RH
Temperature measurement range: 0 - 60℃
Output digital signals indicating temperature and humidity
Working voltage:DC 5V; PCB size: 2.0 x 2.0 cm
Humidity measurement accuracy: ±5%RH
Temperature measurement accuracy: ±2℃
Application code:
MicroPython:
from machine import Pin
import utime as time
from dht import DHT11
# Initialize the pin for the DHT11 sensor as an input
pin = Pin(16, Pin.IN)
# Create a DHT11 sensor object
sensor = DHT11(pin)
while True:
# Measure temperature and humidity
sensor.measure()
# Print the measured temperature and humidity values
print("Temperature: {}°C, Humidity: {}%".format(sensor.temperature(), sensor.humidity()))
# Wait for 1 second before the next measurement
time.sleep(1)
Embedded C:
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
// Define the GPIO pin connected to the DHT11 data pin
#define DHT_PIN 16
// Function to read bits from DHT11 (this function should be implemented based on the DHT11 protocol)
bool read_dht11(uint8_t *humidity, uint8_t *temperature) {
uint8_t data[5] = {0, 0, 0, 0, 0}; //for temp integer,temp decimal,humidity integer,humidity decial,checksum
uint32_t last_tick, tick;
// Send start signal to the DHT11
gpio_set_dir(DHT_PIN, GPIO_OUT);
gpio_put(DHT_PIN, 0); // Pull low for at least 18 ms
sleep_ms(20);
gpio_put(DHT_PIN, 1); // Release and wait for sensor response
sleep_us(40);
// Change pin to input to read response
gpio_set_dir(DHT_PIN, GPIO_IN);
// Wait for DHT11 to pull pin low
while (gpio_get(DHT_PIN) == 1);
// Wait for DHT11 to pull pin high
while (gpio_get(DHT_PIN) == 0);
// Wait for DHT11 to pull pin low again (end of response)
while (gpio_get(DHT_PIN) == 1);
// Read 40 bits of data (5 bytes)
for (int i = 0; i < 40; i++) {
// Wait for pin to go high
while (gpio_get(DHT_PIN) == 0);
// Measure how long the pin stays high
last_tick = time_us_32();
while (gpio_get(DHT_PIN) == 1);
tick = time_us_32() - last_tick;
// If pin was high for more than 50 microseconds, it's a 1
if (tick > 50) {
data[i / 8] |= (1 << (7 - (i % 8)));
}
}
// Verify checksum
if (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) {
*humidity = data[0];
*temperature = data[2];
return true; // Success
}
return false; // Checksum error
}
int main() {
stdio_init_all();
uint8_t humidity = 0, temperature = 0;
gpio_init(DHT_PIN);
gpio_set_dir(DHT_PIN, GPIO_IN);
while (1) {
// Read DHT11 data
if (read_dht11(&humidity, &temperature)) {
printf("Temperature: %d°C, Humidity: %d%%\n", temperature, humidity);
} else {
printf("Failed to read from DHT11 sensor\n");
}
sleep_ms(2000); // Wait 2 seconds before reading again
}
return 0;
}
Hardware Image:

Output:
For both MicroPython and Embedded C code, the output is displayed on the serial console. The output is similar to:

Video Demonstration:
Appendix:
Common CMakeLists:
cmake_minimum_required(VERSION 3.12)
# Pull in SDK (must be before project)
include(pico_sdk_import.cmake)
project(pico_experiments C CXX ASM)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
if (PICO_SDK_VERSION_STRING VERSION_LESS "1.3.0")
message(FATAL_ERROR "Raspberry Pi Pico SDK version 1.3.0 (or later) required. Your version is ${PICO_SDK_VERSION_STRING}")
endif()
# Initialize the SDK
pico_sdk_init()
add_compile_options(-Wall
-Wno-format # int != int32_t as far as the compiler is concerned because gcc has int32_t as long int
-Wno-unused-function # we have some for the docs that arent called
)
if (CMAKE_C_COMPILER_ID STREQUAL "GNU")
add_compile_options(-Wno-maybe-uninitialized)
endif()
# Hardware-specific examples in subdirectories:
add_subdirectory(expt_10_LCD)
CMakeLists:
# Minimum CMake version required
cmake_minimum_required(VERSION 3.13)
# Set project name
project(dht11_reader)
# Set Pico SDK path
include(pico_sdk_import.cmake)
# Initialize the Pico SDK
pico_sdk_init()
# Add the executable target
add_executable(dht11_reader main.c)
# Link with the Pico standard library
target_link_libraries(dht11_reader pico_stdlib)
# Enable USB output and UART output
pico_enable_stdio_usb(dht11_reader 1)
pico_enable_stdio_uart(dht11_reader 0)
# Create the target binary
pico_add_extra_outputs(dht11_reader)
References:
| # | Topic | Date & time | Action |
|---|
0% Completed (0/1)