HMC6352 Compass module with LPCXpresso Base Board

This project demonstrates how to use the digital compass HMC6352 with the LPC1343 LPCXpresso Board and the LPCXpresso Base Board.


Compass heading on OLED display
Figure 1 - Compass heading on OLED display

 

Required Parts

Description

The first thing that needs to be done is to solder four wires to the compass module and then connect them to the expansion dual row header connector on the LPCXpresso Base Board, see Figure 2.

SDA on the module should be plugged in to PIO0_5-SDA on the base board and SCL on the module should be plugged in to PIO0_4-SCL on the base board. The compass module uses I2C communication protocol and that's why the SDA/SCL pins are used. VCC for HMC6352 is Min. 2.7 V, Max. 5.2 V and Typ. 3.0 V.


Compass module connected to LPCXpresso Base Board
Figure 2 - HMC6352 module connected to LPCXpresso Base Board

Source code

In the main function timers needs to be initiated to use delays, the I2C controller needs to be initiated in order to read and write to the compass module. SSP (SPI) needs to be initialized to use the OLED screen.

The compass layout, that is, the directions (N, W, S, and E), is drawn on the display. After that the heading value is read from the compass and then shown on the display.

  int main(void)
  {
    init_timer32(0, 10);

    I2CInit( (uint32_t)I2CMASTER, 0 );
    SSPInit();

    oled_init();
    oled_clearScreen(OLED_COLOR_WHITE);
   
    // Draw the compass layout 
    drawCompassLayout();
   
    // Read and display value from the compass
    readFromCompassHMC6352();

    return 1;
  }
                    

The code below show how to fetch the compass heading from the HMC6352 module.

  void readFromCompassHMC6352(void)
  {
    int value = -1;
    uint8_t data[2]={0,0};

    while(1) {
      // Send a "Get Data" command to the compass
      data[0]= COMMAND_GET_DATA;
      I2CWrite(READ_WRITE_OPERATION,data,1);
     
      // datasheet suggests to wait at least 6000 microseconds
      delay32Ms(0, 10);
     
      // Read two bytes from the compass (compass heading)
      data[0] = 0;
      I2CRead(READ_WRITE_OPERATION, data, 2);
     
      value = ((data[0] << 8)+data[1]) / 10;

      // read compass twice to clear last reading
      delay32Ms(0, 10);
      data[0] = 0;
      I2CRead(READ_WRITE_OPERATION,data, 2);

      //Display the value on the display
      if(value >= 0 && value <= 359) {
        intToString(value, buf, 10, 10);
        oled_fillRect(0,0, 30, 8, OLED_COLOR_WHITE);
        oled_putString(0,0, buf, OLED_COLOR_BLACK, OLED_COLOR_WHITE);
        drawArrow(value);
      }
     
     delay32Ms(0, 500);
    }
  }
                    

Import project into LPCXpresso IDE

Begin by importing the driver and sample application bundle (zip file) for the LPC1343 LPCXpresso Board found at the Embedded Artists support site.

  1. In the LPCXpresso IDE, Quickstart tab, choose Import Example project(s)
  2. Browse to the downloaded zip file (for example lpc1343_xpr_bb_100222.zip)
  3. Click finish to import the drivers and sample applications.

The second step is to import Compass module project. Do the same as when importing the drivers, but choose HMC6352_compass_example.zip in step 2. To build or debug the example just click build/debug in the QuickStart tab

More information on how to work with the LPCXpresso Base Board as well as the LPCXpresso IDE can be found in the User's Manual for the LPCXpresso Base Board. This User's Manual can be found at the Embedded Artists support site.


Base Board overview
Figure 3 - Base Board overview with Compass module