Beaglebone Isn't Reading I2C Values from HMC5883L.

82 views
Skip to first unread message

Tomas Medina

unread,
Jan 3, 2018, 9:54:26 PM1/3/18
to BeagleBoard

I am trying to use a Beaglebone Wireless (debian image 2017-07-01) to read values from an HMC5883L Magnetometer.
The device is connected through I2C2 (pins P9_19 and P9_20). The address for the device is 0x68.

I know the device is connected since I can see it by using i2cdetect as shown below:

However, whenever I run my python code to read the values, I get messages that the bone is having errors accessing the adresses.
As with most of my problems with the BBBW, the code works fine on an older bone (debian image 2015-11-12).
My faith in Beagleboard dwindles by the minute.

Here is the python code, first is my main file:
from time import sleep
import os
import hmcCl

sensor
= hmcCl.hmc()
sensor
.setConfig()

count
= 0
while True:

    count
+= 1
    x
= 0
    y
= 0
    z
= 0
    he
= 0
   
    os
.system("clear")

    x
, y, z = sensor.getAvgRead(5)
   
   
print("X: {}     Y: {}     Z: {}     Count: {}".format(x,y,z, count))
   
   
print(sensor.getDirection())
    sleep
(.2)

Next is the object file (hmcCl.py):
from Adafruit_I2C import Adafruit_I2C
import math

class hmc:

   
def __init__(self):
       
self.hmc = Adafruit_I2C(0x1E)
       
self.debug = False
       
self.address = 0x1E
       
       
self.readingList = [0, 0, 0]
       
self.maxList = [0, 0, 0]
       
self.minList = [0, 0, 0]
       
       
self.mpu = Adafruit_I2C(0x68)
       
   
def setConfig(self, mode=0x00):
       
       
self.mpu.write8(0x6B, 0) #MPU6050 Power Management_1 Register
       
self.mpu.write8(0x6A, 0) #MPU6050 USER_CTRL Register: Disables FIFO Buffer
       
self.mpu.write8(0x37, 2) #MPU6050 INT_PIN_CFG Register: Enables I2C Bypass
       
       
self.hmc.write8(0x00, 0x70) #Config Reg A: Frequency of measurement = 15Hz
       
self.hmc.write8(0x01, 0xA0) #Config Reg B: Gain = 5
       
self.hmc.write8(0x02, mode) #Mode Register
   
       
return True
   
   
   
def readMag(self):
        retList
= []
        regList
= [0x03, 0x04, 0x07, 0x08, 0x05, 0x06]
       
       
for i in range(0, len(regList), 2):
           
            msb
= self.hmc.readS8(regList[i])
            lsb
= self.hmc.readS8(regList[i+1])
           
            retVal
= (msb<<8) | lsb
           
            retList
.append(retVal)
       
       
for j in range(0, len(retList)):
           
self.readingList[j] = retList[j]

           
if retList[j] > self.maxList[j]:
               
self.maxList[j] = retList[j]
               
           
if retList[j] < self.minList[j]:
               
self.minList[j] = retList[j]
           
       
return True
       
       
   
def getAvgRead(self, samples):
        xRead
= 0
        yRead
= 0
        zRead
= 0
       
       
for i in range(0, samples):
           
if self.readMag():
                xRead
+= self.readingList[0]
                yRead
+= self.readingList[1]
                zRead
+= self.readingList[2]

           
else:
               
print("Error")
       
        xRead
= xRead / samples
        yRead
= yRead / samples
        zRead
= zRead / samples
       
       
return xRead, yRead, zRead
   
   
def relativeRead(self, axis, reading):
   
       
#Takes the max and min of a given axis and finds 1/8 of that distance than
       
#checks the given number to see if it is within that range of the max or min
       
        range
= (self.maxList[axis] - self.minList[axis]) / 4
   
       
if (reading + range) > self.maxList[axis]:
           
return "High"
       
elif(reading - range) < self.minList[axis]:
           
return "Low"
       
else:
           
return False
   
   
def getDirection(self):
        xMax
= self.maxList[0]
        yMax
= self.maxList[1]
       
       
self.readMag()
       
       
if self.readingList[0] > self.readingList[1]:
           
           
if self.relativeRead(0, self.readingList[0]) == "High":
               
return "North"
           
elif self.relativeRead(1, self.readingList[1]) == "Low":
               
return "East"
               
       
elif self.readingList[1] > self.readingList[0]:
       
           
if self.relativeRead(1, self.readingList[1]) == "High":
               
return "West"
           
elif self.relativeRead(0, self.readingList[0]) == "Low":
               
return "South"
       
       
else:
           
return "N/A"
       



Any help into this matter is greatly appreciated.
Thanks,
Tomas

Auto Generated Inline Image 1

Robert Nelson

unread,
Jan 3, 2018, 10:07:29 PM1/3/18
to Beagle Board, Tomas Medina
busnum?

Regards,

--
Robert Nelson
https://rcn-ee.com/

Tomas Medina

unread,
Jan 3, 2018, 11:13:43 PM1/3/18
to BeagleBoard
I believe it's 2

Graham

unread,
Jan 4, 2018, 9:37:33 AM1/4/18
to BeagleBoard
I suspect your code is looking for it on the wrong bus number.

You did not include your driver code.

Look there.

The part is obviously sitting on Bus 2.

I have seen drivers that default to bus 1, unless overridden.

--- Graham

==

Graham

unread,
Jan 4, 2018, 9:40:08 AM1/4/18
to BeagleBoard

Graham

unread,
Jan 4, 2018, 9:42:58 AM1/4/18
to BeagleBoard
I meant, go look at the AdaFruit I2C code.
I think you need to override the bus number to get it talking to bus 2.

--- Graham

==

Tomas Medina

unread,
Jan 4, 2018, 12:54:30 PM1/4/18
to BeagleBoard
Thanks for your advice!
I added the parameter 2 to lines 7 and 15 and now it's reading.
def __init__(self):
       
self.hmc = Adafruit_I2C(0x1E,2)

       
self.debug = False
       
self.address = 0x1E
       
       
self.readingList = [0, 0, 0]
       
self.maxList = [0, 0, 0]
       
self.minList = [0, 0, 0]

       
       
self.mpu = Adafruit_I2C(0x68,2)
Reply all
Reply to author
Forward
0 new messages