Intermediate
30 min

Achieve harmonious performance in battery cell-balancing with BQ25887 and PIC18F57Q43

Design safer battery solutions

Balancer 5 Click with Curiosity Nano with PIC18F57Q43

Published Feb 13, 2024

Click board™

Balancer 5 Click

Dev. board

Curiosity Nano with PIC18F57Q43

Compiler

NECTO Studio

MCU

PIC18F57Q43

The perfect companion for portable power with integrated cell balancing!

A

A

Hardware Overview

How does it work?

Balancer 5 Click is based on the BQ25887, a fully integrated 2-cell Li-ion battery charger from Texas Instruments, ideal for portable applications with cell balancing. The BQ25887 optimizes the charging of two batteries with balancing. The input voltage range from the USB connector can be as high as 5.5V; the battery can be charged up to 3.3A. When the input voltage exceeds the OVP (Over Voltage Protection) threshold, it will turn off the charging MOSFET to avoid overheating the chip. Besides its small physical size, the low number of external components makes this IC ideal for various applications. As Lithium Ion batteries require a very accurate current and voltage for charging, Balancer 5 click can be a perfect solution for such a task. Balancer 5 Click is equipped with a highly integrated Li-Ion battery charger, supporting intelligent, constant-current, constant

voltage (CCCV), a temperature-regulated battery charger that charges 2-cell lithium-ion (Li+) cell batteries. This Click has a charging current control IC over the I2C interface, which ensures perfect and efficient charging. Balancer 5 Click can be part of the power supply and distribution system in many applications: handheld appliances, portable media players, portable audio players, and other general-purpose battery-operated electronic devices. On the left side of the click board is an input USB connector, where an external voltage as high as 5.5V can be applied. Two connectors on the right side are reserved for Li-Ion batteries with corresponding markings; the right connector is for the first batteries, and the left connector is for the second batteries. When connected to a power source, the green CHARGE LED will indicate it, and LOW indicates the charge in progress.

HIGH indicates the charge is complete or disabled. When any fault occurs, the STAT pin blinks at 1Hz. The STAT function can be turned off when the STAT_DIS bit is set. On the right side is a 1x2 male header for connecting an NTC (Negative temperature Coefficient) resistor. Connect a negative temperature coefficient thermistor and program temperature window with a resistor divider from REGN to TS to GND. The charge suspends when the TS pin is out of range. Recommend 103AT-2 thermistor. This Click board™ can only be operated with a 5V 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.

Balancer 5 Click hardware overview image

Features overview

Development board

PIC18F57Q43 Curiosity Nano evaluation kit is a cutting-edge hardware platform designed to evaluate microcontrollers within the PIC18-Q43 family. Central to its design is the inclusion of the powerful PIC18F57Q43 microcontroller (MCU), offering advanced functionalities and robust performance. Key features of this evaluation kit include a yellow user LED and a responsive

mechanical user switch, providing seamless interaction and testing. The provision for a 32.768kHz crystal footprint ensures precision timing capabilities. With an onboard debugger boasting a green power and status LED, programming and debugging become intuitive and efficient. Further enhancing its utility is the Virtual serial port (CDC) and a debug GPIO channel (DGI

GPIO), offering extensive connectivity options. Powered via USB, this kit boasts an adjustable target voltage feature facilitated by the MIC5353 LDO regulator, ensuring stable operation with an output voltage ranging from 1.8V to 5.1V, with a maximum output current of 500mA, subject to ambient temperature and voltage constraints.

PIC18F57Q43 Curiosity Nano double side image

Microcontroller Overview

MCU Card / MCU

default

Architecture

PIC

MCU Memory (KB)

128

Silicon Vendor

Microchip

Pin count

48

RAM (Bytes)

8196

You complete me!

Accessories

Curiosity Nano Base for Click boards is a versatile hardware extension platform created to streamline the integration between Curiosity Nano kits and extension boards, tailored explicitly for the mikroBUS™-standardized Click boards and Xplained Pro extension boards. This innovative base board (shield) offers seamless connectivity and expansion possibilities, simplifying experimentation and development. Key features include USB power compatibility from the Curiosity Nano kit, alongside an alternative external power input option for enhanced flexibility. The onboard Li-Ion/LiPo charger and management circuit ensure smooth operation for battery-powered applications, simplifying usage and management. Moreover, the base incorporates a fixed 3.3V PSU dedicated to target and mikroBUS™ power rails, alongside a fixed 5.0V boost converter catering to 5V power rails of mikroBUS™ sockets, providing stable power delivery for various connected devices.

