Intermediate
30 min

Master electrical power analysis with MCP3910 and PIC18F67K40

Voltage, current, and beyond

PWR Meter 2 click with Clicker 2 for PIC18FK

Published Sep 29, 2023

Click board™

PWR Meter 2 click

Dev Board

Clicker 2 for PIC18FK

Compiler

NECTO Studio

MCU

PIC18F67K40

Our groundbreaking power monitoring solution is designed to provide unrivaled accuracy in measuring and monitoring voltage and current values, ensuring optimal performance and efficiency in your electrical systems.

A

A

Hardware Overview

How does it work?

PWR Meter 2 Click is based on the MCP3910, an integrated two-channel analog front-end (AFE) device from Microchip. This IC is composed of several sections, aimed at accurate capturing of the input voltage. Two sigma-delta input A/D converters used with the internal reference voltage of 1.2V with very low thermal drift, reducing the measurement noise at a minimum, yielding Signal to Noise ratio (SNR) up to 96dB. The input ADCs are fully configurable and can be set to work in 16-bit or 24-bit mode, can use oversampling ratio from 32 X up to 4096 X, gain ratio from 1 X to 32 X, and 24-bit digital offset and gain error correction for each ADC channel. The Click board™ is clocked by a 20MHz crystal. However, it is up to the user to set the pre-scalers correctly, according to the datasheet of the MCP3910. However, the included library offers functions which take care about correct settings. High clock speed allows maximum oversampling rate (OSR) to be used, allowing the best performance to be achieved, but consuming more power at the same time. The voltage and current readings are performed over two differential ADC input channels of the MCP3910 itself. The CH0 (Channel 0) is connected to a resistor voltage

divider, allowing it to measure up to 0.6V when the input voltage is 24V, which is the maximum voltage at the input terminal. Although the voltage across a differential input on the ADC channel can go up to ±2V, it is recommended by the manufacturer to stay within the ±0.6V margin to achieve optimal harmonic distortion and noise ratios, which might affect the measurement accuracy. The CH1 (Channel 1) differential input is connected to the shunt resistor of 0.03 Ω. A small voltage drop is measured by the ADC, allowing up to 5A of current to be measured. More of 5A might destroy the shunt resistor so it is not recommended going over 5A. The current measurement is done by connecting the load in series with the Click board™, so the shunt value of 0.03Ω will not introduce significant error or influence the current through the load. The measurement is performed using so-called Kelvin connections, where the main trace carries the majority of the current, while thin traces are used to measure the voltage across the shunt, reducing the current running through the ADC section of the IC itself. Additional 270 Ω resistors reduce the current through the ADC even further. The MCP3910 contains several additional pins, which

are used to simplify the implementation and reduce the bulkiness of the firmware application. The Data Ready pin can be used to trigger an interrupt event on the host MCU when there is conversion data ready to be read. This simplifies the MCU performance greatly, saving it from having to poll status bits in order to determine if the data is ready for reading. The Data Ready pin is routed to the mikroBUS™ INT pin, labeled as the DR. Two Modulator Output pins are also routed to the mikroBUS™. These pins offer direct 1-bit data output directly from the delta-sigma modulators for a user-defined MCU or DSP filtering, overriding the internal SINC filter, which is turned off if these pins are activated. The DR pin is also disabled when these pins are enabled. MDAT0 and MDAT1 pins offer modulator output from ADC channel 0 and ADC channel 1. These pins are routed to mikroBUS™ pins PWM and AN and are labeled as MDT0 and MDT1, respectively. As already mentioned, the Click board™ offers measurement of either internal power supply from the mikroBUS™, or the externally with up to 24V and 5A. To select the measurement target, the SMD jumper labeled INPUT SEL should be switched to the desired position.

PWR Meter 2 click hardware overview image

Features overview

Development board

Clicker 2 for PIC18FK is a compact starter development board that brings the flexibility of add-on Click boards™ to your favorite microcontroller, making it a perfect starter kit for implementing your ideas. It comes with an onboard 8-bit PIC microcontroller, the PIC18F67K40 from Microchip, two mikroBUS™ sockets for Click board™ connectivity, a USB connector, LED indicators, buttons, a mikroProg connector, and two 26-pin headers for interfacing with external electronics. Its compact design with clear and easily recognizable silkscreen markings allows you to build gadgets with unique functionalities and features quickly.

