Notes About Working with Various Arduino & Netduino Microcontroller Boards

Tuesday, December 4, 2012

Using a HMC6352 Compass with a Netduino Plus 2

I posted an example of how to use an HMC6352 (Sparkfun breakout) with a Netduino Plus (v. 1.0). Here is an updated and improved version of that example for the Netduino Plus 2.  


Connections:
SDA to SDA (no pull-up resistor)
SCL to SCL (no pull-up resistor) 
VCC to 5V
GND to GND

This code requires the updated firmware for the Netduino Plus 2 to fix problems with I2C. This is a fairly easy update and only requires a few minutes.

Code:
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;

namespace Compass2
{
    public class Program
    {
        public static void Main()
        {
            byte HMC6352Addr = 0x42 >> 1;
            byte HMC6352ReadCmd = 0x41; //"A"
            I2CDevice compass = new I2CDevice(new I2CDevice.Configuration(HMC6352Addr, 100));
            byte[] compassCmd = { HMC6352ReadCmd };
            byte[] compassData = { 0, 0 };
            I2CDevice.I2CTransaction[] reading = new I2CDevice.I2CTransaction[] {
                I2CDevice.CreateWriteTransaction(compassCmd),
                I2CDevice.CreateReadTransaction(compassData)
            };
            int bytesRead = compass.Execute(reading, 100);
            float headingSum = (compassData[0] << 8) + compassData[1];
            float heading = headingSum / 10;
            Debug.Print(heading.ToString());
        }
    }
}



1 comment:

  1. I found your post such a informative and useful post,thanks for sharing the post..
    Car hire Harpenden
    Van hire luton airport

    ReplyDelete