Adsense HTML/JavaScript

Thursday, February 11, 2021

Test AHT20+BMP280 Temperature, Humidity and Pressure Sensor Module on Seeeduino XIAO with SSD1306 I2C OLED

AHT20+BMP280 Digital Temperature, Humidity and Pressure Sensor Module
It's a module adopts the digital temperature and humidity sensor AHT20 and Bosch BPM280 composed of Aosong, I2C mainstream output,


This post test it on Seeeduino XIAO with SSD1306 I2C OLED, in Arduino framework.


Connection:


Library:

To install SSD1306 library on Arduino IDE, refer to the post "Seeeduino XIAO display on OLED screen (with ssd1306 using I2C interface)".

For AHT20+BMP280,  AHTX0 and BMP280 libraries by Adafruit will be used, both them can be installed in Arduino IDE Library Manager.


AHT20 Testing:

X_AHT20_ssd1306.ino, modified from Examples > Adafruit AHTX0 > adafruit_aht_test.
#include "ssd1306.h"
#include <Adafruit_AHTX0.h>

Adafruit_AHTX0 aht;

void setup() {

  Serial.begin(9600);
  Serial.println("AHT20 test");

  if (! aht.begin()) {
    Serial.println("Could not find AHT? Check wiring");
    while (1) delay(10);
  }
  Serial.println("AHT10 or AHT20 found");
  
  ssd1306_setFixedFont(ssd1306xled_font6x8);
  ssd1306_128x64_i2c_init();
  ssd1306_clearScreen();
  ssd1306_printFixed(0,  8, "arduino-er exercise", STYLE_NORMAL);
  ssd1306_printFixed(0, 16, "XIAO/ssd1306", STYLE_BOLD);
  ssd1306_printFixed(0, 24, "AHT20 test", STYLE_ITALIC);
  ssd1306_positiveMode();

  delay(2000);
  ssd1306_clearScreen();
}

void loop() {
  sensors_event_t humidity, temp;
  aht.getEvent(&humidity, &temp);// populate temp and humidity objects with fresh data
  Serial.print("Temperature: "); 
  Serial.print(temp.temperature); 
  Serial.println(" degrees C");

  ssd1306_printFixed(0, 10, "- AHT20 -", STYLE_BOLD);
  
  char charTemp[32];
  dtostrf(temp.temperature, 8, 2, charTemp);
  ssd1306_printFixed(0, 28, "Temperature (C):", STYLE_NORMAL);
  ssd1306_printFixed(80, 36, charTemp, STYLE_NORMAL);
  
  Serial.print("Humidity: "); 
  Serial.print(humidity.relative_humidity); 
  Serial.println("% rH");

  char charHumi[32];
  dtostrf(humidity.relative_humidity, 8, 2, charHumi);
  ssd1306_printFixed(0, 44, "Humidity (% rH):", STYLE_NORMAL);
  ssd1306_printFixed(80, 52, charHumi, STYLE_NORMAL);

  delay(500);
}


BMP280 Testing:

X_BMP280_ssd1306.ino, modified from Examples > Adafruit BMP280 Library > bmp280test.
#include "ssd1306.h"
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_BMP280.h>

#define BMP_SCK  (13)
#define BMP_MISO (12)
#define BMP_MOSI (11)
#define BMP_CS   (10)

Adafruit_BMP280 bmp; // I2C
//Adafruit_BMP280 bmp(BMP_CS); // hardware SPI
//Adafruit_BMP280 bmp(BMP_CS, BMP_MOSI, BMP_MISO,  BMP_SCK);

void setup() {

  if (!bmp.begin()) {
    Serial.println(F("Could not find a valid BMP280 sensor, check wiring!"));
    while (1);
  }
  Serial.println("BMP280 found");

  /* Default settings from datasheet. */
  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. */
  
  ssd1306_setFixedFont(ssd1306xled_font6x8);
  ssd1306_128x64_i2c_init();
  ssd1306_clearScreen();
  ssd1306_printFixed(0,  8, "arduino-er exercise", STYLE_NORMAL);
  ssd1306_printFixed(0, 16, "XIAO/ssd1306", STYLE_BOLD);
  ssd1306_printFixed(0, 24, "BMP280 test", STYLE_ITALIC);
  ssd1306_positiveMode();

  delay(2000);
  ssd1306_clearScreen();
}

void loop() {
  ssd1306_printFixed(0, 8, "- BMP280 -", STYLE_BOLD);

  Serial.print(F("Temperature = "));
  Serial.print(bmp.readTemperature());
  Serial.println(" *C");
  
  char charTemp[32];
  dtostrf(bmp.readTemperature(), 8, 2, charTemp);
  ssd1306_printFixed(0, 16, "Temperature (C):", STYLE_NORMAL);
  ssd1306_printFixed(60, 24, charTemp, STYLE_NORMAL);
  
  Serial.print(F("Pressure = "));
  Serial.print(bmp.readPressure());
  Serial.println(" Pa");

  char charPres[32];
  dtostrf(bmp.readPressure(), 8, 2, charPres);
  ssd1306_printFixed(0, 32, "Pressure (Pa):", STYLE_NORMAL);
  ssd1306_printFixed(60, 40, charPres, STYLE_NORMAL);

  Serial.print(F("Approx altitude = "));
  Serial.print(bmp.readAltitude(1013.25)); /* Adjusted to local forecast! */
  Serial.println(" m");

  char charAlti[32];
  dtostrf(bmp.readAltitude(1013.25), 8, 2, charAlti);
  ssd1306_printFixed(0, 48, "Approx altitude (m):", STYLE_NORMAL);
  ssd1306_printFixed(60, 56, charAlti, STYLE_NORMAL);

  Serial.println();
  delay(2000);
}



Remark for connector:

My module come in Grove-like connector, but not in Grove standard pins order. So I have to re-order it by myself, to make it fit my Seeed Grove Breadboard.