I2C/SPI sensor readings not accurate #18261
-
|
I have a sensor from TDK that is ICM-42670-P. The sensor is working ok on I2C bus and it is detected by scan(). There are basically three registers that I need to configure. Namely PWR_MGMT0, ACC_CONFIG0, ACC_CONFIG1 Another issue that I am facing is with working on this sensor over SPI. I am not sure, but I cannot read the ID register. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
|
Your read_accelerometer() mixes different approaches: sometimes you use readfrom, other times readfrom_mem. def read_accelerometer(self):
# Assume _ACCEL_DATA_X1H is the base address for X axis high byte
data = self.i2c.readfrom_mem(_ICM2670P_ADDRESS, _ACCEL_DATA_X1H, 6)
ax = int.from_bytes(data[0:2], "big", signed=True)
ay = int.from_bytes(data[2:4], "big", signed=True)
az = int.from_bytes(data[4:6], "big", signed=True)
print(ax/2048, ay/2048, az/2048)
return (ax, ay, az)Based on : ICM-42670-P datasheet (TDK) Nitpicking: get_temprature --> get_temperature |
Beta Was this translation helpful? Give feedback.
-
|
@Josverl Your answer is right except that MP does not seem to support the |
Beta Was this translation helpful? Give feedback.
-
|
Try to use struct.struct to interpret the binary data. The type should be |
Beta Was this translation helpful? Give feedback.
Consider your 31.91402 value:
The 16 bit value
0xFF50is evidently a negative number in two's complement which the code is interpreting as unsigned. To convert a raw 16 bit two's complement number to a signed integer you want: