AHT10接入esp 8266
时间: 2024-01-16 12:15:29 浏览: 133
AHT10是一款温湿度传感器,可以通过I2C接口与ESP8266进行通信。首先需要将AHT10连接到ESP8266的I2C总线上,然后通过I2C库读取传感器数据。以下是一个简单的示例代码:
```c
#include <Wire.h>
#define AHT10_I2C_ADDR 0x38
void setup() {
Wire.begin();
Serial.begin(9600);
}
void loop() {
Wire.beginTransmission(AHT10_I2C_ADDR);
Wire.write(0xAC); // 发送读取命令
Wire.endTransmission();
delay(100);
Wire.requestFrom(AHT10_I2C_ADDR, 6);
if (Wire.available() == 6) {
uint8_t buf[6];
for (int i = 0; i < 6; i++) {
buf[i] = Wire.read();
}
uint32_t raw_humidity = ((uint32_t)buf[1] << 16) | ((uint16_t)buf[2] << 8) | buf[3];
uint32_t raw_temperature = ((uint32_t)(buf[3] & 0x0F) << 16) | ((uint16_t)buf[4] << 8) | buf[5];
float humidity = (float)raw_humidity * 100 / 0x1000000; float temperature = (float)(raw_temperature & 0x7FFFFF) * 200 / 0x800000 - 50;
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print("%, Temperature: ");
Serial.print(temperature); Serial.println("℃");
}
delay(1000);
}
```
阅读全文