Arduino Digital Compass

See the previous entry for details on hooking up and talking to the LCD.   This adds an HMC6352 heading sensor, and writes heading on the LCD (which was no small task, at least for me). 

Hooking up the compass:
VCC and GND go right to the bus on my breadboard (coming from the Arduino)
SDA -> Analog 4
SCL -> Analog 5

I read a lot about pull up resistors being required, but found someone’s sample where that was not done, so tried it just like specified above, and it worked.   We don’t need no stinkin’ resistors.  

It took me a lot of hacking to get the format of the heading number correctly left zero padded.   I’m sure there’s a better way than what I’ve got here, but this is the best I could do.   I’d kill for a String.format(); 

And the code:

   1:  // -- For the LCD (Though the newer LiquidCrystal library is much easier to use, IMHO)
   2:  #include  <LCD4Bit.h> 
   3:   
   4:  //create object to control an LCD.  
   5:  //number of lines in display=1
   6:  LCD4Bit lcd = LCD4Bit(2); 
   7:   
   8:  // -- For the Compass
   9:  #include <Wire.h>
  10:  int HMC6352Address = 0x42;
  11:   
  12:  // This is calculated in the setup() function
  13:  int slaveAddress;
  14:  int i, headingValue;
  15:   
  16:  void setup() { 
  17:   
  18:    lcd.init();
  19:    lcd.clear();
  20:    writeLcd("Initializing...", 2, 0, true);
  21:   
  22:    // set up the compass 
  23:    slaveAddress = HMC6352Address >> 1;   // This results in 0x21 as the address to pass to TWI
  24:   
  25:    Wire.begin();
  26:    
  27:    writeLcd("Reading...", 2, 0, true); 
  28:    writeLcd("Hdg: ", 1, 0, false);
  29:  }
  30:   
  31:  // Main loop
  32:  void loop() {  
  33:     updateDisplay();
  34:  }
  35:   
  36:  // write to the LCD at a defined position
  37:  void writeLcd(char data[], int line, int pos, bool clear)
  38:  {
  39:    // clear if desired
  40:    if(clear == true)
  41:    {
  42:      lcd.clear();
  43:    }
  44:   
  45:    lcd.cursorTo(line, pos);
  46:    lcd.printIn(data);
  47:    delay(10);
  48:  }
  49:   
  50:  // just keep going wherever the cursor was
  51:  void appendLcd(char data[])
  52:  {
  53:    lcd.printIn(data);
  54:  }
  55:   
  56:  void updateDisplay()
  57:  {
  58:    byte headingData[2];
  59:    
  60:    // ask the compass for a heading
  61:    Wire.beginTransmission(slaveAddress);
  62:    Wire.send("A");              // The "Get Data" command
  63:    Wire.endTransmission();
  64:    delay(10);             
  65:    
  66:    // The HMC6352 needs at least a 70us (microsecond) delay
  67:    // after this command.  Using 10ms just makes it safe
  68:    // Read the 2 heading bytes, MSB first
  69:    // The resulting 16bit word is the compass heading in 10th's of a degree
  70:    // For example: a heading of 1345 would be 134.5 degrees
  71:    
  72:    Wire.requestFrom(slaveAddress, 2);        // Request the 2 byte heading (MSB comes first)
  73:    i = 0;  
  74:    while(Wire.available() && i < 2)
  75:    { 
  76:      headingData[i] = Wire.receive();
  77:      i++;
  78:    }
  79:     
  80:    headingValue =  headingData[0]*256 + headingData[1];  // Put the MSB and LSB together
  81:        
  82:    int firstPart = headingValue / 10;
  83:    int secondPart = headingValue % 10;
  84:    char buffer[4];
  85:    
  86:    // first write to the LCD.  Set the cursor
  87:    lcd.cursorTo(1, 5); 
  88:   
  89:    // brute force zero padding 
  90:    if(headingValue <= 999)
  91:    {
  92:      appendLcd("0"); // pad with a leading zero
  93:    }
  94:    
  95:    if(headingValue <= 99)
  96:    {
  97:      appendLcd("0");
  98:    }
  99:    
 100:    appendLcd(itoa((firstPart), buffer, 10));  // convert the int to a string and write the degrees
 101:    appendLcd(".");    // decimal point
 102:    appendLcd(itoa((secondPart), buffer, 10));  // and the tenths
 103:   
 104:   delay (750);
 105:  }

Arduino_digitalCompass.wmv (12.66 mb)

 

Leave a Reply

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