Use DHT11 Sensor with XIAO SAMD21

Use DHT11 Sensor with XIAO SAMD21

DHT11 is a temperature and humidity sensor. It can be used in various applications such as weather stations, garden and greenhouses monitoring systems, air conditioning systems, etc.

This tutorial shows how to use DHT11 sensor with XIAO SAMD21 development board.

Components

No.ComponentQuantity
1.XIAO SAMD21 + USB-C cable1
2.DHT11 sensor1
3.10 kΩ resistor1

Circuit diagram

The data pin of DHT11 sensor is connected to the D1 pin on the XIAO SAMD21 board. 10 kΩ pull-up resistor is added between VCC and the data pin.

DHT11 Sensor with XIAO SAMD21 (Circuit diagram)

Project setup

For project setup, the PlatformIO Core CLI tool is used. Make sure you have installed it.

  • Create a new directory for project and navigate to it:
mkdir xiao_project && cd xiao_project
  • Run the following command to initialize a new project:
pio project init --board seeed_xiao
pio pkg install --library "adafruit/DHT sensor library"

Code

In the setup function, the DHT11 sensor is initialized. Next, we read temperature and humidity values, which are printed to the serial port.

src/main.cpp

#include <Arduino.h>
#include <DHT_U.h>

#define DHTPIN 1
#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);

void setup()
{
    Serial.begin(115200);
    dht.begin();
}

void loop()
{
    float temperature = dht.readTemperature();
    float humidity = dht.readHumidity();

    Serial.print(temperature, 1);
    Serial.print(", ");
    Serial.println(humidity, 1);
    delay(1000);
}

Build the project and upload the program to the development board:

pio run --target upload

Testing

Connect XIAO SAMD21 via USB to a PC. Open the Serial Monitor to see temperature and humidity measurements.

Testing DHT11 Sensor with XIAO SAMD21

Leave a Comment

Cancel reply

Your email address will not be published.