Home CodeArduino enviro:bit BME280 and micro:bit Arduino code example

enviro:bit BME280 and micro:bit Arduino code example

by shedboy71

Sense the world around you with enviro:bit! It’s loaded with sensors for air and weather, colour and light, and sound, and slots right onto your microbit.

Just slot in your micro:bit, then code enviro:bit with the block-based Microsoft MakeCode editor, or with MicroPython in the Mu code editor.

The sensors go hand-in-hand really well with the LED matrix on micro:bit, letting you graph sensors readings or have the LEDs react to sound, for example.

Features:

  • Comes fully-assembled and ready to use.
  • BME280 temperature, pressure, and humidity sensor.
  • TCS3472 light and colour sensor.
  • MEMS microphone.
  • Compatible with micro:bit.
  • Microsoft MakeCode and MicroPython support.
  • No soldering required!

Lets look at the BME280

The BME280 is an integrated environmental sensor developed specifically for mobile applications where size and low power consumption are key design constraints. The unit combines individual high linearity, high accuracy sensors for pressure, humidity and temperature in an 8-pin metal-lid 2.5 x 2.5 x 0.93 mm³ LGA package, designed for low current consumption (3.6 μA @1Hz), long term stability and high EMC robustness.

The 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 BME280 supports a full suite of operating modes which provides the flexibility to optimize the device for power consumption, resolution and filter performance.

Parts List

BME280 Code

You need to import the BME280 and sensor librraies into the Arduino IDE. The BME280 sensor is set to 0x76 on the envirobit

[codesyntax lang=”cpp”]

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>

#define BME_SCK 13
#define BME_MISO 12
#define BME_MOSI 11
#define BME_CS 10

#define SEALEVELPRESSURE_HPA (1013.25)

Adafruit_BME280 bme; // I2C
//Adafruit_BME280 bme(BME_CS); // hardware SPI
//Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI

unsigned long delayTime;

void setup() {
    Serial.begin(9600);
    Serial.println(F("BME280 test"));

    bool status;
    
    // default settings
    // (you can also pass in a Wire library object like &Wire2)
    status = bme.begin(0x76);  
    if (!status) {
        Serial.println("Could not find a valid BME280 sensor, check wiring!");
        while (1);
    }
    
    Serial.println("-- Default Test --");
    delayTime = 1000;

    Serial.println();
}


void loop() { 
    printValues();
    delay(delayTime);
}


void printValues() {
    Serial.print("Temperature = ");
    Serial.print(bme.readTemperature());
    Serial.println(" *C");

    Serial.print("Pressure = ");

    Serial.print(bme.readPressure() / 100.0F);
    Serial.println(" hPa");

    Serial.print("Approx. Altitude = ");
    Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
    Serial.println(" m");

    Serial.print("Humidity = ");
    Serial.print(bme.readHumidity());
    Serial.println(" %");

    Serial.println();
}

[/codesyntax]

BME280 output

Open the serial monitor window and you will see something like this

Temperature = 25.46 *C
Pressure = 972.81 hPa
Approx. Altitude = 342.30 m
Humidity = 42.62 %

Temperature = 25.45 *C
Pressure = 972.83 hPa
Approx. Altitude = 342.10 m
Humidity = 42.61 %

Temperature = 25.45 *C
Pressure = 972.76 hPa
Approx. Altitude = 342.22 m
Humidity = 42.62 %

You may also like

Leave a Comment