Curiosity Nano Base for Click boards accessories 1 image

Li-Polymer Battery is the ideal solution for devices that demand a dependable and long-lasting power supply while emphasizing mobility. Its compatibility with mikromedia boards ensures easy integration without additional modifications. With a voltage output of 3.7V, the battery meets the standard requirements of many electronic devices. Additionally, boasting a capacity of 2000mAh, it can store a substantial amount of energy, providing sustained power for extended periods. This feature minimizes the need for frequent recharging or replacement. Overall, the Li-Polymer Battery is a reliable and autonomous power source, ideally suited for devices requiring a stable and enduring energy solution. You can find a more extensive choice of Li-Polymer batteries in our offer.

Balancer 5 Click accessories image

Used MCU Pins

mikroBUS™ mapper

Power Good Indicator
PA0
AN
Power Source Selection
PA7
RST
Chip Disable
PD4
CS
NC
NC
SCK
NC
NC
MISO
NC
NC
MOSI
NC
NC
3.3V
Ground
GND
GND
NC
NC
PWM
Interrupt
PA6
INT
NC
NC
TX
NC
NC
RX
I2C Clock
PB2
SCL
I2C Data
PB1
SDA
Power Supply
5V
5V
Ground
GND
GND
1

Take a closer look

Click board™ Schematic

Balancer 5 Click Schematic schematic

Step by step

Project assembly

Curiosity Nano Base for Click boards front image hardware assembly

Start by selecting your development board and Click board™. Begin with the Curiosity Nano with PIC18F57Q43 as your development board.

Curiosity Nano Base for Click boards front image hardware assembly
Charger 27 Click front image hardware assembly
PIC18F47Q10 Curiosity Nano front image hardware assembly
Prog-cut hardware assembly
Charger 27 Click complete accessories setup image hardware assembly
Curiosity Nano with PICXXX Access 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
PIC18F57Q43 Curiosity 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 Balancer 5 Click driver.

Key functions:

  • balancer5_charge - This function sets charging status

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 Balancer5 Click example
 * 
 * # Description
 * This example demonstrates basic Balancer 5 Click functionalities.
 *
 * The demo application is composed of two sections :
 * 
 * ## Application Init 
 * Initializes Click and Driver, Checks Device ID, starts charging,
 * reads charge status registers and configures ADC.
 * 
 * ## Application Task  
 * Reads ADC values from registers and logs it.
 * 
 * ## Additional function
 * void charger_status_1_handler ( uint8_t cs1_data );
 * void charger_status_2_handler ( uint8_t cs2_data );
 * 
 * \author MikroE Team
 *
 */
// ------------------------------------------------------------------- INCLUDES

#include "board.h"
#include "log.h"
#include "balancer5.h"

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

static balancer5_t balancer5;
static log_t logger;

static uint8_t temp_data;
static uint16_t temp_uint_data;
static float temp_float_data;

// ------------------------------------------------------- ADDITIONAL FUNCTIONS

void charger_status_1_handler ( uint8_t cs1_data )
{
    uint8_t charge = 0;

    log_printf( &logger, "* CHARGER STATUS 1 :\r\n" );
    if ( ( cs1_data & BALANCER5_CS1_IINDPM_IN_REGULATION ) == BALANCER5_CS1_IINDPM_IN_REGULATION )
    {
        log_printf( &logger, "    - IINDPM_IN_REGULATION" );
    }
    else
    {
        log_printf( &logger, "    - IINDPM_NORMAL" );
    }

    if ( ( cs1_data & BALANCER5_CS1_VINDPM_IN_REGULATION ) == BALANCER5_CS1_VINDPM_IN_REGULATION )
    {
        log_printf( &logger, "    - VINDPM_IN_REGULATION" );
    }
    else
    {
        log_printf( &logger, "    - VINDPM_NORMAL" );
    }

    if ( ( cs1_data & BALANCER5_CS1_IC_IN_THERMAL_REGULATION ) == BALANCER5_CS1_IC_IN_THERMAL_REGULATION )
    {
        log_printf( &logger, "    - IC_IN_THERMAL_REGULATION" );
    }
    else
    {
        log_printf( &logger, "    - IC_NORMAL" );
    }

    if ( ( cs1_data & BALANCER5_CS1_WD_TIMER_EXPIRED ) == BALANCER5_CS1_WD_TIMER_EXPIRED )
    {
        log_printf( &logger, "    - WD_TIMER_EXPIRED" );
    }
    else
    {
        log_printf( &logger, "    - WD_NORMAL" );
    }

    if ( ( cs1_data & BALANCER5_CS1_TRICKLE_CHARGE ) == BALANCER5_CS1_TRICKLE_CHARGE )
    {
        charge++;
        log_printf( &logger, "    - TRICKLE_CHARGE" );
    }
    if ( ( cs1_data & BALANCER5_CS1_PRE_CHARGE ) == BALANCER5_CS1_PRE_CHARGE )
    {
        charge++;
        log_printf( &logger, "    - PRE_CHARGE" );
    }
    if ( ( cs1_data & BALANCER5_CS1_FAST_CHARGE ) == BALANCER5_CS1_FAST_CHARGE )
    {
        charge++;
        log_printf( &logger, "    - FAST_CHARGE" );
    }
    if ( ( cs1_data & BALANCER5_CS1_TAPER_CHARGE ) == BALANCER5_CS1_TAPER_CHARGE )
    {
        charge++;
        log_printf( &logger, "    - TAPER_CHARGE" );
    }
    if ( ( cs1_data & BALANCER5_CS1_TOP_OFF_TIMER_CHARGE ) == BALANCER5_CS1_TOP_OFF_TIMER_CHARGE )
    {
        charge++;
        log_printf( &logger, "    - TOP_OFF_TIMER_CHARG" );
    }
    if ( ( cs1_data & BALANCER5_CS1_CHARGE_TERMINATION ) == BALANCER5_CS1_CHARGE_TERMINATION )
    {
        charge++;
        log_printf( &logger, "    - CHARGE_TERMINATION" );
    }
    if ( charge == 0 )
    {
        log_printf( &logger, "    - NOT_CHARGING" );
    }
}

