Compilation questions

Hi, we are doing a project about the air pressure sensor. Measure barometric pressure, temperature and altitude with mega2560 and bmp280 connection. The compilation result shows sensor initialization failure, why?


The code are as follows:
#include <Wire.h>
#include <SPI.h> //SPI Bus Library

#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h> //BMP280 Library

/Define BMP280 pins/
#define BMP_SCK 13 //SCL pin
#define BMP_MISO 10 //SDO pin
#define BMP_MOSI 12 //SDA pin
#define BMP_CS 11 //CSB pin

Adafruit_BMP280 bmp(BMP_CS, BMP_MOSI, BMP_MISO, BMP_SCK);

/Sensor initialization detection/
void setup() {
Serial.begin(9600); //Set baud rate
Serial.println(F("BMP280 sensor initialization detection"));
Serial.println(F("BMP280 sensor detection success"));
/Detection of SPI bus communication/
if (!bmp.begin()) {
Serial.println(F("BMP280 sensor initialization failed"));
while (1);
}
}

void loop() {
Serial.print(F("current temperature:"));
Serial.print(bmp.readTemperature());
Serial.println(" *C");
Serial.print(F("Current air pressure value = "));
Serial.print(bmp.readPressure());
Serial.println(" Pa");
Serial.print(F("Current altitude = "));
Serial.print(bmp.readAltitude(1013.25));
Serial.println(" M");
delay(800); //Time delay detection
}

Can you show us the compilation error messages, please?

Please remember to use code tags when posting code

I think you mean that you get the message "BMP280 sensor initialization failed" on the serial monitor , and not that you get a compilation error.

Probably because you're using a constructor of this library that works with an SPI version of the sensor, but your sensor module suggests you are using an I2C version.
See this example for the I2C version you should be using: Adafruit_BMP280_Library/examples/bmp280_sensortest/bmp280_sensortest.ino at master · adafruit/Adafruit_BMP280_Library · GitHub

Please follow the advice given in the link below when posting code. Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Sorry, I am a new user. They only let me upload 1 picture. The program has no error warnings, but the serial monitor always shows initialization failure when interacting with the MEGA2560.

Thank you for your suggestion. I'll try to fix the code later.

Don't upload pictures. Copy the code using "Copy for forum" in the IDE and post it here. The IDE will add the code tags for you. Copy the Serial monitor output and paste it here adding the code tags yourself

Thank you

#include <Wire.h>     
#include <SPI.h>      //SPI Bus Library

#include <Adafruit_Sensor.h>  
#include <Adafruit_BMP280.h>      //BMP280 Library

/*Define BMP280 pins*/
#define BMP_SCK 13   //SCL pin 
#define BMP_MISO 10   //SDO pin
#define BMP_MOSI 12   //SDA pin
#define BMP_CS 11     //CSB pin

Adafruit_BMP280 bmp(BMP_CS, BMP_MOSI, BMP_MISO,  BMP_SCK);  

/*Sensor initialization detection*/
void setup() {
  Serial.begin(9600);     //Set baud rate
  Serial.println(F("BMP280 sensor initialization detection"));
  Serial.println(F("BMP280 sensor detection success"));
  /*Detection of SPI bus communication*/
  if (!bmp.begin()) {  
    Serial.println(F("BMP280 sensor initialization failed"));
    while (1);
  }
}

void loop() {
    Serial.print(F("current temperature:"));
    Serial.print(bmp.readTemperature());
    Serial.println(" *C");    
    Serial.print(F("Current air pressure value = "));
    Serial.print(bmp.readPressure());
    Serial.println(" Pa");
    Serial.print(F("Current altitude = "));
    Serial.print(bmp.readAltitude(1013.25));
    Serial.println(" M");   
    delay(800);     //Time delay detection
}

I just re-checked the datasheet of this sensor: https://cdn-shop.adafruit.com/datasheets/BST-BMP280-DS001-11.pdf
You should be able to use yours in either I2C or SPI mode. So SPI mode should also work, provided that you've made the right connections, AND you make sure that the interface selection procedure on startup involving the CSB pin is performed correctly. See section 5.1 on page 28 of the datasheet. You'd have to check the Adafruit library if this indeed performs the correct sequence.

Consult the Adafruit examples/tutorials through this page: Overview | Adafruit BMP280 Barometric Pressure + Temperature Sensor Breakout | Adafruit Learning System

1 Like

Looks like you are using the SPI pins for UNO, NOT Mega.
mega2.pdf (354.3 KB)

2 Likes

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.