Adafruit BME280 library [PORTED]

I edited the example code to make it work better for you.

SYSTEM_MODE(MANUAL); will not connect you to the Particle Cloud, default is (AUTOMATIC).

You do not need #include “Adafruit_Sensor.h” since this is included already in the Adafruit_BME280.h file.

#define BME_SCK D4
#define BME_MISO D3
#define BME_MOSI D2

Are not required (and the values are wrong for hardware SPI).

#define BME_CS D5 this is the pin your board SS/CS pin is attached to?

Adafruit_BME280 bme; // I2C
//Adafruit_BME280 bme(BME_CS); // hardware SPI

Should be

//Adafruit_BME280 bme; // I2C
Adafruit_BME280 bme(BME_CS); // hardware SPI

Because you are using hardware SPI interface and not I2C. You will need to re-compile and then flash.

1 Like

@armor
Good day armor thank you for the code and explanation but i think im missing something. I just compiled your code and although it compiled when i flashed it to the photon it did not appear on the arduino serial monitor like the other programs i worked with. If i remove the SYSTEM_MODE(MANUAL) line of code than the photon breathes green. Why is the temperature humidity and pressure measurements not displaying on the serial monitor? See attached.

@Alli are you sure COM7 is the right port? Can you also share the wiring of the sensor?

Are you using an Original Adafruit Sensor or a compatible one like the GY-BME/P280?
I tested the example code on an GY and it does not work! Also green blinking at the end with no output at serial monitor with hardware SPI connected on D5.

@mhdevx
Well its the only port available for selection and its also the only port that worked for other programs i used when testing the serial monitor.
Please find the image of my configuration attached: 3V3 connected to VCC on the sensor, GND connected to GND and CS connected to A2 (as coded using armor’s code above)

I also connected the photon to BME280 sensor as the bme820test.ino was coded: where i connected the SDI as i would have MOSI to D2, SDO as MISO to D3, SCK to D4 and CSB as CS to D5.

@Postler
It is actually a CJMCU-280E BME280 found here

but even without the sensor connected the the photon does not seem to be sending any information to the serial monitor and it should because of the following lines of code:
Serial.println(F(“BME280 test”));

