This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

BME280[I2C] return same values using TWIM

Hello

I have nRF52832 custom board and have BME280 sensor connected to I2C/TWI 

Whatever I do I get same reading (for 3 different sensors)

I'm constantly reading same values

<00> info>app: TWI device detected at address 0x76
<00> debug> app: Pressure 64275
<00> debug> app: Temperature 2239
<00> debug> app: Humidity 96241

and here is my I2C/TWI init 

void twi_init(void)
{       
    ret_code_t err_code;

    const nrfx_twim_config_t i2c_config = 
    {
       .scl                = I2C_SCL_PIN,
       .sda                = I2C_SDA_PIN,
       .frequency          = NRF_TWI_FREQ_100K,
       .interrupt_priority = NRFX_TWI_DEFAULT_CONFIG_IRQ_PRIORITY,
       .hold_bus_uninit    = NRFX_TWI_DEFAULT_CONFIG_HOLD_BUS_UNINIT
    };

    err_code = nrfx_twim_init(&i2c, &i2c_config, NULL, NULL);
    APP_ERROR_CHECK(err_code);

    if (NRF_SUCCESS == err_code)
    {
        nrfx_twim_enable(&i2c); 
    }
}

also I2C read and write methods

int8_t twi_read(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len)
{
    ret_code_t err_code;
    const nrfx_twim_xfer_desc_t xfer_desc  = NRFX_TWIM_XFER_DESC_TXRX(id, &reg_addr, sizeof(reg_addr), data, len);
    err_code = nrfx_twim_xfer(&i2c, &xfer_desc, 0);
    return (err_code == NRF_SUCCESS) ? 0 : -1;
}


int8_t twi_write(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len)
{
    ret_code_t err_code;
    const nrfx_twim_xfer_desc_t xfer_desc  = NRFX_TWIM_XFER_DESC_TX(id, data, len);
    err_code = nrfx_twim_xfer(&i2c, &xfer_desc, 0);
    return (err_code == NRF_SUCCESS) ? 0 : -1;
}

and finally BME280 reading 

bme280_data_t twi_bme280_read()
{
	int8_t rslt = BME280_OK;
    uint8_t settings_sel;
	bme280_data_t ret;

    bme280.settings.osr_h = BME280_OVERSAMPLING_1X;
    bme280.settings.osr_p = BME280_OVERSAMPLING_16X;
    bme280.settings.osr_t = BME280_OVERSAMPLING_2X;
    bme280.settings.filter = BME280_FILTER_COEFF_16;
    bme280.settings.standby_time = BME280_STANDBY_TIME_62_5_MS;

    settings_sel = BME280_OSR_PRESS_SEL;
	settings_sel |= BME280_OSR_TEMP_SEL;
	settings_sel |= BME280_OSR_HUM_SEL;
	settings_sel |= BME280_STANDBY_SEL;
	settings_sel |= BME280_FILTER_SEL;

	rslt = bme280_set_sensor_settings(settings_sel, &bme280);
    if (rslt != BME280_OK) {
		NRF_LOG_DEBUG("bme280_set_sensor_settings (%d).", rslt);
	}

    rslt = bme280_set_sensor_mode(BME280_FORCED_MODE, &bme280);
	if (rslt != BME280_OK) {
		NRF_LOG_DEBUG("bme280_set_sensor_mode (%d).", rslt);
	}

    /* Wait for the measurement to complete and print data */
    nrf_delay_ms(40);

    rslt = bme280_get_sensor_data(BME280_ALL, &data, &bme280);
    if (rslt != BME280_OK) {
		NRF_LOG_DEBUG("bme280_get_sensor_data (%d).", rslt);
	}

    ret.humidity = (data.humidity / 1024.0f) * 100;
    ret.pressure = data.pressure;
    ret.temperature = data.temperature;

    NRF_LOG_DEBUG("Pressure %d hPa", data.pressure / 100);
    NRF_LOG_DEBUG("Temperature %d C", data.temperature);
    NRF_LOG_DEBUG("Humidity %d %%", data.humidity);

    NRF_LOG_DEBUG("Temperature Calibration (T1, T2, T3) %d %d %d\n", bme280.calib_data.dig_T1, bme280.calib_data.dig_T2, bme280.calib_data.dig_T3);

    print_sensor_data(&data);
	return ret;
}

void print_sensor_data(struct bme280_data *comp_data)
{
#ifdef BME280_FLOAT_ENABLE
        NRF_LOG_DEBUG("%0.2f, %0.2f, %0.2f\r\n",comp_data->temperature, comp_data->pressure, comp_data->humidity);
#else
        NRF_LOG_DEBUG("T: %ld, P: %ld, H: %ld\r\n",comp_data->temperature, comp_data->pressure, comp_data->humidity);
#endif
}

