Intermediate
30 min

Measure vibration or acceleration of motion using BMA400 and PIC18F46K42

Unlocking motion's hidden potential

Accel 5 Click with EasyPIC v8

Published Oct 05, 2023

Click board™

Accel 5 Click

Dev Board

EasyPIC v8

Compiler

NECTO Studio

MCU

PIC18F46K42

Easily measure acceleration forces and enable the monitoring of motion and changes in velocity

A

A

Hardware Overview

How does it work?

Accel 5 Click is based on the BMA400, an ultra-low power triaxial accelerometer sensor, from Bosch Sensortec. This sensor has many features perfectly suited for IoT applications and wearables, offering a good balance between the performance and the power consumption. One of its key features is its ultra-low power consumption, allowing it to be used in various always-on low power applications. To improve the battery life even more, this sensor also features a Sleep mode when the sensor current consumption is in magnitude of few hundred nanoamperes. This sensor can measure the acceleration in ranges of ±2 g, ±4 g, ±8, and ±16 g. It also offers lowpass filtering of the output data, in the range from 0.48 x ODR (Output Data Refresh rate), up to maximal ODR frequency of 800Hz. An internal 12bit A/D converter ensures reliable and low noise operation, so that the data coming from the internal MEMS remains clean and accurate. Three power modes allow customized balance between the power consumption and performance. An extensive integrated interrupt engine offers many distinctive functionalities, such as the automatic enter/exit Low Power mode, advanced actions detection such as the running, walking, several other features such as the step counting, and more. After the POR (Power ON Reset) event, the device stays in the Sleep mode. In Sleep mode, the sensor practically does not consume any power (about 300nA), but the sensor functionality is completely suspended. To use the sensor, it has to be either in Low Power mode, where it uses a fixed Output Data Refresh (ODR) of 25Hz, or in the Normal mode. Some options are exclusive only to Normal mode, such as the step counting detection,

output filtering and so on. Normal mode still uses power conservative, allowing the device to be used in the Always-ON low power applications. While operating in Normal mode, two filters are available for the data filtering. The filters can be applied either to the output registers, the FIFO engine, or can be used to process the interrupt data. The first filter can be used to obtain data rates from 12.5Hz up to 800Hz, which is defined by the filter registers, while the second filter offers fixed frequency of 100Hz, superimposed by a frequency of 1Hz. The output noise is affected by the ODR frequency. Acceleration data is available in 12-bit format from both the data registers and the internal FIFO buffer of 1kb. The FIFO buffer can be used for more complex calculations or timed readings. Writing to FIFO buffer is only allowed in the Normal mode, while it can be read in the Low power mode, too. The interrupt engine facilitates the complete FIFO buffer, triggering an interrupt for several FIFO events: overflow event, watermark event, almost full event, and so on. The BMA400 sensor contains an integrated timer, which can be used along with the interrupts to be used for the auto Wakeup or auto Power down functions. The automatic functions are a part of the sensor power management. The automatic mode changes can be set either to an acceleration interrupt after a specified threshold is reached, or it can be set to a timer interrupt: when the timer expires, the interrupt is generated, and the power mode is switched. An extensive interrupt engine offers two layers of interrupts. It offers basic interrupts, including some basic functions, such as the Data Ready interrupt, FIFO buffer related interrupts and the Wakeup event interrupt. Basic interrupts also

report Interrupt overrun event, where too many interrupts are competing, so that the sensor is not able to process them all. Besides the basic interrupts, the interrupt engine offers some more sophisticated, advanced interrupts, that include detection all of the activities: tap/double tap, step counting, activity changed, orientation changed, and two generic interrupts. The advanced interrupts require a certain ODR rate and can be used in the Normal mode exclusively, while basic interrupts offer more rudimental control over events. The advanced interrupt engine can use two programmable interrupt pins. Both of these pins can be assigned with any interrupt source and can be either LOW or HIGH on interrupt, depending on settings in appropriate registers. These two pins are routed to INT and PWM pins of the mikroBUS™, and are labeled as IT1 and IT2, respectively. Besides the acceleration MEMS and complementary analog front-end circuitry, the BMA400 sensor also has an integrated temperature sensor. It is updated every 160ms and sampled with the 8-bit resolution. Thermal data is always available, except when the device is in the Sleep mode. Accel 5 click offers two communication interfaces. It can be used with either I2C or SPI. The onboard SMD jumpers labeled as SEL COM allow switching between the two interfaces. Note that all the jumpers have to be positioned either to I2C or to SPI position. When I2C interface is selected, an additional SMD jumper labeled as the I2C ADD becomes available, determining the least significant bit of the BMA400 I2C address. The Click board™ should be interfaced only with MCUs that operate on 3.3V.

Accel 5 Click top side image
Accel 5 Click bottom side image

Features overview

Development board

EasyPIC v8 is a development board specially designed for the needs of rapid development of embedded applications. It supports many high pin count 8-bit PIC microcontrollers from Microchip, regardless of their number of pins, and a broad set of unique functions, such as the first-ever embedded debugger/programmer. The development board is well organized and designed so that the end-user has all the necessary elements, such as switches, buttons, indicators, connectors, and others, in one place. Thanks to innovative manufacturing technology, EasyPIC v8 provides a fluid and immersive working experience, allowing access anywhere and under any

circumstances at any time. Each part of the EasyPIC v8 development board contains the components necessary for the most efficient operation of the same board. In addition to the advanced integrated CODEGRIP programmer/debugger module, which offers many valuable programming/debugging options and seamless integration with the Mikroe software environment, the board also includes a clean and regulated power supply module for the development board. It can use a wide range of external power sources, including a battery, an external 12V power supply, and a power source via the USB Type-C (USB-C) connector.

