I am setting up a Raspberry Pi to record data (CO2, humidity, and temperature) from the Sensirion SCD30 sensor. My code is in Python 3, using the SMBus library to communicate with the sensor over the I²C pins in the Raspberry Pi's GPIO. There is a command to determine whether the sensor is ready to send data.
Link to SCD30 interface datasheet
Link to SCD30 library for Arduino by Sparkfun
The value 0x0202
is sent over I²C, and three bytes of data are returned:
0x00 0x00 0x81 for data not ready
0x00 0x01 0xB0 for data ready
The first two bytes are the MSB and the LSB of the data ready value. Combined properly, they should be 0x0000
and 0x0001
.
The third byte is the CRC8 of the first two bytes. This is calculated with a polynomial of 0x31
and an initialization of 0xFF
.
About half the time, the bytes are sent in the wrong order. Instead of MSB LSB CRC
it is sent MSB CRC LSB
. For example, if the data is ready it might send 0x00, 0xB0, 0x01
instead of 0x00, 0x01, 0xB0
. I can't figure out why this is happening, and I'm concerned there is some corruption or issues in sending data. I could change the code to recognize if the CRC is the second byte, but I would like to find the underlying problem.
I am sending and receiving I²C data using the smbus
library. This is my code to send commands and read data:
bus = smbus.SMBus(0)
I2C_ADDRESS = 0x61
def sendCommand(self, cmd): # Sends a 2 byte command (cmd)
data = [0]*2
data[0] = cmd >> 8 # Splits 2 byte command into MSB and LSB
data[1] = cmd & 0xFF
bus.write_i2c_block_data(I2C_ADDRESS, data[0], data[1:])
def readRegister(self, reg, length): # Sends 2 byte command (reg) and receives (length) bytes
sendCommand(reg)
data = bus.read_i2c_block_data(I2C_ADDRESS, 0, length)
return data
For the example I gave above, I would run the following code:
ready = readRegister(0x0202, 3) # Returns a list of 3 bytes
print(ready)
And it would return a list of the three bytes demonstrated above.