This is the multi-page printable view of this section. Click here to print...

Return to the regular view of this page

As of 2025-07-24

BMx280

Environmental sensor
    This is a pressure, temperature, and humidity (humidity only for BME280) sensor using the I2C bus.

    Process Flow

    1. Wire.begin(): Initialize the bus
    2. .setup(): Initialize the sensor
    3. .begin(): Start sensor operation
    4. Wait for several milliseconds
    5. .available() becomes true
    6. .get_press(), .get_temp(), .get_humid(): Read values

    Procedures Required for Operation

    Wire Bus

    Before calling the setup() method, ensure Wire is operational by calling Wire.begin().

    Procedures for Sleep Wake-up

    Before entering sleep, keep the Wire bus operational (Wire will automatically recover after waking from sleep).

    Code Example

    ##include <TWELITE>
    ##include <SNS_BME280>
    
    SNS_BME280 sns_bme280; // Declare the object
    

    You need to include #include <SNS_SHT3X> and declare an SNS_SHT3X class object.

    Initialization

    void setup() {
        Wire.begin();
        sns_bme280.setup();
    }

    Start Getting Sensor Values

    void loop() {
    
      if(eState == E_STATE::INIT) {
        sns_bme280.begin();
        eState = E_STATE::CAPTURE;
      }
    
    }

    Call .begin() to start getting sensor values. It takes several milliseconds to complete.

    Note: The above loop() is designed to branch processing based on the state variable eState. (Reference)

    Waiting for Sensor Value Acquisition

    void loop() {
    
      if(eState == E_STATE::CAPTURE) {
        if (sns_bme280.available()) {
          // Sensor values ready to read
        }
      }
    
    }

    You can check if sensor values are ready by .available().

    Reading Sensor Values

    void loop() {
    
      if(eState == E_STATE::CAPTURE) {
        if (sns_bme280.available()) {
          Serial << crlf << "BMx280:"
                << " P=" << int(sns_bme280.get_press()) << "hPa";
                << " T=" << sns_bme280.get_temp() << 'C'
    						<< " H=" << sns_bme280.get_humid() << '%';
        }
      }
    
    }

    Once sensor values are ready, you can read them.

    .get_temp(), get_humid() involve floating-point operations. You can also get values as integers multiplied by 100.

    auto temp = div100(sns_bme280.get_temp_cent());
    auto humd = div100(sns_bme280.get_humid_per_dmil);
    
    Serial << crlf << "BMx280:"
      << " P=" << int(sns_bme280.get_press()) << "hPa";
      << format(" T=%c%d.%02d", temp.neg ? '-' : ' ', temp.quo, temp.rem)
      << format(" H=%c%d.%02d", humd.neg ? '-' : ' ', humd.quo, humd.rem);

    Here, div100() is used to split the value multiplied by 100 into integer and fractional parts.

    Methods

    get_press()

    int16_t get_press()

    Reads pressure. The unit is hectopascal (hPa), usually around 1000.

    get_temp(), get_temp_cent()

    double get_temp()
    int16_t get_temp_cent()

    Reads temperature. get_temp() returns °C as a double, and get_temp_cent() returns temperature multiplied by 100 as an integer.

    Returns values between -32760 and -32768 on error.

    get_humid(), get_humid_per_dmil()

    double get_humid()
    int16_t get_humid_per_dmil()

    Reads humidity. get_humid() returns % as a double, and get_humid_per_dmil() returns humidity multiplied by 100 as an integer.

    Returns values between -32760 and -32768 on error.

    Common Methods

    setup()

    void setup(uint32_t arg1 = 0UL)

    Allocates memory and initializes the sensor.

    The lower 8 bits of arg1 can store the I2C address. If not specified, it defaults to 0.

    ##include <SNS_BME280>
    SNS_BME280 sns_bme280;
    bool b_found_bme280 = false;
    
    void setup() {
      ...
      sns_bme280.setup();
    	if (!sns_bme280.probe()) {
    			delayMicroseconds(100); // device needs small time for further I2C comm.
    			sns_bme280.setup(0x77); // alternative ID
    			if (sns_bme280.probe()) b_found_bme280 = true;
    	} else {
    			b_found_bme280 = true;
    	}
    	...

    The code first tries the default I2C ID to check device response; if no response, tries 0x77 as an alternative.

    begin(), end()

    void begin()
    void end()

    Starts sensor acquisition. It takes several milliseconds to read sensor values; wait until available() returns true.

    end() is not supported.

    process_ev()

    void process_ev(uint32_t arg1, uint32_t arg2 = 0)

    For sensors with wait time processing, pass E_EVENT_TICK_TIMER or E_EVENT_START_UP in arg1 to notify elapsed time. After this call, if enough time has passed, available() will return true and sensor values can be read.

    available()

    bool available()

    Returns true when sensor reading conditions are met.

    probe()

    bool probe()

    Returns true when the sensor is connected.

    sns_stat()

    uint32_t sns_stat()

    Contains various sensor device information.

    • The lower 8 bits store the chip model of BME280/BMP280. 0x60 means BME280, and 0x58 means BMP280.

    sns_opt()

    uint32_t& sns_opt()

    Stores the value passed to setup(uint32_t arg1).

    • The lower 8 bits store the specified I2C device address.