Adafruit BME280 SPI frequency

multidig
Posts: 3
Joined: Wed Aug 27, 2025 1:33 pm

Adafruit BME280 SPI frequency

Postby multidig » Wed Aug 27, 2025 1:57 pm

Hi all.

I'm using an ESP32 DOIT DEVKIT V1 development board and the BME280 sensor. Below is the code for reading temperature, pressure, and humidity data via SPI. The code uses the "Adafruit BME280" libraries and works correctly, but the SPI frequency is fixed at 2 MHz. I'd like to know how to change the SPI frequency.

Many Thanks.
Regards

Code: Select all

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

#define BME_SCK 18
#define BME_MISO 19
#define BME_MOSI 23
#define BME_CS 5

Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK);

void setup() {
  Serial.begin(115200);
  Serial.println("BME280 test on ESP32 via SPI");

  if (!bme.begin()) {
    Serial.println("Error! BME280 sensor not found.");
    while (1);
  }

  Serial.println("BME280 ok!");
}

void loop() {
  float temperature = bme.readTemperature();
  float pressure = bme.readPressure() / 100.0F; // hPa convertion
  float humidity = bme.readHumidity();

  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.println(" °C");

  Serial.print("Pressure: ");
  Serial.print(pressure);
  Serial.println(" hPa");

  Serial.print("Humidity: ");
  Serial.print(humidity);
  Serial.println(" %");

  Serial.println("--------------------");

  delay(2000); // Wait 2 seconds
}

lbernstone
Posts: 1132
Joined: Mon Jul 22, 2019 3:20 pm

Re: Adafruit BME280 SPI frequency

Postby lbernstone » Thu Aug 28, 2025 1:29 am

You can edit the library to directly add the frequency in Adafruit_BME280.cpp. It is the next parameter in Adafruit_SPIDevice

multidig
Posts: 3
Joined: Wed Aug 27, 2025 1:33 pm

Re: Adafruit BME280 SPI frequency

Postby multidig » Thu Aug 28, 2025 7:41 am

Hi Ibernstone, thanks for the answer.

Is this the only way to change the frequency? Is there a function that allows to override the frequency without directly modifying the Adafruit BME280 library?

Regards

lbernstone
Posts: 1132
Joined: Mon Jul 22, 2019 3:20 pm

Re: Adafruit BME280 SPI frequency

Postby lbernstone » Thu Aug 28, 2025 3:37 pm

No, you are using other people's code- you get what you get
This library likely has a fixed frequency because there isn't much point in increasing the frequency (and thereby power consumption) to go from 1000 samples per second to 1500 for barometric pressure (it typically changes over hours).

multidig
Posts: 3
Joined: Wed Aug 27, 2025 1:33 pm

Re: Adafruit BME280 SPI frequency

Postby multidig » Thu Aug 28, 2025 4:56 pm

Ok, I understand.

However, I tried to modify the Adafruit_BME280.cpp but nothing change. I'm using Visual Studio Code and the Adafruit_BME280.cpp to modify should be contained at the path ".pio\libdeps\esp32doit-devkit-v1\Adafruit BME280 Library", is this correct?.

I modified the following function:

Code: Select all

/*!
 *   @brief  class constructor if using hardware SPI
 *   @param  cspin the chip select pin to use
 *   @param  *theSPI
 *           optional SPI object
 */
Adafruit_BME280::Adafruit_BME280(int8_t cspin, SPIClass *theSPI) {
  spi_dev = new Adafruit_SPIDevice(cspin, 1000000, SPI_BITORDER_MSBFIRST,
                                   SPI_MODE0, theSPI);
}
In the previous function, the default SPI frequency is 1MHz (1000000) but whatever value I put, the real measured frequency on the SCL line is always 2MHz. Maybe I'm editing the wrong Adafruit_BME280.cpp library?

Thanks

Who is online

Users browsing this forum: No registered users and 2 guests