ESP8266 and BME280 Temp, Pressure and Humidity Sensor over SPI

esp2866-bme280-2.jpg

As part of a bigger project to measure temperature and humidity I’ve set up the NodeMcu ESP8266 (ESP12E) with a GY BME280 as below. I’m using ‘Software’ SPI which means I assign the pins used for the SPI bus in the code rather than use the standard SPI pins on the NodeMcu.

ESP8266 Single BME280 Wiring Diagram

ESP8266 with a single BME280 Wiring Diagram

ESP8266 single BME280 breadboard wiring

Illustration showing the ESP8266 wired to the BME280 on a breadboard.

ESP8266 with BME280

Image showing the wiring for a single BME280 with the ESP8266

ESP8266 with BME280

Image to show the pins on the BME280 that are connected.

The code is below. You might need to install the two Adafruit libraries. The Sensor one can be difficult to find in the Arduino libraries – search unified abstraction and it should show at the top.

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

// assign the ESP8266 pins to arduino pins
#define D1 5
#define D2 4
#define D4 2
#define D5 14

// assign the SPI bus to pins
#define BME_SCK D1
#define BME_MISO D4
#define BME_MOSI D2
#define BME_CS D5

#define SEALEVELPRESSURE_HPA (1013.25)

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
    status = bme.begin();
    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();
}

These are the actual items I used but you can find the same on many other sites.
ESP8266 – http://www.ebay.es/itm/201838061951
BME280 – http://www.ebay.es/itm/112471267075

Leave a Reply

Your email address will not be published. Required fields are marked *

scroll to top