Uncategorized

Rpi4B python I2C AK8963C magnetometer

Asked 
Viewed 8 times
0

While attempting to read data from an ak8963c sensor board over i2c I am only getting 0s as a response except when reading the first 2 registries. I have checked my connections and tried other devices but they all work as expected. I also tried placing a magnet nearby in the hopes interference would be overpowered by said magnet.

Code:

import smbus
import time
CHL = 1
#AK8963 registers
AK8963_ADDR   = 0x0C
AK8963_ST1    = 0x02
HID      = 0x00
HX       = 0x03
HY       = 0x05
HZ       = 0x07
AK8963_ST2   = 0x09
AK8963_CNTL  = 0x0A
mag_sens = 4900.0 # magnetometer sensitivity: 4800 uT

bus = smbus.SMBus(CHL)


print('recording data')

#val = bus.read_i2c_block_data(AK8963_ADDR, HXH, 6)
val = bus.read_byte_data(AK8963_ADDR, HX)
valH = bus.read_byte_data(AK8963_ADDR, HX+1)
#mx = (val[0] << 8) | val[1]
#my = (val[2] << 8) | val[3]
#mz = (val[4] << 8) | val[5]
mx = (valH << 8) | val

val = bus.read_byte_data(AK8963_ADDR, HY)
valH = bus.read_byte_data(AK8963_ADDR, HY+1)
my = (valH << 8) | val

val = bus.read_byte_data(AK8963_ADDR, HZ)
valH = bus.read_byte_data(AK8963_ADDR, HZ+1)
mz = (valH << 8) | val

misc = bus.read_byte_data(AK8963_ADDR, 0x00)



print('{}'.format('-'*50))
#print('mag [uT]:   x = , y = , z = '.format(mx,my,mz))
print('mag [uT]:    x= % 2d, y= % 2d, z= % 2d, misc= % 2d' %(mx, my, mz, misc))
print('{}'.format('-'*50))
 New contributor
  • Hello @Keeper317, Welcome and nice to meet you. Ah, let me see. I would suggest the following troubleshooting steps: (1) Read deviceID read only register WIA addr 0x00 for data 0x48. (2) Power or software reset, then try self test sequence (Section 6.4.5), (3) Reset and do single measurement only, (4) Try SPI mode, which is usually more reliable, / to continue … , – tlfong01 23 mins ago
  • (5) For either I2C or SPI mode, use low speed, eg, 50kHz or even lower (only for Rpi4B, because Rpi3B+ I2C cannot change speed), (6) Use connecting wires shorter than 30cm, and no longer than 1m, (7) Do not overload the I2C bus by too many devices (400pF max), preferably entertain only the VIP magneto guy and no body else, (8) If you must put multiple devices on the same I2C bus, remove the on chip pull up registers (usually SMD 4k7 or 10k). / to continue, … – tlfong01 2 mins ago   Edit
  • (9) Try you luck on other I2C buses, I2C 1, 3, 4, 5, 6) References: “AK8963 3-Axix Magnetometer – AKM”: download.mikroe.com/documents/datasheets/ak8963c-datasheet.pdff. Good luck and cheers. – tlfong01 2 mins ago   Edit
.END

Categories: Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: