Skip to content

Instantly share code, notes, and snippets.

@ClearlyKyle
Created March 15, 2024 19:24
Show Gist options
  • Save ClearlyKyle/7f7db35c741dd4dda2645d71d9a1fbe8 to your computer and use it in GitHub Desktop.
Save ClearlyKyle/7f7db35c741dd4dda2645d71d9a1fbe8 to your computer and use it in GitHub Desktop.
AHT20 + BMP280 combined sensor example script
#include <Adafruit_AHTX0.h>
#include <Adafruit_BMP280.h>
Adafruit_AHTX0 aht = {};
Adafruit_BMP280 bmp = {}; // I2C
void setup()
{
Serial.begin(115200);
while (!Serial)
; /* Wait for serial port to connect */
/* AHT20 SETUP */
if (!aht.begin())
{
Serial.println("Could not find AHT20? Check wiring!");
while (1)
delay(100);
}
Serial.println("AHT20 found");
/* BMP280 SETUP */
if (!bmp.begin())
{
Serial.println("Could not find a valid BMP280 sensor, check wiring!");
while (1)
delay(100);
}
Serial.println("BMP280 found");
/* Default settings */
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. */
delay(100);
}
void loop()
{
sensors_event_t humidity = {}, temp = {};
aht.getEvent(&humidity, &temp); // Populate temp and humidity objects with fresh data
Serial.print("AHT: ");
Serial.print(temp.temperature);
Serial.print("C\t");
Serial.print(humidity.relative_humidity);
Serial.println("% rH");
Serial.print("BMP: ");
Serial.print(bmp.readTemperature());
Serial.print("C\t");
Serial.print(bmp.readPressure());
Serial.print("Pa\t");
const float seaLevelhPa = 1013.25f;
Serial.print(bmp.readAltitude(local_air_pressure));
Serial.println("m");
delay(3000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment