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. | Component | Quantity |
---|---|---|
1. | XIAO SAMD21 + USB-C cable | 1 |
2. | DHT11 sensor | 1 |
3. | 10 kΩ resistor | 1 |
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.
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
- Install the Adafruit DHT sensor library for reading temperature and humidity:
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.
#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.
Leave a Comment
Cancel reply