ESP32读取AHT20与BMP280
1. 硬件环境
ESP32-C3开发板 (合宙)
AHT20温湿度传感器
BMP280气压计
2. 软件环境Vscode、Arduino
plateformio.ini
[env:airm2m_core_esp32c3]
platform = espressif32
board = airm2m_core_esp32c3
framework = arduino
upload_port = COM13
monitor_speed = 115200
board_build.flash_mode = dio
lib_deps =
adafruit/Adafruit AHTX0@^2.0.3
adafruit/Adafruit BMP280 Library@^2.6.6
main.cpp
#include <Adafruit_AHTX0.h>
#include <Adafruit_BMP280.h>
#include <Arduino.h>
#include <Wire.h>
Adafruit_AHTX0 aht;
Adafruit_BMP280 bmp; // I2C
void setup() {
Serial.begin(115200);
// sda-scl
Wire.setPins(GPIO_NUM_3, GPIO_NUM_2);
if (!aht.begin(&Wire, 0L, 0x38)) {
Serial.println("Could not find AHT? Check wiring");
while (1)
delay(10);
}
Serial.println("AHT10 or AHT20 found");
if (!bmp.begin(0x77, 0x58)) {
Serial.println(
F("Could not find a valid BMP280 sensor, check wiring or "
"try a different address!"));
Serial.print("SensorID was: 0x");
Serial.println(bmp.sensorID(), 16);
Serial.print(
" ID of 0xFF probably means a bad address, a BMP 180 or BMP "
"085\n");
Serial.print(" ID of 0x56-0x58 represents a BMP 280,\n");
Serial.print(" ID of 0x60 represents a BME 280.\n");
Serial.print(" ID of 0x61 represents a BME 680.\n");
while (1)
delay(10);
}
bmp.setSampling(Adafruit_BMP280::MODE_NORMAL, /* Operating Mode. */
Adafruit_BMP280::SAMPLING_X2, /* Temp. oversampling */
Adafruit_BMP280::SAMPLING_X16, /* Pressure oversampling */
Adafruit_BMP280::FILTER_X16, /* Filtering. */
Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */
}
void loop() {
delay(1000);
sensors_event_t humidity, temp;
aht.getEvent(&humidity, &temp);
printf("温度:%f -- 湿度: %f\n", temp.temperature,
humidity.relative_humidity);
delay(100);
Serial.print(F("Temperature = "));
Serial.print(bmp.readTemperature());
Serial.println(" *C");
Serial.print(F("Pressure = "));
Serial.print(bmp.readPressure());
Serial.println(" Pa");
Serial.print(F("Approx altitude = "));
Serial.print(bmp.readAltitude(1013.25)); /* Adjusted to local forecast! */
Serial.println(" m");
Serial.println();
}
评论区