// if (!bme.begin(0x76)) {
if (!bme.begin()) {
Serial.println(“Could not find a valid BME280 sensor, check wiring!”);
while (1);

nothing is being displayed to the monitor as if there is no communication taking place between the photon and serial monitor.
When i flash the bme280test.ino example found in the library my photon also breathes green (instead of cyan) indicating its connected to wifi but is not connected to the cloud. And no serial communication appears on the serial monitor. With the example armor uploaded the photon breathes white (offline) and again no serial communication appears data appears on the serial monitor.

Capture4

I think you will find that connecting SDI (MOSI) to A5, SDO (MISO) to A4 and SCK to A3 will make it work. The library is using the standard hardware SPI port and that is definitely not on D2, D3 and D4.

Is the serial monitor you are using set to 9600 baud and are you using the right COM port? You will need to go to settings, devices and see which COM port has been assigned to you photon once you have connected to the USB.

1 Like

Regarding the serial monitor, did you installed Particle CLI? This serial window on your screenshot looks like something else (but I’m using Mac, so might be different on Windows).

@armor
Those are the pins that were coded in the example. However I did flash the code you uploaded which specified CS to be connected to A2. Even if these pins were incorrect the lines of code in the setup up loop should display on the serial monitor
Yes it is. And yes it is the correct com port as well becuase when I run other programs they display on the serial monitor. Im 100% sure it is the correct baudrate and com port.

@mhdevx
Yes I did install The particle CLI but if I’m not mistaken that allows for serial communication on the particle desktop dev. Which didn’t work for any of my programs so I read a thread which said I could use the arduinoIDE serial monitor to view serial data and it worked fine for my other programs. So that serial monitor displayed is the aruino ide serial monitor

Why don’t you show us a picture of your actual and complete wiring?

In the photo above, you only have one wire connected–you are missing several.

1 Like

Thank you for the response Bko. The picture i upload was done according to the code armor uploaded where he said I only need to use the CS pin which must be connected to A2.

The when i used the other code i did the connections as i stipulated in the above messages. I connected the rest of it according to the pins specified in the photons doc manual.

Correct me if I'm wrong but even if the connection between the micro and the sensor were incorrect the code in the setup would still execute and should've displayed on the serial Monitor due to the following code:
Serial.println(F("BME280 test"));
And

Since this does not display does is not indicate that the fault is not likely to be the connection but something else ?

@Alli, I believe @armor was referring to only specifying CS in the Adafruit_BME280 constructor. He was not referring to the wiring. In order for the BME280 to work using SPI, you will need four pins to be connected as @armor said here:

The Photon's default SPI port is on pins A3 (SCK), A4 (MISO) and A5 (MOSI) with the CS pin being any pin but typically A2 since it is the default. Without having all the Photon to BME280 board pins connected, your code will never work.

As for the console output, you may want to create a simple app that prints "Hello" to the console very second so you can establish that your console program actually works. Then move on to the BME280 testing.

1 Like

@peekay123
Okay thanks for clearing that up.
When I first connected the photon to the sensor I connected the way armor specified
SCK to A3
MISO to A4
MOSI to A5
CS to A2
And I had the same problem.
Also want to ask: Will it make if I use the arduino ide serial monitor instead of the console?
Also I can undertand the fact that if my connections were incorrect my entire code would not execute but wouldn’t the code in the setup loop:
Serial.println(F(“BME280 test”));
Serial.println(“Could not find a valid BME280 sensor, check wiring!”);

execute and display on the serial monitor/console ?

@Alli, you need to test a simple app that prints “hello world” to the console every second to make sure your Arduino terminal is working as expected.

setup() {
  Serial.begin(9600);
}

loop() {
  Serial.println("Hello World");
  delay(1000);
}

If this works, I suggest putting a 5 second delay after Serial.begin(9600); in your setup to allow time for the Arduino terminal to come up correctly.

I have tested it with many other programs. Even with the hello world it’s working perfectly fine. I’m not sure what’s wrong.
I think I’m just going to switch to another library and use the I2C protocol instead.
Thanks everyone for the assitance.

I would double check that you have the data lines hooked up correctly since it is easy to swap data in and data out:

A2 (CS) goes to the sensor CSB
A3 (SCK) goes to the sensor SCK
A4 (MISO) goes to the sensor SDO
A5 (MOSI) goes to the sensor SDI

plus power and ground of course.

@Alli, I believe the issue is with the CSB pin on your BME280 board. It needs to be set as follows:

Interface selection is done automatically based on CSB (chip select) status. If CSB is connected to VDDIO, the I²C interface is active. If CSB is pulled down, the SPI interface is activated. After CSB has been pulled down once (regardless of whether any clock cycle occurred), the I²C interface is disabled until the next power-on-reset. This is done in order to avoid inadvertently decoding SPI traffic to another slave as I²C data. Since the device startup is deferred until both VDD and VDDIO are established, there is no risk of incorrect protocol detection because of the power-up sequence used. However, if I²C is to be used and CSB is not directly connected to VDDIO but is instead connected to a programmable pin, it must be ensured that this pin already outputs the VDDIO level during power-on-reset of the device. If this is not the case, the device will be locked in SPI mode and not respond to I²C commands.

So for SPI operation, CSB must be connected to GND. :wink:

1 Like

@peekay123 @bko
Hello. Thank you for the advice and assistance but nothing seems to be working to resolve this issue. I decided to use I2C with the Control everything library instead and everything is working great.

Thank you all for your time and help. i really appreciate the support i received from the particle community.

@Alli I am a newbie in microcontroller programming and encountered the same problem when using SPI interface with CS as A2 as recommended by @Postler and even performed as using D1,D2… but no luck.
will be grateful to you if able to get assistance on I2C programming for this sensor.
(P.S. I am needing it for my school project)

Hello @manjil123

Firstly, make sure that you have connected your BME280 correctly to your particle device. I used a particle photon, thus pins D0 and D1 (SDA and SCL) are the I2C pins as shown in the pinout diagram in this link:

Ensure that your BME280 is connected to the photon as shown in this link:

You will need to include the CE_BME280 library in your project first. Then use these instructions in your program to poll measurements from your BME280:

Pressure = (bme.readPressure()/100);                 //Pressure measurements in hPa
Temperature = bme.readTemperature();
Humidity = bme.readHumidity();

Its as simple as that. If you are still having a problem, post the program you created so that we can see what you are doing and try to help :+1:t2:

Here is a sample program that displays to the serial monitor:

#include <CE_BME280.h>                                          
#define Addr 0x77                            //BME280 address 119 (decimal) for the Sparkfun board

float Temperature;
float Pressure;
float Humidity;

void GetBME280SensorData(void);                //Get BME280 Sensor data
void DisplayOnSerialMonitor();

byte BME280SensorConnected=0;

CE_BME280 bme;                                      // I2C

void setup() {
    
    Serial.begin(9600);
    
    Serial.println("BME280 sensor detection test");         
        
    delay(3000);    

}

void loop() {
     
    if(!bme.begin(Addr)){          //If a BME280 sensor is not available at that Address than print this 
    
        Serial.println("BME280 sensor not connected");
        BME280SensorConnected=0;   
        
    }
    else if(bme.begin(Addr)){
       
       if(BME280SensorConnected==0){                                        
        
        Serial.println("BME280 sensor is connected and setup is complete"); 
        Serial.println("");                                                 
        BME280SensorConnected=1;                                            

        
       }
        
        GetBME280SensorData();
        DisplayOnSerialMonitor();
        
        
    }
   

}

void GetBME280SensorData(){
    

    Temperature = bme.readTemperature();
    Humidity = bme.readHumidity();
    Pressure = (bme.readPressure()/100);                    //To convert to hPa
     
}

void DisplayOnSerialMonitor(){
     
    Serial.print("Temperature ");     
    Serial.print(Temperature);
    Serial.println(" C");
    Serial.print("Humidity ");
    Serial.print(Humidity);
    Serial.println(" %");
    Serial.print("Pressure ");
    Serial.print(Pressure);
    Serial.println(" hPa");
    Serial.println("");
    delay(500);
    
}