ESP8266 with two BME280 Sensors over SPI

esp2866-bme280x2-1.jpg

I had a problem with the wiring on this one. With SPI you use the same pins for SCLK (SCL pin on the BME280 boards) , MOSI (SDA pin) and MISO (SDO pin) and then each SPI sensor has to have its own CS (CSB pin).

Originally I had MISO sharing the same pin (D4)  I used on the single sensor version. When I connected two MISO (SDO) connections the ESP8266 was not happy. At one point it wasn’t recognised over USB. Possibly due to D4 normally being the LED pin. I changed the pins and the sketch code so the MISO (SDO) connections are on D5 and changed D4  to the CS output for the second sensor.

You can see the wiring layout below. Basically both sensors have the same connectors for everything except the CSB connections on each BME280 have their own pin on the ESP8266. These are the white cables.

ESP8266 Two BME280s Wiring Diagram

ESP8266 with two BME280s Wiring Diagram

It’s possibly easier to see the wiring on the breadboard illustration below. Everything wired to the same pins except the white cables.

Diagram showing the ESP8266 wired to both BME280s on a breadboard.

ESP8266 with two BME280s Above

Image showing the ESP8266 with two BME280s on the breadboard

BME280 Connections

Connections on the BME280s

ESP8266 Connections

Connections on the ESP8266

#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 D3 0
#define D4 2
#define D5 14

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

#define SEALEVELPRESSURE_HPA (1013.25)

Adafruit_BME280 bme1(BME1_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI
Adafruit_BME280 bme2(BME2_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 = bme1.begin();
    if (!status) {
        Serial.println("Could not find a valid BME280 sensor 1 , check wiring!");
    }

    // default settings
    status = bme2.begin();
    if (!status) {
        Serial.println("Could not find a valid BME280 sensor 2 , check wiring!");
    }    
    
    Serial.println("-- Default Test --");
    delayTime = 5000;

    Serial.println();
}


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


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

    Serial.print("Pressure = ");
    Serial.print(bme1.readPressure() / 100.0F);
    Serial.println(" hPa");
    Serial.print("Pressure = ");
    Serial.print(bme2.readPressure() / 100.0F);
    Serial.println(" hPa");    

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

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

        

    Serial.println();
}

Below are the results in the serial monitor. I don’t know why the altitude reading is different. The temperature and humidity readings are close and that’s all I’m going to use in the project this test is for.

Temperature = 30.53 *C
Temperature = 30.55 *C
Pressure = 1004.37 hPa
Pressure = 1004.05 hPa
Approx. Altitude = 74.21 m
Approx. Altitude = 76.87 m
Humidity = 65.61 %
Humidity = 65.08 %

Leave a Reply

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

scroll to top