ESP8266 and BME280 Temp, Pressure and Humidity Sensor over SPI

esp2866 with bme280

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.

[col type=”half”]

ESP8266 with BME280

Image showing the wiring for a single BME280 with the ESP8266

[/col]

[col type=”half last”]

ESP8266 with BME280

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

[/col]

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 D3 0

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

#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

[box style=”0″][coffee][/box]

9 Replies to “ESP8266 and BME280 Temp, Pressure and Humidity Sensor over SPI”

  1. Ian says:

    I get Could not find a valid BME280 sensor, check wiring!

  2. tom says:

    I get the error too…
    Is it possible that the code is pointing at the wrong wires?
    // assign the ESP8266 pins to arduino pins
    #define D1 5
    #define D2 4
    #define D4 2
    #define D5 14

    Thanks

  3. Suren says:

    The schematics show D1 to D4 connections but in the code the mapping is for D1, D2, D4, D5. Messy 🙁

  4. WordBot says:

    Arg! I must have made a mistake when I copied this Sketch from my other computer or something as the pin definitions weren’t correct.

    I’ve fixed the code. Sorry for the wasted time.

  5. Victor Peters says:

    How do I add this code to existing code for ubidots?

    1. WordBot says:

      Does this help? – https://help.ubidots.com/connect-your-devices/connect-a-nodemcu-esp8266-to-ubidots-over-http

      If someone buys me lots of coffee I can write an article on how to combine the two.

      1. Victor Peters says:

        No already got that far, brain function severely diminished ie. older than dirt

  6. Anush says:

    I am getting the same issue – “find a valid BME280 sensor, check wiring!”

    Wiring is the same as per the image

    Error Stack:

    1. WordBot says:

      Hi, Which 8266 board are you using?

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

scroll to top