Build a Custom Arduino Clone (Part 3) – Add a Compass

This post is the last part of this series of articles showing how to build a project using a custom Arduino board. To the accelerometer I added a compass. It begins to look more like a forest than an electronic circuit.

Arduino custom board, accelerometer and compass

Arduino custom board, accelerometer and compass

The compass uses the IC2 protocol to communicate with the Arduino micro-controller. That make it more complicated to interface with than the accelerometer. This protocol is handled by the library Wire.h that comes standard with the Arduino API.

In this example, you connect the SCL pin of the compass to the analog input #5 (pin #28) and the SDA pin to the analog input #4 (pin #27) of the micro-controller. As with the accelerometer, the compass is powered with the 3.3v output of the micro-controller on pin #20.

#include 

int x, y, z;
int pin13 = LOW;

int slaveAddress;
byte headingData[2];
int i, headingValue;
int HMC6352Address = 0x42;

void setup()
{
  Wire.begin();
  slaveAddress = HMC6352Address >> 1;  

  pinMode(13, OUTPUT);
  Serial.begin(19200);
}
void loop()
{
  // Send a "A" command to the HMC6352
  Wire.beginTransmission(slaveAddress);
  Wire.send("A");  // The "Get Data" command
  Wire.endTransmission();
  delay(100);

  i = 0;
  Wire.requestFrom(slaveAddress, 2);        // Request the 2 byte heading (MSB comes first)
  while(Wire.available() && i < 2)
  {
    headingData[i] = Wire.receive();
    i++;
  }

  headingValue = headingData[0]*256 + headingData[1];  // Put the MSB and LSB together

  Serial.print("Current heading: ");
  Serial.print(int (headingValue / 10));     // The whole number part of the heading
  Serial.print(".");
  Serial.print(int (headingValue % 10));     // The fractional part of the heading
  Serial.println(" degrees");

  x = analogRead(0);
  y = analogRead(1);
  z = analogRead(2);

  Serial.print("acceleretations are x, y, z: ");
  Serial.print(x, DEC);
  Serial.print(" ");
  Serial.print(y, DEC);
  Serial.print(" ");
  Serial.println(z, DEC);

  //pin13 = !pin13;
  //digitalWrite(13,pin13);

  delay(400);
}

The output will refresh twice a seccond and display the compass heading and the accelerometer three axis values.

Serial port output displaying the compass heading and the accelerometer three axis values

Serial port output displaying the compass heading and the accelerometer three axis values

I had a problem of interference between the compass and the small green LED. When the LED is turned on, the compass reading is off by up to 10 degrees. I can’t explain that one but, when the LED is disables in the code, the compass readings is back to normal and stable. If you have an explanation for that please let me know in the comment section below.

 

Tags: , , , ,

2 Replies

Trackback  •  Comments RSS

  1. Dimitris says:

    I want to make an accelerometer servo controller can you send me a circuit?
    thanks

Post a Reply

Your email address will not be published. Required fields are marked *

Top