Development steps

Project planning and development stages

Step 3 - Add temperature, Humidty sensor

Back to project development

Add temperature, Humidty sensor

  • Display Temperature, Humidity, Barometric pressure;

We already have a OLED display using the SDA and SCL pins, but because the OLED display and the BME 280 use the I2C communication protocol we can share the pin by addressing each module byt it's on address.

I²C: Inter Integrated Circuit (I-squared-C) - Synchronous, multi-master, multi-slave communication protocol.

We will use the multiple slaves to one master:XIAO ESP32 reads from a BME280 sensor using I2C and writes the sensor readings in an I2C OLED display.

First I need to find the I2C addresses for the OLED and for the BME280.

I will be using the following sketch from Random Nerd Tutorials:



  /*********
  Rui Santos
  Complete project details at https://randomnerdtutorials.com  
*********/

#include <Wire.h>
 
void setup() {
  Wire.begin();
  Serial.begin(115200);
  Serial.println("\nI2C Scanner");
}
 
void loop() {
  byte error, address;
  int nDevices;
  Serial.println("Scanning...");
  nDevices = 0;
  for(address = 1; address < 127; address++ ) {
    Wire.beginTransmission(address);
    error = Wire.endTransmission();
    if (error == 0) {
      Serial.print("I2C device found at address 0x");
      if (address<16) {
        Serial.print("0");
      }
      Serial.println(address,HEX);
      nDevices++;
    }
    else if (error==4) {
      Serial.print("Unknow error at address 0x");
      if (address<16) {
        Serial.print("0");
      }
      Serial.println(address,HEX);
    }    
  }
  if (nDevices == 0) {
    Serial.println("No I2C devices found\n");
  }
  else {
    Serial.println("done\n");
  }
  delay(5000);          
}


The connection Schematic:

Sketch serial output:

Two Addresses:

  • 0x3C: OLED display
  • 0x76: BME 280

Now the code to get the data from the BME280 and display it.


/*
    Pedro Candeias, based on a Sketch by Rui Santos from 
    https://RandomNerdTutorials.com/
  */
  
  #include <Wire.h>
  #include <Adafruit_GFX.h>
  #include <Adafruit_SSD1306.h>
  #include <Adafruit_Sensor.h>
  #include <Adafruit_BME280.h>
  
  #define SCREEN_WIDTH 128 // OLED display width, in pixels
  #define SCREEN_HEIGHT 64 // OLED display height, in pixels
  
  Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
  
  #define SEALEVELPRESSURE_HPA (1013.25)
  
  Adafruit_BME280 bme;
  
  void setup() {
    Serial.begin(115200);
  
    if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
      Serial.println(F("SSD1306 allocation failed"));
      for(;;);
    }
    
    bool status = bme.begin(0x76);  
    if (!status) {
      Serial.println("Could not find a valid BME280 sensor, check wiring!");
      while (1);
    }
    
    delay(2000);
    display.clearDisplay();
    display.setTextColor(WHITE);
  }
  
  void loop() {
    display.clearDisplay();
    // display temperature
    display.setTextSize(1);
    display.setCursor(0,0);
    display.print("Temperature: "+String(bme.readTemperature())+"C");
    
    // display humidity
    display.setTextSize(1);
    display.setCursor(0, 10);
    display.print("Humidity: "+String(bme.readHumidity())+" %");
    
    // display pressure
    display.setTextSize(1);
    display.setCursor(0, 20);
    display.print("Pressure: "+String(bme.readPressure() / 100.0F)+" hPa");
    
    // display altitude
    display.setTextSize(1);
    display.setCursor(0, 30);
    display.print("Approx. Altitude: "+String(bme.readAltitude(SEALEVELPRESSURE_HPA))+"m");
  
    
    display.display();
  
    delay(1000);
  }

Links

Random Nerd Tutorials: ESP32 I2C