Each part of the Clicker 2 for PIC18FK development kit contains the components necessary for the most efficient operation of the same board. In addition to the possibility of choosing the Clicker 2 for PIC18FK programming method: using UART mikroBootloader, an external mikroProg connector for PIC18FK programmer, or through Xpress bootloader, the Clicker 2 board also includes a clean and regulated power supply module for the development kit. It provides two ways of board-powering; through the USB Micro-B cable, where onboard voltage regulators provide the appropriate voltage levels to each component

on the board, or using a Li-Polymer battery via an onboard battery connector. All communication methods that mikroBUS™ itself supports are on this board, including the well-established mikroBUS™ socket, reset button, and several user-configurable buttons and LED indicators. Clicker 2 for PIC18FK is an integral part of the Mikroe ecosystem, allowing you to create a new application in minutes. Natively supported by Mikroe software tools, it covers many aspects of prototyping thanks to a considerable number of different Click boards™ (over a thousand boards), the number of which is growing every day.

Clicker 2 for PIC18FK dimensions image

Microcontroller Overview

MCU Card / MCU

default

Architecture

PIC

MCU Memory (KB)

128

Silicon Vendor

Microchip

Pin count

64

RAM (Bytes)

3562

Used MCU Pins

mikroBUS™ mapper

Modulator Output 1
RA0
AN
Reset
RG3
RST
SPI Chip Select
RD0
CS
SPI Clock
RC3
SCK
SPI Data OUT
RC4
MISO
SPI Data IN
RC5
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
Modulator Output 0
RC2
PWM
Data Ready
RB2
INT
NC
NC
TX
NC
NC
RX
NC
NC
SCL
NC
NC
SDA
NC
NC
5V
Ground
GND
GND
1

Take a closer look

Schematic

PWR Meter 2 click Schematic schematic

Step by step

Project assembly

Clicker 2 for PIC32MZ front image hardware assembly

Start by selecting your development board and Click board™. Begin with the Clicker 2 for PIC18FK as your development board.

Clicker 2 for PIC32MZ front image hardware assembly
GNSS2 Click front image hardware assembly
Prog-cut hardware assembly
GNSS2 Click complete accessories setup image hardware assembly
Micro B Connector Clicker 2 Access - 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
Flip&Click PIC32MZ 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

After loading the code example, pressing the "DEBUG" button builds and programs it on the selected setup.

Application Output Step 1

After programming is completed, a header with buttons for various actions available in the IDE appears. By clicking the green "PLAY "button, we start reading the results achieved with Click board™.

Application Output Step 3

Upon completion of programming, the Application Output tab is automatically opened, where the achieved result can be read. In case of an inability to perform the Debug function, check if a proper connection between the MCU used by the setup and the CODEGRIP programmer has been established. A detailed explanation of the CODEGRIP-board connection can be found in the CODEGRIP User Manual. Please find it in the RESOURCES section.

Application Output Step 4

Software Support

Library Description

This library contains API for PWR Meter 2 Click driver.

Key functions:

  • pwrmeter2_get_data - This function gets the calculated voltage( V ), current( A ) and power( W ) data

  • pwrmeter2_write_reg - This function writes 24-bit data to the register

  • pwrmeter2_read_reg - This function reads the desired number of 24-bit data from the register/registers.

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 PwrMeter2 Click example
 * 
 * # Description
 * This app measuring and monitoring voltage up to 24V and current up to 5A.
 *
 * The demo application is composed of two sections :
 * 
 * ## Application Init 
 * Initializes device.
 * 
 * ## Application Task  
 * Gets calculated voltage, current and power data every 500 miliseconds
 * and shows results on UART.
 * 
 * \author MikroE Team
 *
 */
// ------------------------------------------------------------------- INCLUDES

#include "board.h"
#include "log.h"
#include "pwrmeter2.h"

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

static pwrmeter2_t pwrmeter2;
static log_t logger;

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

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

    pwrmeter2_cfg_setup( &cfg );
    PWRMETER2_MAP_MIKROBUS( cfg, MIKROBUS_1 );
    pwrmeter2_init( &pwrmeter2, &cfg );

    pwrmeter2_default_cfg( &pwrmeter2 );
    log_printf( &logger, "PWR Meter 2 is initialized \r\n" );
    Delay_ms( 200 );
}

void application_task ( void )
{
    int32_t voltage_res;
    int32_t current_res;
    uint32_t power_res;

    pwrmeter2_get_data( &pwrmeter2, &voltage_res, &current_res, &power_res );

    log_printf( &logger, "U = %ld mV \r\n", voltage_res );
    log_printf( &logger, "I = %ld mA \r\n", current_res );
    log_printf( &logger, "P = %lu mW \r\n", power_res );

    Delay_ms( 500 );
}

void main ( void )
{
    application_init( );

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

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

Additional Support

Resources