I have an Arm64 Linux board and an IST8308 3D magnetometer that I am reading via I2C with the following script:
import smbus2
i2c_bus = smbus2.SMBus(2)
i2c_magnetometer_register = 12
# initialize magnetometer
i2c_bus.write_byte_data(i2c_magnetometer_register, 32, 0) # action register
i2c_bus.write_byte_data(i2c_magnetometer_register, 50, 1) # control register 3
i2c_bus.write_byte_data(i2c_magnetometer_register, 49, 4) # control register 2
# read values
while True:
dataxl = i2c_bus.read_byte_data(i2c_magnetometer_register, 17)
dataxh = i2c_bus.read_byte_data(i2c_magnetometer_register, 18)
datax = uint_to_int((dataxh << 8) + dataxl)
datayl = i2c_bus.read_byte_data(i2c_magnetometer_register, 19)
datayh = i2c_bus.read_byte_data(i2c_magnetometer_register, 20)
datay = uint_to_int((datayh << 8) + datayl)
datazl = i2c_bus.read_byte_data(i2c_magnetometer_register, 21)
datazh = i2c_bus.read_byte_data(i2c_magnetometer_register, 22)
dataz = uint_to_int((datazh << 8) + datazl)
# do stuff...
According to my profiler, the read_byte_data()
function is very slow compared to the declared 200Hz. I am pretty new to sensors and I am not sure if I can change some setting in the sensor in order to read faster.
I referred to this code to find how to read from the sensor.
I could not find the full datasheet of the IST8308 online but I found the IST8310 one.
UPDATE: according to the github code linked above, the sensor provides a status register to check for the availability of new data (DRDY - data ready). I added this check in my loop and it seems that there is always new data available. For this reason I think that the bottleneck is the Python builtin ioctl()
function, that is called by read_byte_data()
. However, I am not sure about it.