void charger_status_2_handler ( uint8_t cs2_data )
{
    uint8_t power_in = 0;

    log_printf( &logger, "\r\n* CHARGER STATUS 2 :\r\n" );

    if ( ( cs2_data & BALANCER5_CS2_MAX_INPUT ) == BALANCER5_CS2_MAX_INPUT )
    {
        log_printf( &logger, "    - MAX_INPUT" );
    }
    else if ( ( cs2_data & BALANCER5_CS2_ICO_OPTIMIZATION_IN_PROGRESS ) == BALANCER5_CS2_ICO_OPTIMIZATION_IN_PROGRESS )
    {
        log_printf( &logger, "    - ICO_OPTIMIZATION_IN_PROGRESS" );
    }
    else
    {
        log_printf( &logger, "    - ICO_DISABLED" );
    }

    if ( ( cs2_data & BALANCER5_CS2_POWER_GOOD ) == BALANCER5_CS2_POWER_GOOD )
    {
        log_printf( &logger, "    - POWER_GOOD" );
    }
    else
    {
        log_printf( &logger, "    - POWER_NOT_GOOD" );
    }

    if ( ( cs2_data & BALANCER5_CS2_NON_STANDARD_ADAPTER ) == BALANCER5_CS2_NON_STANDARD_ADAPTER )
    {
        power_in++;
        log_printf( &logger, "    - NON_STANDARD_ADAPTER" );
    }
    if ( ( cs2_data & BALANCER5_CS2_UNKNOWN_ADAPTER ) == BALANCER5_CS2_UNKNOWN_ADAPTER )
    {
        power_in++;
        log_printf( &logger, "    - UNKNOWN_ADAPTER" );
    }
    if ( ( cs2_data & BALANCER5_CS2_POORSRC ) == BALANCER5_CS2_POORSRC )
    {
        power_in++;
        log_printf( &logger, "    - POORSRC" );
    }
    if ( ( cs2_data & BALANCER5_CS2_ADAPTER ) == BALANCER5_CS2_ADAPTER )
    {
        power_in++;
        log_printf( &logger, "    - ADAPTER" );
    }
    if ( ( cs2_data & BALANCER5_CS2_USB_CDP ) == BALANCER5_CS2_USB_CDP )
    {
        power_in++;
        log_printf( &logger, "    - CS2_USB_CDP" );
    }
    if ( ( cs2_data & BALANCER5_CS2_USB_HOST_SDP ) == BALANCER5_CS2_USB_HOST_SDP )
    {
        power_in++;
        log_printf( &logger, "    - USB_HOST_SDP" );
    }
    if ( power_in == 0 )
    {
        log_printf( &logger, "    - NO_INPUT" );
    }
}

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

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

    balancer5_cfg_setup( &cfg );
    BALANCER5_MAP_MIKROBUS( cfg, MIKROBUS_1 );
    balancer5_init( &balancer5, &cfg );

    //  Device ID sanity check

    temp_data = balancer5_check_id( &balancer5 );

    if ( temp_data == BALANCER5_ERROR_ID )
    {
        log_info( &logger, "ID ERROR!!!" );
        for ( ; ; );
    }
    
    log_info( &logger, "***** ID OK *****" );

    //  Switch charger on

    balancer5_charge( &balancer5, BALANCER5_CHARGE_ON );

    //  Send configuration info to logger

    temp_data = balancer5_read_data( &balancer5, BALANCER5_REG_CHARGER_STATUS_1 );
    charger_status_1_handler( temp_data );

    temp_data = balancer5_read_data( &balancer5, BALANCER5_REG_CHARGER_STATUS_2 );
    charger_status_2_handler( temp_data );

    //  Set default configuration

    balancer5_default_cfg ( &balancer5 );
   
}