Communication options such as USB-UART, USB DEVICE, and CAN are also included, including the well-established mikroBUS™ standard, two display options (graphical and character-based LCD), and several different DIP sockets. These sockets cover a wide range of 8-bit PIC MCUs, from the smallest PIC MCU devices with only eight up to forty pins. EasyPIC v8 is an integral part of the Mikroe ecosystem for rapid development. Natively supported by Mikroe software tools, it covers many aspects of prototyping and development thanks to a considerable number of different Click boards™ (over a thousand boards), the number of which is growing every day.

EasyPIC v8 horizontal image

Microcontroller Overview

MCU Card / MCU

PIC18F46K42

Architecture

PIC

MCU Memory (KB)

64

Silicon Vendor

Microchip

Pin count

40

RAM (Bytes)

4096

Used MCU Pins

mikroBUS™ mapper

NC
NC
AN
NC
NC
RST
SPI Chip Select
RE0
CS
SPI Clock
RC3
SCK
SPI Data OUT
RC4
MISO
SPI Data IN
RC5
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
Interrupt 2
RC0
PWM
Interrupt 1
RB0
INT
NC
NC
TX
NC
NC
RX
I2C Clock
RC3
SCL
I2C Data
RC4
SDA
NC
NC
5V
Ground
GND
GND
1

Take a closer look

Schematic

Accel 5 Click Schematic schematic

Step by step

Project assembly

EasyPIC v8 front image hardware assembly

Start by selecting your development board and Click board™. Begin with the EasyPIC v8 as your development board.

EasyPIC v8 front image hardware assembly
Buck 22 Click front image hardware assembly
MCU DIP 40 hardware assembly
EasyPIC v8 DIP 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 Compiler Selection Step Image hardware assembly
NECTO Output Selection Step Image hardware assembly
Necto image step 6 hardware assembly
Necto DIP image step 7 hardware assembly
Necto image step 8 hardware assembly
Necto image step 9 hardware assembly
Necto image step 10 hardware assembly
Necto PreFlash Image hardware assembly

Track your results in real time

Application Output

After pressing the "FLASH" button on the left-side panel, it is necessary to open the UART terminal to display the achieved results. By clicking on the Tools icon in the right-hand panel, multiple different functions are displayed, among which is the UART Terminal. Click on the offered "UART Terminal" icon.

UART Application Output Step 1

Once the UART terminal is opened, the window takes on a new form. At the top of the tab are two buttons, one for adjusting the parameters of the UART terminal and the other for connecting the UART terminal. The tab's lower part is reserved for displaying the achieved results. Before connecting, the terminal has a Disconnected status, indicating that the terminal is not yet active. Before connecting, it is necessary to check the set parameters of the UART terminal. Click on the "OPTIONS" button.

UART Application Output Step 2

In the newly opened UART Terminal Options field, we check if the terminal settings are correct, such as the set port and the Baud rate of UART communication. If the data is not displayed properly, it is possible that the Baud rate value is not set correctly and needs to be adjusted to 115200. If all the parameters are set correctly, click on "CONFIGURE".

UART Application Output Step 3

The next step is to click on the "CONNECT" button, after which the terminal status changes from Disconnected to Connected in green, and the data is displayed in the Received data field.

UART Application Output Step 4

Software Support

Library Description

This library contains API for Accel 5 Click driver.

Key functions:

  • accel5_write_byte - Functions for write one byte in register

  • accel5_read_byte - Functions for read byte from register

  • accel5_read_data - Functions for read data from register

Open Source

Code example

This example can be found in NECTO Studio. Feel free to download the code, or you can copy the code below.

/*!
 * \file 
 * \brief Accel5 Click example
 * 
 * # Description
 * This application allows linear motion and gravitational force measurements.
 *
 * The demo application is composed of two sections :
 * 
 * ## Application Init 
 * Initializes Driver init and settings accelerometer data range and mode.
 * 
 * ## Application Task  
 * Reads the accel X / Y / Z axis data, every 500 ms.
 * 
 * \author MikroE Team
 *
 */
// ------------------------------------------------------------------- INCLUDES

#include "board.h"
#include "log.h"
#include "accel5.h"

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

static accel5_t accel5;
static log_t logger;

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

void application_init ( void )
{
    log_cfg_t log_cfg;
    accel5_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.

    accel5_cfg_setup( &cfg );
    ACCEL5_MAP_MIKROBUS( cfg, MIKROBUS_1 );

    if ( accel5_init( &accel5, &cfg ) == ACCEL5_INIT_ERROR )
    {
        log_info( &logger, "---- Application Error ----" );

        for ( ; ; );
    }

    log_info( &logger, "---- Application Init Done ----\n" );

    accel5_soft_reset( &accel5 );
    Delay_ms( 500 );
    accel5_default_cfg( &accel5, ACCEL5_CFG_0_NORMAL_MODE, ACCEL5_CFG_1_ACC_RANGE_4g );
    Delay_ms( 500 );
}

void application_task ( void )
{
    int16_t x_axis_data;
    int16_t y_axis_data;
    int16_t z_axis_data;

    //  Task implementation.

    x_axis_data = accel5_get_axis( &accel5, ACCEL5_X_AXIS );
    log_printf ( &logger, " X axis : %d\r\n", x_axis_data );

    y_axis_data = accel5_get_axis( &accel5, ACCEL5_Y_AXIS );
    log_printf ( &logger, " Y axis : %d\r\n", y_axis_data );

    z_axis_data = accel5_get_axis( &accel5, ACCEL5_Z_AXIS );
    log_printf ( &logger, " Z axis : %d\r\n\n", z_axis_data );

    Delay_ms( 500 );
}

void main ( void )
{
    application_init( );

    for ( ; ; )
    {
        application_task( );
    }
}

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

Additional Support

Resources