Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 8609

Troubleshooting • Re: Is my Pimoroni BME280 faulty?

$
0
0
OK,
Using the Pimoroni all-values.py code currently is producing the same default type values again and again. If I stop the code runnig and run this kernel code which was generated by Perplexity AI everything works fine. Stop this code and try all-values.py and again default values.

So I am going to stick with the Perplexity AI code.

Thanks for all the help everyone.

Code:

import timefrom smbus2 import SMBus# BME280 I2C addressBME280_I2C_ADDRESS_76 = 0x76BME280_I2C_ADDRESS_77 = 0x77# Register addressesBME280_REG_CALIB00 = 0x88BME280_REG_CALIB26 = 0xE1BME280_REG_CTRL_HUM = 0xF2BME280_REG_CTRL_MEAS = 0xF4BME280_REG_CONFIG = 0xF5BME280_REG_PRESS_MSB = 0xF7BME280_REG_TEMP_MSB = 0xFABME280_REG_HUM_MSB = 0xFDclass BME280:    def __init__(self, bus_num=1, address=BME280_I2C_ADDRESS_76):        self.bus_num = bus_num        self.address = address        self.bus = SMBus(bus_num)        self._load_calibration_data()        self._configure_sensor()    def _load_calibration_data(self):        # Read first block of calibration data (0x88 to 0xA0, 24 bytes)        calib = self.bus.read_i2c_block_data(self.address, BME280_REG_CALIB00, 24)        # Read dig_H1 from 0xA1 (1 byte)        dig_H1 = self.bus.read_byte_data(self.address, 0xA1)        # Read second block of calibration data (0xE1 to 0xE7, 7 bytes)        calib2 = self.bus.read_i2c_block_data(self.address, BME280_REG_CALIB26, 7)        # Combine all calibration data        calib += [dig_H1] + calib2        # Temperature calibration coefficients        self.dig_T1 = calib[0] | (calib[1] << 8)        self.dig_T2 = self._signed(calib[2] | (calib[3] << 8))        self.dig_T3 = self._signed(calib[4] | (calib[5] << 8))        # Pressure calibration coefficients        self.dig_P1 = calib[6] | (calib[7] << 8)        self.dig_P2 = self._signed(calib[8] | (calib[9] << 8))        self.dig_P3 = self._signed(calib[10] | (calib[11] << 8))        self.dig_P4 = self._signed(calib[12] | (calib[13] << 8))        self.dig_P5 = self._signed(calib[14] | (calib[15] << 8))        self.dig_P6 = self._signed(calib[16] | (calib[17] << 8))        self.dig_P7 = self._signed(calib[18] | (calib[19] << 8))        self.dig_P8 = self._signed(calib[20] | (calib[21] << 8))        self.dig_P9 = self._signed(calib[22] | (calib[23] << 8))        # Humidity calibration coefficients        self.dig_H1 = calib[24]        self.dig_H2 = self._signed(calib[25] | (calib[26] << 8))        self.dig_H3 = calib[27]        self.dig_H4 = self._signed((calib[28] << 4) | (calib[29] & 0x0F))        self.dig_H5 = self._signed((calib[30] << 4) | (calib[29] >> 4))        self.dig_H6 = self._signed8(calib[31])    def _signed(self, val):        # Convert unsigned to signed 16-bit integer        if val & 0x8000:            return -((~val & 0xFFFF) + 1)        else:            return val                def _signed8(self, val):        if val & 0x80:            return -((~val & 0xFF) + 1)        else:            return val    def _configure_sensor(self):        # Set humidity oversampling to x1        self.bus.write_byte_data(self.address, BME280_REG_CTRL_HUM, 0x01)        # Set temperature and pressure oversampling to x1 and mode to normal        self.bus.write_byte_data(self.address, BME280_REG_CTRL_MEAS, 0x27)        # Set config register (standby time 1000 ms, filter off)        self.bus.write_byte_data(self.address, BME280_REG_CONFIG, 0xA0)    def read_raw_data(self):        # Read 8 bytes starting from PRESS_MSB        data = self.bus.read_i2c_block_data(self.address, BME280_REG_PRESS_MSB, 8)        # Pressure raw value (20 bits)        adc_p = (data[0] << 12) | (data[1] << 4) | (data[2] >> 4)        # Temperature raw value (20 bits)        adc_t = (data[3] << 12) | (data[4] << 4) | (data[5] >> 4)        # Humidity raw value (16 bits)        adc_h = (data[6] << 8) | data[7]        return adc_t, adc_p, adc_h    def compensate_temperature(self, adc_t):        var1 = (((adc_t >> 3) - (self.dig_T1 << 1)) * self.dig_T2) >> 11        var2 = (((((adc_t >> 4) - self.dig_T1) * ((adc_t >> 4) - self.dig_T1)) >> 12) * self.dig_T3) >> 14        self.t_fine = var1 + var2        temp = (self.t_fine * 5 + 128) >> 8        return temp / 100.0    def compensate_pressure(self, adc_p):        var1 = self.t_fine - 128000        var2 = var1 * var1 * self.dig_P6        var2 = var2 + ((var1 * self.dig_P5) << 17)        var2 = var2 + (self.dig_P4 << 35)        var1 = ((var1 * var1 * self.dig_P3) >> 8) + ((var1 * self.dig_P2) << 12)        var1 = (((1 << 47) + var1) * self.dig_P1) >> 33        if var1 == 0:            return 0  # avoid division by zero        p = 1048576 - adc_p        p = ((p << 31) - var2) * 3125 // var1        var1 = (self.dig_P9 * (p >> 13) * (p >> 13)) >> 25        var2 = (self.dig_P8 * p) >> 19        p = ((p + var1 + var2) >> 8) + (self.dig_P7 << 4)        return p / 25600.0  # pressure in hPa    def compensate_humidity(self, adc_h):        v_x1_u32r = self.t_fine - 76800        v_x1_u32r = (((((adc_h << 14) - (self.dig_H4 << 20) - (self.dig_H5 * v_x1_u32r)) + 16384) >> 15) *                     (((((((v_x1_u32r * self.dig_H6) >> 10) * (((v_x1_u32r * self.dig_H3) >> 11) + 32768)) >> 10) + 2097152) *                       self.dig_H2 + 8192) >> 14))        v_x1_u32r = v_x1_u32r - (((((v_x1_u32r >> 15) * (v_x1_u32r >> 15)) >> 7) * self.dig_H1) >> 4)        v_x1_u32r = max(v_x1_u32r, 0)        v_x1_u32r = min(v_x1_u32r, 419430400)        humidity = v_x1_u32r >> 12        return humidity / 1024.0  # humidity in %    def read_all(self):        adc_t, adc_p, adc_h = self.read_raw_data()        temperature = self.compensate_temperature(adc_t)        pressure = self.compensate_pressure(adc_p)        humidity = self.compensate_humidity(adc_h)        return temperature, pressure, humidityif __name__ == "__main__":    sensor76 = BME280(address=BME280_I2C_ADDRESS_76)    sensor77 = BME280(address=BME280_I2C_ADDRESS_77)    try:        while True:            temperature, pressure, humidity = sensor76.read_all()            print(f"0x76")            print(f"Temperature: {temperature:.2f} C")            print(f"Pressure: {pressure:.2f} hPa")            print(f"Humidity: {humidity:.2f} %")            print("-----------------------------")            temperature, pressure, humidity = sensor77.read_all()            print(f"0x77")            print(f"Temperature: {temperature:.2f} C")            print(f"Pressure: {pressure:.2f} hPa")            print(f"Humidity: {humidity:.2f} %")            print("-----------------------------")            time.sleep(1)    except KeyboardInterrupt:        print("Measurement stopped by user")            

Statistics: Posted by SniffTheGLove — Sun Jul 06, 2025 7:48 am



Viewing all articles
Browse latest Browse all 8609

Trending Articles