Gas station without pumps

2012 January 30

Magnetometer and accelerometer read simultaneously

In Learning to Use I2C and Magnetometer not fried, I talked about interfacing the MAG3110 magnetometer and MQA8452Q accelerometer to an Arduino.  For both, I’m using breakout boards from Sparkfun Electronics.

I  checked today that there are no problems when I connect both devices to the same I2C bus.

The first test was very simple: I put both the breakout boards into a breadboard and wired them together, then tried running each of the programs I’d written for the chips separately. Result: no problems—worked first time.

I then tried merging the programs (cleaning up any naming conflicts) so that both could be run from the same code.  After a few typo fixes, this also worked fine

I think I’m now ready to hand over the software to the students to use for their robot.

I still need to put the i2c.h, i2c.cpp, and accel_magnet code in some public place for others to use (perhaps on github? maybe on my web pages at work?) [UPDATE 2012-jan-31: I have put the libraries and the sample code for the accelerometer and magnetometer at http://users.soe.ucsc.edu/~karplus/Arduino/]

One thing that is still missing is doing tilt correction for the compass heading.  Since the ROV is not expected to remain level (the accelerometer is intended to be used in a feedback loop to adjust the pitch, with anything from -90° to +90° being reasonable), getting a good compass heading requires rotating the magnetometer readings into the horizontal plane.  Only one of the students in the robotics club has had trigonometry or matrix math, so I’ll have to work with him to get him to figure out how to do the tilt correction. It may be simplest conceptually  to compute pitch and roll angles first, then rotate twice, rather than trying to do the whole tilt correction in one step (especially since the Arduino does not have matrix libraries).

About these ads

7 Comments »

  1. Just saying hi and thanks for the code, it worked like a charm with my MAG3110. The code provided by Sparkfun was pretty frustrating at first, without knowing whether I had fried my chip or not (although I knew the chip is 3.3V, I’m not using any level shifter for the SDA/SCL lines)

    Comment by juampe — 2012 February 2 @ 06:59 | Reply

    • Actually, the code you downloaded had a bug: when switching between RAW and non-RAW modes on the magnetometer, I forgot to discard the read already in the registers on the chip. This messed up the calibration, because the first read of the calibration was not RAW.

      The code on the website now does that correctly.

      Comment by gasstationwithoutpumps — 2012 February 3 @ 22:03 | Reply

  2. [...] I had bought the robotics club an I2C accelerometer and magnetometer, I decided to make a new PC board for them to mount the accelerometer, the magnetometer, and the [...]

    Pingback by Sensor board for underwater ROV « Gas station without pumps — 2012 March 1 @ 09:46 | Reply

  3. [...] after the July 4 fire). But there is no reason to stop there.  I have multimeters and a MAG3110 magnetometer, and we’ve previously written code to re-center the magnetometer readings, so we can actually [...]

    Pingback by Chapter 17 done, on to magnetic fields « Gas station without pumps — 2012 November 14 @ 20:53 | Reply

  4. [...] Magnetometer and accelerometer read simultaneously [...]

    Pingback by 2012 in review « Gas station without pumps — 2012 December 31 @ 11:18 | Reply

  5. I cant get this code to work. I get an error saying “i2cReadRegister was not declared in this scope”. Am I just totally stupid and missing something? Also what does this sketch do? Measure the earths magnetic pole?

    Comment by Marcus — 2013 March 3 @ 05:12 | Reply

    • I’ve not looked at the code in a while, but it sounds like the i2c.h file is not being read. You need to install the i2c.h and i2c.cpp library files. On my Mac that meant putting the i2c directory (from http://users.soe.ucsc.edu/~karplus/Arduino/ ) into /Library/Arduino/libraries/ but you’ll need to look at the Arduino documentation to figure out where the libraries go on your machine.

      The sketch was for testing the magnetometer and making it work (crudely) as a compass. My son and I wrote some better code later on. I’ll see if we can find it and release it also.

      Comment by gasstationwithoutpumps — 2013 March 3 @ 09:43 | Reply


RSS feed for comments on this post. TrackBack URI

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

The Rubric Theme. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 284 other followers

%d bloggers like this: