Connecting to BerryGPS-IMUv3

I have a pair of BerryGPS-IMUv3 (10 DOF + GPS) HATs which I want to use with a pair of Raspberry Pi 3B+ and 4B computers, using Raspbian and Stormux (an Arch Linux ARM variant) respectively.

I’m able to talk to the device on the RasPi 3B+, using circuits_i2c, but I’m unable to get useful data from it. Here are some IEx interactions, in which I try to read the compass:

iex(1)> alias Circuits.I2C
Circuits.I2C
iex(2)> {:ok, ref} = I2C.open("i2c-1")
{:ok, #Reference<0.517024855.4042391563.14505>}
iex(3)> I2C.bus_names
["i2c-1"]
iex(4)> I2C.detect_devices  
  Devices on I2C bus "i2c-1":
   * 28
   * 106
   * 119
  3 devices detected on 1 I2C buses

iex(5)> I2C.write(ref, 0x1c, <<0xf0, 0x60, 0x00>>)
iex(6)> I2C.read(ref, 0x1c, 6)
{:ok, <<0, 0, 0, 0, 0, 0>>}
iex(7)> I2C.read(ref, 0x08, 6)
{:error, :i2c_nak}
iex(8)> I2C.read(ref, 0x28, 6)                    
{:error, :i2c_nak}

Because I find the documentation a bit confusing, I’m not sure what address(es) I need to use. More generally, it’s quite possible that I’m sending the wrong commands. Help?

Nerves won’t help you in the near term, but i would highly recommend checking it out for reproducible builds.

I haven’t used those devices before, so unfortunately can’t be any help there. But it is most likely an issue with your commands. Also be sure you try both hats, maybe one of them is foobard.

Here you are reading from the device with address 0x08, which is not among the addresses returned by I2C.detect_devices. Same for the other read, where you use 0x28 (which is address 40 in base 10).

The documentation on the page you linked reports that the I2C addresses should be:

  • 0x1E (30 in base 10) for the magnetometer and accelerometer
  • 0x6A (106 in base 10) for the gyroscope
  • 0x77 (119 in base 10) for the pressure sensor

Or, on version 3 (which seems to be yours):

  • 0x6A (106 in base 10) for the gyroscope and accelerometer
  • 0x1C (28 in base 10) for the magnetometer
  • 0x77 (119 in base 10) for the pressure sensor

The specifics of the protocol to read the magnetometer data over I2C is explained here, while for the accelerometer/gyro data explained here (and the C code seems to refer to this source code).

You’ll have to translate the C code in the guide to Elixir, but with some patience it looks feasible, as the guide explains it step by step. The actual addresses and registers can be found in one of these two header files, depending on the specific chip in use on your board: LSM9DS0.h and LSM9DS1.h. It looks like your board mounts the LSM9DS1.

At a glance, in order to read the compass (magnetometer) you’ll have to:

  • Open the I2C bus and use address 0x1C for the next steps
  • Set a few of registers to enable the magnetometer:
    • register CTRL_REG5_XM to 0b11110000
    • register CTRL_REG6_XM to 0b01100000
    • register CTRL_REG7_XM to 0b00000000
  • Read a block of data (6 bytes it seems) starting from OUT_X_L_M as shown in the guide
  • Decode the data

Once you get the basic working, if you get stuck on something I am sure people here will be able to help.

Note: I made a few edits to this reply, as I was finding out more and more about it.

2 Likes