I²C Communication · Learning
English translation
Execution conditions
Basic exercise: provided bytes and synthetic sensor data. Real I²C example: requires a Linux I²C device and smbus2 (python -m pip install smbus2).
Topics covered
The same two bytes can represent very different physical quantities depending on the sign and sensor range settings. Verify the conversion starting with boundary values.
Overall workflow
Register bytes → signed integer → sensitivity factor → physical quantity and deviation
From bytes to signed integers
If u=(high<<8)|low, formed by combining the two bytes, is at least 32768, subtract 65536 to interpret it as a signed 16-bit value. 0x8000 is −32768. Testing boundary values first helps you find conversion errors quickly.
From integers to physical quantities
The sensitivity factors for acceleration and angular velocity depend on the sensor range settings. Saving only raw values makes it difficult to recover units later, so record the settings and measurement time together.
Also distinguish mean deviation from fluctuations at rest.
Worked example
The byte pairs are (0x40,0x00),(0xC0,0x00),(0x7F,0xFF),(0x80,0x00),(0xFF,0xFF). The first byte is the high byte.
pairs=[(0x40,0),(0xC0,0),(0x7F,0xFF),(0x80,0),(0xFF,0xFF)]
def signed16(high,low):
value=(high<<8)|low
return value-65536 if value>=32768 else value
print([signed16(*p) for p in pairs])
[16384, -16384, 32767, -32768, -1]
Reference example
View more examples