arduino stm32 DHt11
时间: 2023-10-05 14:12:13 浏览: 103
stm32驱动DHT11
3星 · 编辑精心推荐
The DHT11 is a digital temperature and humidity sensor that can be interfaced with an Arduino or an STM32 microcontroller. The sensor communicates through a single wire digital interface and provides an accuracy of ±2°C for temperature and ±5% for humidity readings.
To interface the DHT11 sensor with an Arduino or an STM32 microcontroller, you can use the following steps:
1. Connect the Vcc pin of the DHT11 sensor to the 5V pin of the microcontroller.
2. Connect the GND pin of the DHT11 sensor to the GND pin of the microcontroller.
3. Connect the data pin of the DHT11 sensor to any digital pin of the microcontroller.
For Arduino:
4. Install the DHT11 library from the Arduino IDE library manager.
5. Include the DHT11 library in the Arduino sketch.
6. Create a DHT11 object and initialize it with the data pin.
7. Read the temperature and humidity values using the read() method of the DHT11 object.
Here's a sample code to read the temperature and humidity values from the DHT11 sensor using Arduino:
#include <dht11.h>
dht11 DHT;
#define DHT11_PIN 2
void setup() {
Serial.begin(9600);
}
void loop() {
int chk = DHT.read(DHT11_PIN);
Serial.print("Temperature: ");
Serial.print(DHT.temperature);
Serial.print("°C | Humidity: ");
Serial.print(DHT.humidity);
Serial.println("%");
delay(2000);
}
For STM32:
4. Download the DHT11 library from the internet.
5. Include the DHT11 library in the STM32 project.
6. Create a DHT11 object and initialize it with the data pin.
7. Read the temperature and humidity values using the read() method of the DHT11 object.
Here's a sample code to read the temperature and humidity values from the DHT11 sensor using STM32:
#include "DHT11.h"
DHT11 dht11(GPIOA, GPIO_PIN_0);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
while (1)
{
if (dht11.read() == DHT11_OK)
{
printf("Temperature: %d°C | Humidity: %d%%\n", dht11.getTemperature(), dht11.getHumidity());
}
HAL_Delay(2000);
}
}
Note: The data pin of the DHT11 sensor should be connected to a GPIO pin that supports input/output mode and has a pull-up resistor. Also, make sure to use a capacitor between the Vcc and GND pins of the DHT11 sensor to stabilize the power supply.
阅读全文