If anyone can help me what I'm doing wrong Disappointed 

I'm going in circles and can't figure it out, but when connect to  Arduino sensor works Slight smile

so is not hardware as is detected on I2C bus (as you can see in log)

Parents
  • After hooking I2C logic analyzer I got next results:

     Decoded Protocol Result
    Setup Write to [0xEC] + ACK
    0xD0 + ACK
    Setup Read to [0xED] + ACK
    0x60 + NAK
    Setup Write to [0xEC] + ACK
    0xB6 + ACK
    Setup Write to [0xEC] + ACK
    0x88 + ACK
    Setup Read to [0xED] + ACK
    0x4A + ACK
    0x71 + ACK
    0x25 + ACK
    0x68 + ACK
    0x32 + ACK
    0x00 + ACK
    0xE7 + ACK
    0x89 + ACK
    0xDA + ACK
    0xD6 + ACK
    0xD0 + ACK
    0x0B + ACK
    0x27 + ACK
    0x19 + ACK
    0xCA + ACK
    0xFF + ACK
    0xF9 + ACK
    0xFF + ACK
    0xAC + ACK
    0x26 + ACK
    0x0A + ACK
    0xD8 + ACK
    0xBD + ACK
    0x10 + ACK
    0x00 + ACK
    0x4B + NAK
    Setup Write to [0xEC] + ACK
    0xE1 + ACK
    Setup Read to [0xED] + ACK
    0x60 + ACK
    0x01 + ACK
    0x00 + ACK
    0x15 + ACK
    0x28 + ACK
    0x03 + ACK
    0x1E + NAK
    Setup Write to [0xEC] + ACK
    0xF4 + ACK
    Setup Read to [0xED] + ACK
    0x00 + NAK
    Setup Write to [0xEC] + ACK
    0x01 + ACK
    Setup Write to [0xEC] + ACK
    0xF4 + ACK
    Setup Read to [0xED] + ACK
    0x00 + NAK
    Setup Write to [0xEC] + ACK
    0x00 + ACK
    Setup Write to [0xEC] + ACK
    0xF4 + ACK
    Setup Read to [0xED] + ACK
    0x00 + NAK
    Setup Write to [0xEC] + ACK
    0x54 + ACK
    Setup Write to [0xEC] + ACK
    0xF5 + ACK
    Setup Read to [0xED] + ACK
    0x00 + NAK
    Setup Write to [0xEC] + ACK
    0x30 + ACK
    Setup Write to [0xEC] + ACK
    0xF4 + ACK
    Setup Read to [0xED] + ACK
    0x00 + NAK
    Setup Write to [0xEC] + ACK
    0xF4 + ACK
    Setup Read to [0xED] + ACK
    0x00 + NAK
    Setup Write to [0xEC] + ACK
    0x01 + ACK
    Setup Write to [0xEC] + ACK
    0xF7 + ACK
    Setup Read to [0xED] + ACK
    0x80 + ACK
    0x00 + ACK
    0x00 + ACK
    0x80 + ACK
    0x00 + ACK
    0x00 + ACK
    0x80 + ACK
    0x00 + NAK
    

    And weird one is at Write to register F4 [BME280_PWR_CTRL_ADDR] now starting to be even more confused Disappointed

  • I don't know the BME280, therefore, I don't know what you'd expect from this log and what is actually captured by the logical analyzer. 

    "The members that points to the addresses that the TWI peripheral uses for the transactions need to be declared as volatile." Did this solve your original issue? 


  • That was not issue as someone was already on Bosch site talking about it, just moved it inside function to read so will not be global (so will not have issue with compiler and volatile) 

    Problem still persist, getting same values and log that I posted from logic analyzer is specific for line 

    71  Setup Write to [0xEC] + ACK
    72  0xF4 + ACK
    73  Setup Read to [0xED] + ACK
    74  0x00 + NAK

    So writing to F4 returns ACK while reading data returns 0x00 and NAK 

    Will need to connect to Arduino and check if this is happening there also or something else is wrong Disappointed

Reply
  • That was not issue as someone was already on Bosch site talking about it, just moved it inside function to read so will not be global (so will not have issue with compiler and volatile) 

    Problem still persist, getting same values and log that I posted from logic analyzer is specific for line 

    71  Setup Write to [0xEC] + ACK
    72  0xF4 + ACK
    73  Setup Read to [0xED] + ACK
    74  0x00 + NAK

    So writing to F4 returns ACK while reading data returns 0x00 and NAK 

    Will need to connect to Arduino and check if this is happening there also or something else is wrong Disappointed

Children
Related