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 Reply Children
No Data
Related