MCP3428 I2C Data Errors



  • I'm using an MCP3428 for ADC on the I2C bus with an address of 104. I'm able to get data from the sensor, but sometimes the format seems to be corrupted. After sending
    i2c.readfrom(104, 3)
    the device should return 3 bytes: MSB, LSB, and configuration byte. Sometimes it returns three bytes correctly:
    b'\x00\x00\x10'
    If I change the configuration byte from 0x10 to 0xD0, the i2c.readfrom(104, 3) command yeilds:
    b'\x00\x00P'

    How is it possible to have "P" appear in a hexadecimal number? It'll send other impossible hexadecimal values, too. Are the binary bytes getting translated incorrectly?



  • Great! Thanks for the explanation. It all makes sense now!



  • @jmpratt Nothing is wrong. What you see is just the way print() display byte strings. If a byte is printable, it is shown as ASCII character. So 'P' is just 0x50. If you want to see all bytes as hex, you have to convert it, e.g. with ubinascii.hexlify(). That is standard Python behavior.



  • It's not an I2C trouble, but it a display misunderstand.
    It's because 'P' as ascii code 0x50 and the print methodes show ascii when it can.
    To have always an hexadecimal display use ubinascii.hexlify(i2c.readfrom(104, 3))


Log in to reply
 

Pycom on Twitter