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))
3
  • 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
    Mar 6, 2020 at 3:00
  • (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
    Mar 6, 2020 at 3:21
  • (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.pdf. Good luck and cheers.
    – tlfong01
    Mar 6, 2020 at 3:21

1 Answer 1

0

Keeper317, Since this a hardware-software interface issue, having just the code is limiting. Reading the data sheet, there are a couple of inconsistencies in your code. First, You must check for a ‘data ready’ before reading the values. This can be accomplished by polling either the data ready pin, or reading the data ready register. After reading the value, you must read the ST2 register to signal your done with the current read. Here’s a snippet from the data sheet:

6. 4.3.2. NormalReadSequence (1) Check Data Ready or not by any of the following method. - Polling DRDY bit of ST1 register - Monitor DRDY pin When Data Ready, proceed to the next step. (2) Read ST1 register (not needed when polling ST1) DRDY: Shows Data Ready or not. Not when “0”, Data Ready when “1”. DOR: Shows if any data has been skipped before the current data or not. There are no skipped data when “0”, there are skipped data when “1”. (3) Read measurement data When any of measurement data register (HXL ~ HZH) or ST2 register is read, AK8963 judges that data reading is started. When data reading is started, DRDY bit and DOR bit turnes to “0”. (4) ReadST2register(required) HOFL: Shows if magnetic sensor is overflown or not. “0” means not overflown, “1” means overflown. When ST2 register is read, AK8963 judges that data reading is finished. Stored measurement data is protected during data reading and data is not updated

Read the full data sheet here AK8963 Data Sheet

1
  • I am kinda new at working with chips in this way, especially with a Pi. That being said, based on you answer I jumped into the datasheet and found a "self-test Sequence" in section 6.4.5 and followed its instructions which returned the same 0s I was getting before and never changed the DRDY status. Could this be signs of a dead chip?
    – Keeper317
    Mar 7, 2020 at 6:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.