void application_task ( void )
{
    temp_data = balancer5_read_data( &balancer5, BALANCER5_REG_IBUS_ADC1 );
    temp_uint_data = temp_data;
    temp_uint_data <<= 8;
    temp_data = balancer5_read_data( &balancer5, BALANCER5_REG_IBUS_ADC0 );
    temp_uint_data |= temp_data;
    log_printf( &logger, "- IBUS: %umA\r\n", temp_uint_data );

    temp_data = balancer5_read_data( &balancer5, BALANCER5_REG_ICHG_ADC1 ); 
    temp_uint_data = temp_data;
    temp_uint_data <<= 8;
    temp_data = balancer5_read_data( &balancer5, BALANCER5_REG_ICHG_ADC0 );
    temp_uint_data |= temp_data;
    log_printf( &logger, "- ICHG: %umA\r\n", temp_uint_data );

    temp_data = balancer5_read_data( &balancer5, BALANCER5_REG_VBAT_ADC1 );
    temp_uint_data = temp_data;
    temp_uint_data <<= 8;
    temp_data = balancer5_read_data( &balancer5, BALANCER5_REG_VBAT_ADC0 );
    temp_uint_data |= temp_data;
    log_printf( &logger, "- VBAT: %umV\r\n", temp_uint_data );

    temp_data = balancer5_read_data( &balancer5, BALANCER5_REG_VBUS_ADC1 );
    temp_uint_data = temp_data;
    temp_uint_data <<= 8;
    temp_data = balancer5_read_data( &balancer5, BALANCER5_REG_VBUS_ADC0 );
    temp_uint_data |= temp_data;
    log_printf( &logger, "- VBUS: %umV\r\n", temp_uint_data );

    temp_data = balancer5_read_data( &balancer5, BALANCER5_REG_VCELLTOP_ADC1 );
    temp_uint_data = temp_data;
    temp_uint_data <<= 8;
    temp_data = balancer5_read_data( &balancer5, BALANCER5_REG_VCELLTOP_ADC0 );
    temp_uint_data |= temp_data;
    log_printf( &logger, "- VCELLTOP: %umV\r\n", temp_uint_data );

    temp_data = balancer5_read_data( &balancer5, BALANCER5_REG_VCELLBOT_ADC1 );
    temp_uint_data = temp_data;
    temp_uint_data <<= 8;
    temp_data = balancer5_read_data( &balancer5, BALANCER5_REG_VCELLBOT_ADC0 );
    temp_uint_data |= temp_data;
    log_printf( &logger, "- VCELLBOT: %umV\r\n", temp_uint_data );

    temp_data = balancer5_read_data( &balancer5, BALANCER5_REG_TS_ADC1 );
    temp_uint_data = temp_data;
    temp_uint_data <<= 8;
    temp_data = balancer5_read_data( &balancer5, BALANCER5_REG_TS_ADC0 );
    temp_uint_data |= temp_data;
    temp_float_data = temp_uint_data;
    temp_float_data *= 0.098;
    log_printf( &logger, "- TS: %.2f%%\r\n", temp_float_data );

    temp_data = balancer5_read_data( &balancer5, BALANCER5_REG_TDIE_ADC1 );
    temp_uint_data = temp_data;
    temp_uint_data <<= 8;
    temp_data = balancer5_read_data( &balancer5, BALANCER5_REG_TDIE_ADC0 );
    temp_uint_data |= temp_data;
    temp_float_data = temp_uint_data;
    temp_float_data *= 0.5;
    log_printf( &logger, "- TDIE: %.2f degC\r\n", temp_float_data );
    log_printf( &logger, "____________________\r\n" );
    
    Delay_ms( 5000 );
}

void main ( void )
{
    application_init( );

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


// ------------------------------------------------------------------------ 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.