Rich_Morin

Rich_Morin

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?

Most Liked

lucaong

lucaong

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.

Where Next?

Popular in Questions Top

siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42920 311
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36128 110
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement