Beginner
10 min

Enjoy in electronic compass functionality with LSM303DLHC and PIC32MZ2048EFM100

Determine which direction you're facing by detecting the earth's magnetic field

Compass Click with Curiosity PIC32 MZ EF

Published Jan 31, 2024

Click board™

Compass Click

Dev. board

Curiosity PIC32 MZ EF

Compiler

NECTO Studio

MCU

PIC32MZ2048EFM100

Unlock your project's navigation potential, transforming data into a reliable compass for precise orientation

A

A

Hardware Overview

How does it work?

Compass Click is based on the LSM303DLHC, an ultra-compact, high-performance e-compass module featuring a 3D digital linear acceleration sensor and a 3D digital magnetic sensor from STMicroelectronics. The LSM303DLHC is manufactured using specialized micromachining processes and includes specific sensing elements capable of measuring both the linear acceleration and magnetic field, thus providing a 16-bit data signal to the host MCU through an I2C serial interface. It has linear acceleration full scales of ±2g/±4g/±8g/±16g and a magnetic field full scale of ±1.3/±1.9/±2.5/±4.0/±4.7/±5.6/±8.1 gauss, fully

selectable by the user. The LSM303DLHC provides two different acceleration operating modes, respectively reported as “Normal mode” and “Low-power mode”. While normal mode guarantees high resolution, low-power mode further reduces the current consumption. Besides, magnetic and accelerometer parts can be enabled or put into Power-Down mode separately. Compass Click communicates with MCU using the standard I2C 2-Wire interface to read data and configure settings with a maximum clock frequency of 400kHz. It also features a data-ready signal, routed to the RST pin on the mikroBUS™ socket, which indicates when a

new set of measured acceleration and magnetic data are available, simplifying data synchronization in the digital system that uses the device. The LSM303DLHC may also be configured to generate a free-fall interrupt signal according to a programmed acceleration event along the enabled axes. This Click board™ can only be operated with a 3.3V logic voltage level. The board must perform appropriate logic voltage level conversion before using MCUs with different logic levels. However, the Click board™ comes equipped with a library containing functions and an example code that can be used as a reference for further development.

Compass Click hardware overview image

Features overview

Development board

Curiosity PIC32 MZ EF development board is a fully integrated 32-bit development platform featuring the high-performance PIC32MZ EF Series (PIC32MZ2048EFM) that has a 2MB Flash, 512KB RAM, integrated FPU, Crypto accelerator, and excellent connectivity options. It includes an integrated programmer and debugger, requiring no additional hardware. Users can expand

functionality through MIKROE mikroBUS™ Click™ adapter boards, add Ethernet connectivity with the Microchip PHY daughter board, add WiFi connectivity capability using the Microchip expansions boards, and add audio input and output capability with Microchip audio daughter boards. These boards are fully integrated into PIC32’s powerful software framework, MPLAB Harmony,

which provides a flexible and modular interface to application development a rich set of inter-operable software stacks (TCP-IP, USB), and easy-to-use features. The Curiosity PIC32 MZ EF development board offers expansion capabilities making it an excellent choice for a rapid prototyping board in Connectivity, IOT, and general-purpose applications.

Curiosity PIC32MZ EF double side image

Microcontroller Overview

MCU Card / MCU

default

Architecture

PIC32

MCU Memory (KB)

2048

Silicon Vendor

Microchip

Pin count

100

RAM (Bytes)

524288

Used MCU Pins

mikroBUS™ mapper

NC
NC
AN
Data Ready
RA9
RST
NC
NC
CS
NC
NC
SCK
NC
NC
MISO
NC
NC
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
NC
NC
PWM
Interrupt
RF13
INT
NC
NC
TX
NC
NC
RX
I2C Clock
RPA14
SCL
I2C Data
RPA15
SDA
NC
NC
5V
Ground
GND
GND
1

Take a closer look

Click board™ Schematic

Compass Click Schematic schematic

Step by step

Project assembly

Curiosity PIC32MZ EF front image hardware assembly

Start by selecting your development board and Click board™. Begin with the Curiosity PIC32 MZ EF as your development board.

Curiosity PIC32MZ EF front image hardware assembly
Thermo 28 Click front image hardware assembly
Prog-cut hardware assembly
Curiosity PIC32 MZ EF MB 1 - upright/background hardware assembly
Necto image step 2 hardware assembly
Necto image step 3 hardware assembly
Necto image step 4 hardware assembly
Necto image step 5 hardware assembly
Necto image step 6 hardware assembly
Curiosity PIC32 MZ EF MCU Step hardware assembly
Necto No Display image step 8 hardware assembly
Necto image step 9 hardware assembly
Necto image step 10 hardware assembly
Debug Image Necto Step hardware assembly

