Home ArduinoArduino Code BME280 and OLED example

BME280 and OLED example

In this example we will connect a BME280 sensor to an Arduino and display the output on an 128×32 I2C OLED display

The BME280 is a humidity sensor features an extremely fast response time which supports performance requirements for emerging applications such as context awareness, and high accuracy over a wide temperature range. The pressure sensor is an absolute barometric pressure sensor with features exceptionally high accuracy and resolution at very low noise. The integrated temperature sensor has been optimized for very low noise and high resolution. It is primarily used for temperature compensation of the pressure and humidity sensors, and can also be used for estimating ambient temperature.

The best way to use the device is a module like the one below

bme280 module

bme280 module

I found this schematic for a breakout online for the bme280, you’ll see in the sparkun image above they are using 4k7’s rather than 10K

Sparkfun has all of teh design files for their module available from https://github.com/sparkfun/SparkFun_BME280_Breakout_Board , I also find these interesting, so worth taking a look

You can use this module in either SPI or I2C modes, in this case we went for SPI mode, here is the wiring for our module to our arduino Uno

Connect Vin to 5V.
Connect GND to ground
Connect the SCK pin to Digital #13
Connect the SDO pin to Digital #12
Connect the SDI pin to Digital #11
Connect the CS pin Digital #10

OLED Connection

Connect Vin to 5V.
Connect GND to ground
Connect SDA to A4
Connect SCL to A5

Here is a schematic drawn up in fritzing

Arduino bme280 oled

Arduino bme280 oled

 

Code

The code uses the sparkfun library, I tried 2 others and couldn’t get them to compile. There are several examples available. I removed some code from a basic example

Library – https://github.com/sparkfun/SparkFun_BME280_Arduino_Library

OLED library was from Adafruit

Libary – https://github.com/adafruit/Adafruit_SSD1306

[codesyntax lang=”cpp”]

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <stdint.h>
#include "SparkFunBME280.h"

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

//Global sensor object
BME280 mySensor;

#if (SSD1306_LCDHEIGHT != 32)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif

void setup() 
{ 
 Serial.begin(57600);
 //BME280 sensor setup
 mySensor.settings.commInterface = SPI_MODE;
 mySensor.settings.chipSelectPin = 10;

 //Operation settings
 mySensor.settings.runMode = 3; //Normal mode
 mySensor.settings.tStandby = 0;
 mySensor.settings.filter = 0;
 mySensor.settings.tempOverSample = 1;
 mySensor.settings.pressOverSample = 1;
 mySensor.settings.humidOverSample = 1;
 
 Serial.print("Starting BME280... result of .begin(): 0x");
 delay(10); //BME280 requires 2ms to start up.
 Serial.println(mySensor.begin(), HEX);
 display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x32)

}


void loop() 
{
 // Clear the buffer.
 display.clearDisplay();

 // text display tests
 display.setTextSize(1);
 display.setTextColor(WHITE);
 display.setCursor(0,0);
 display.print("Temperature: ");
 display.print(mySensor.readTempC(), 2);
 display.print(" c");
 display.setCursor(0,10);
 display.print("Pressure: ");
 display.print(mySensor.readFloatPressure(), 2);
 display.setCursor(0,20);
 display.print("Altitude: ");
 display.print(mySensor.readFloatAltitudeMeters(), 2);
 display.print(" m");
 display.display();
 delay(2000);
}

[/codesyntax]

 

Output

 

All going well you should see output like the following on your OLED display

bme280 on OLED output

bme280 on OLED output

 

Links

You can pick up one of these breakout/modules for under $8
BME280 Embedded high-precision barometric pressure sensor module

0.96″ Inch I2C IIC Serial 128X64 OLED LCD

You may also like