Temperature sensor – Raspberry PICO

The DHT11 is an affordable and popular sensor used to measure temperature and humidity in various electronic projects.

The DHT11 is a digital sensor that communicates with the microcontroller via a single data wire (in “one-wire” mode).

It can measure temperatures ranging from 0 to 50°C with an accuracy of ±2°C and humidity levels between 20% and 80% with an accuracy of ±5% RH (relative humidity).

The sensor sends these values as digital data, making integration with platforms like the Raspberry Pi Pico easier.

Here are the different pins of the DHT11 sensor:

  • Signal: Sends the data to the Raspberry Pi Pico

  • Vcc: The power supply (5V)

  • GND: Ground

To compile programs that make the temperature sensor work, you will need the DHT11 library.

It comes in a .zip file, so to install it, you need to go to Sketch, then Include Library, and select Add .ZIP Library. You can find more explanations in our course on libraries.

Here is the program to make the DHT11 sensor work:

#include <DHT.h>

// Define the DHT11 sensor pin
#define DHTPIN 11      // GPIO11 of the Raspberry Pi Pico

// Define the sensor type (DHT11)
#define DHTTYPE DHT11

// Initialize the DHT object
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  // Initialize serial communication to display results
  Serial.begin(9600);
  
  // Initialize the DHT11 sensor
  dht.begin();
}

void loop() {
  // Wait before reading new values
  delay(2000);  // 2-second delay

  // Read humidity
  float humidity = dht.readHumidity();
  // Read temperature in Celsius
  float temperature = dht.readTemperature();

  // Check if readings failed and display an error message
  if (isnan(humidity) || isnan(temperature)) {
    Serial.println("Failed to read from DHT11 sensor!");
    return;
  }

  // Display values on the serial monitor
  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.print(" °C\t");

  Serial.print("Humidity: ");
  Serial.print(humidity);
  Serial.println(" %");
}