Track your results in real time

Application Output

1. Application Output - In Debug mode, the 'Application Output' window enables real-time data monitoring, offering direct insight into execution results. Ensure proper data display by configuring the environment correctly using the provided tutorial.

2. UART Terminal - Use the UART Terminal to monitor data transmission via a USB to UART converter, allowing direct communication between the Click board™ and your development system. Configure the baud rate and other serial settings according to your project's requirements to ensure proper functionality. For step-by-step setup instructions, refer to the provided tutorial.

3. Plot Output - The Plot feature offers a powerful way to visualize real-time sensor data, enabling trend analysis, debugging, and comparison of multiple data points. To set it up correctly, follow the provided tutorial, which includes a step-by-step example of using the Plot feature to display Click board™ readings. To use the Plot feature in your code, use the function: plot(*insert_graph_name*, variable_name);. This is a general format, and it is up to the user to replace 'insert_graph_name' with the actual graph name and 'variable_name' with the parameter to be displayed.

Software Support

Library Description

This library contains API for Compass Click driver.

Key functions:

  • compass_read_magnet_axis - This function reads data for megnetic axes

  • compass_read_accel_axis - This function reads data for accelerometer axes

  • compass_magnet_generic_write - This function writes magnet data to the desired register

Open Source

Code example

The complete application code and a ready-to-use project are available through the NECTO Studio Package Manager for direct installation in the NECTO Studio. The application code can also be found on the MIKROE GitHub account.

/*!
 * \file 
 * \brief Compass Click example
 * 
 * # Description
 * This application measures magnetic and accelerometer axes data and shows them over USBUART
 *
 * The demo application is composed of two sections :
 * 
 * ## Application Init 
 * Initialization driver init and init chip
 * 
 * ## Application Task  
 * Read magnet axis data and accel axis data and logs data on USBUART every 1 sec.
 * 
 * \author MikroE Team
 *
 */
// ------------------------------------------------------------------- INCLUDES

#include "board.h"
#include "log.h"
#include "compass.h"

// ------------------------------------------------------------------ VARIABLES

static compass_t compass;
static log_t logger;

// ------------------------------------------------------ APPLICATION FUNCTIONS

void application_init ( void )
{
    log_cfg_t log_cfg;
    compass_cfg_t cfg;

    /** 
     * Logger initialization.
     * Default baud rate: 115200
     * Default log level: LOG_LEVEL_DEBUG
     * @note If USB_UART_RX and USB_UART_TX 
     * are defined as HAL_PIN_NC, you will 
     * need to define them manually for log to work. 
     * See @b LOG_MAP_USB_UART macro definition for detailed explanation.
     */
    LOG_MAP_USB_UART( log_cfg );
    log_init( &logger, &log_cfg );
    log_info( &logger, "---- Application Init ----" );

    //  Click initialization.

    compass_cfg_setup( &cfg );
    COMPASS_MAP_MIKROBUS( cfg, MIKROBUS_1 );
    compass_init( &compass, &cfg );
    compass_default_config( &compass );
}

void application_task ( void )
{
    int16_t accel_axis[ 3 ];
    int16_t magnet_axis[ 3 ];
    
    compass_read_magnet_axis( &compass, &magnet_axis[ 0 ], &magnet_axis[ 1 ], &magnet_axis[ 2 ] );
    log_printf( &logger, "Magnet axis -- X: %d Y: %d  Z: %d \r\n", magnet_axis[ 0 ], magnet_axis[ 1 ], magnet_axis[ 2 ] );
  
    compass_read_accel_axis ( &compass, &accel_axis[ 0 ], &accel_axis[ 1 ], &accel_axis[ 2 ] );
    log_printf( &logger, "Magnet axis -- X: %d Y: %d  Z: %d \r\n", accel_axis[ 0 ], accel_axis[ 1 ], accel_axis[ 2 ] );
   
    log_printf( &logger, " \r\n");
    Delay_ms ( 1000 );
}

int main ( void ) 
{
    /* Do not remove this line or clock might not be set correctly. */
    #ifdef PREINIT_SUPPORTED
    preinit();
    #endif
    
    application_init( );
    
    for ( ; ; ) 
    {
        application_task( );
    }

    return 0;
}


// ------------------------------------------------------------------------ END

Additional Support

Resources

Love this project?

'Buy This Kit' button takes you directly to the shopping cart where you can easily add or remove products.