Intermediate
30 min

Master electrical power analysis with MCP3910 and PIC32MZ2048EFM100

Voltage, current, and beyond

PWR Meter 2 click with Curiosity PIC32 MZ EF

Published Sep 29, 2023

Click board™

PWR Meter 2 click

Dev Board

Curiosity PIC32 MZ EF

Compiler

NECTO Studio

MCU

PIC32MZ2048EFM100

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

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

Modulator Output 1
RPB4
AN
Reset
RA9
RST
SPI Chip Select
RPD4
CS
SPI Clock
RPD1
SCK
SPI Data OUT
RPD14
MISO
SPI Data IN
RPD3
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
Modulator Output 0
RPE8
PWM
Data Ready
RF13
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

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
GNSS2 Click front image hardware assembly
Prog-cut hardware assembly
GNSS2 Click complete accessories setup image hardware assembly
Curiosity PIC32 MZ EF MB 1 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
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 via Debug Mode

1. Once the code example is loaded, pressing the "DEBUG" button initiates the build process, programs it on the created setup, and enters Debug mode.

2. After the programming is completed, a header with buttons for various actions within the IDE becomes visible. Clicking the green "PLAY" button starts reading the results achieved with the Click board™. The achieved results are displayed in the Application Output tab.

DEBUG_Application_Output

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 milliseconds
 * 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;  /**< Logger config object. */
    pwrmeter2_cfg_t pwrmeter2_cfg;  /**< Click config object. */

    /** 
     * 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( &pwrmeter2_cfg );
    PWRMETER2_MAP_MIKROBUS( pwrmeter2_cfg, MIKROBUS_1 );
    if ( SPI_MASTER_ERROR == pwrmeter2_init( &pwrmeter2, &pwrmeter2_cfg ) )
    {
        log_error( &logger, " Communication init." );
        for ( ; ; );
    }
    
    if ( PWRMETER2_ERROR == pwrmeter2_default_cfg ( &pwrmeter2 ) )
    {
        log_error( &logger, " Default configuration." );
        for ( ; ; );
    }
    
    log_info( &logger, " Application Task " );
}

void application_task ( void )
{
    float voltage = 0;
    float current = 0;
    float power = 0;
    if ( PWRMETER2_OK == pwrmeter2_get_data( &pwrmeter2, &voltage, &current, &power ) )
    {
        log_printf( &logger, " U = %.3f V\r\n", voltage );
        log_printf( &logger, " I = %.3f A\r\n", current );
        log_printf( &logger, " P = %.3f W\r\n\n", power );
        Delay_ms ( 500 );
    }
}

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.