Design of a Microcontroller-Based Charging and Discharging Circuit for LiFePO4 Battery Packs

In the realm of energy storage systems, the LiFePO4 battery has emerged as a pivotal technology due to its exceptional characteristics, including high energy density, long cycle life, enhanced safety, and environmental friendliness. These batteries, particularly in pack configurations such as series-connected 18650 or 26650 cells, are extensively employed in applications ranging from portable power banks and electric vehicles to backup power systems for telecommunications. However, the efficient management of these LiFePO4 battery packs necessitates sophisticated charging and discharging circuits that ensure optimal performance, longevity, and safety. Traditional unidirectional circuits often fall short in terms of cost, efficiency, and functionality, prompting the development of bidirectional power conversion systems. In this article, I present a comprehensive design of a charging and discharging circuit centered around a microcontroller, specifically tailored for LiFePO4 battery packs. The system leverages a bidirectional DC-DC converter as the main power stage, controlled by a STC12C5A60S2 microcontroller, which integrates analog-to-digital conversion and pulse-width modulation capabilities. Through detailed hardware and software implementations, this design aims to achieve high precision in current and voltage regulation, bidirectional energy flow, and robust protection features, all while maintaining efficiency above 90%. The discussion will encompass circuit topology, component selection, control algorithms, and experimental validation, with an emphasis on the advantages of using such a system for LiFePO4 battery management.

The core of the charging and discharging circuit for the LiFePO4 battery pack is the bidirectional DC-DC converter, which enables seamless energy transfer in both directions—charging from a power source to the battery and discharging from the battery to a load. This topology eliminates the need for separate unidirectional circuits, thereby reducing cost, size, and complexity. The converter operates in two distinct modes: buck mode for charging and boost mode for discharging. In buck mode, the input voltage is stepped down to match the LiFePO4 battery pack voltage, while in boost mode, the battery voltage is stepped up to supply a higher output voltage. The circuit schematic, as illustrated in prior designs, utilizes two MOSFET switches (SW and SR) and an inductor L to facilitate synchronous switching, minimizing losses associated with diode conduction. The operation principles can be mathematically described using fundamental equations for DC-DC converters. For the buck mode during charging, the output voltage \(V_o\) is related to the input voltage \(V_s\) and duty cycle \(D\) by:

$$V_o = D \cdot V_s \quad \text{for} \quad 0 \leq D \leq 1$$

where \(D\) is the duty cycle of the switch SW. Conversely, in boost mode during discharging, the output voltage \(V_s\) (now acting as the load side) is given by:

$$V_s = \frac{V_o}{1 – D} \quad \text{for} \quad 0 \leq D < 1$$

These equations underscore the bidirectional capability, allowing the same hardware to manage both charging and discharging of the LiFePO4 battery pack efficiently. The inductor L plays a critical role in energy storage and transfer, with its value selected based on desired ripple current and switching frequency. For this design, a switching frequency of 47 kHz was chosen to balance switching losses and component size. The bidirectional nature of this converter is particularly advantageous for LiFePO4 battery packs, as it supports periodic conditioning cycles that help maintain cell balance and extend lifespan. Moreover, the use of MOSFETs in place of diodes reduces conduction losses, enhancing overall efficiency—a key requirement for sustainable energy systems.

To realize the charging and discharging functions for the LiFePO4 battery pack, the hardware architecture comprises several interconnected modules: the main bidirectional DC-DC converter, microcontroller-based control unit, driver circuits, sensor interfaces, power supply, and user interface. The system is designed to handle a typical LiFePO4 battery pack consisting of five 18650 cells in series, with a nominal voltage of around 16.5V and a capacity tailored for high-current applications. The primary specifications include adjustable charging current from 0.9A to 1.5A with less than 2% error, automatic charge termination upon reaching a set voltage, over-voltage and over-discharge protection, and bidirectional efficiency exceeding 90%. The overall block diagram, as conceptualized in the design phase, integrates these elements into a cohesive system where the microcontroller orchestrates all operations based on feedback signals.

The control nucleus of the circuit is the STC12C5A60S2 microcontroller, an enhanced 8051-compatible device with integrated 10-bit ADC and PWM modules. This choice eliminates the need for external ADCs and PWM generators, simplifying the design and reducing cost. The microcontroller reads input and output voltages, as well as battery current, via conditioning circuits that scale the signals to the 0-5V range suitable for ADC conversion. For voltage sensing, resistive dividers are employed, with calculations ensuring accurate measurement. For instance, the input voltage \(V_s\) is scaled using a divider ratio \(R_1/(R_1 + R_2)\), yielding an ADC input voltage \(V_{adc} = V_s \cdot \frac{R_2}{R_1 + R_2}\). Similarly, the battery pack voltage \(V_o\) is measured. Current sensing is achieved through a low-resistance shunt resistor (20 mΩ) and an operational amplifier circuit based on the NE5534, which provides amplification with a gain \(A = 1 + \frac{R_4}{R_1}\). The output current \(I_R\) is derived from the voltage across the shunt, ensuring precise monitoring for both charging and discharging modes. The use of a LiFePO4 battery pack necessitates careful calibration of these sensors to account for the battery’s flat discharge curve and specific voltage thresholds.

MOSFET driver circuits are implemented using the IR2104 half-bridge driver IC, which generates high-side and low-side gate signals for the switches SW and SR. This driver incorporates bootstrap circuitry to efficiently drive the high-side MOSFET without requiring an isolated power supply, thus enhancing reliability. The auxiliary power for the control electronics is derived from a standard LM7805 linear regulator, which steps down the input voltage to a stable 5V rail. Additionally, the ICL7660 voltage converter is used to generate a -5V supply for the NE5534 op-amp, ensuring proper operation of the current sensing circuit. The user interface consists of a 12864 graphical LCD display and five tactile buttons, allowing users to select modes, adjust parameters, and view real-time data such as voltages, currents, and system status. This interface is crucial for configuring the circuit for different LiFePO4 battery pack configurations and monitoring performance during operation.

In terms of software design, the firmware for the microcontroller is structured around a main loop and interrupt-driven routines to ensure responsive control. The main program handles initialization, keypad scanning, display updates, and software filtering of sensor data. A moving average filter is implemented to reduce noise in ADC readings, which involves sampling multiple points, sorting them, discarding outliers, and computing the mean. This enhances measurement accuracy, critical for maintaining precise control over the LiFePO4 battery pack charging and discharging processes. The core control algorithm is executed within an ADC interrupt service routine, which triggers at regular intervals synchronized with the PWM switching cycle. This routine implements digital PI (proportional-integral) controllers for both current and voltage regulation. The PI algorithm computes the control output \(u(k)\) at each sampling instant \(k\) based on the error \(e(k)\) between the setpoint and measured value. For current control during charging, the error is defined as \(e(k) = I_{set} – I_{measured}\), and the control law is:

$$u(k) = K_p \cdot e(k) + K_i \cdot \sum_{j=0}^{k} e(j) \cdot T_s$$

where \(K_p\) and \(K_i\) are the proportional and integral gains, respectively, and \(T_s\) is the sampling period. The output \(u(k)\) is then mapped to a PWM duty cycle to adjust the switch conduction times, thereby regulating the current flowing into the LiFePO4 battery pack. Similarly, for voltage control during discharging or charge termination, the error is based on voltage deviations. The PWM module generates 47 kHz signals with adjustable duty cycles, driving the MOSFETs via the IR2104 drivers. This closed-loop control ensures that the LiFePO4 battery pack is charged with constant current until the voltage reaches a preset limit (e.g., 21V for the five-cell pack), after which the system switches to constant voltage mode or halts charging entirely to prevent overcharging. Discharging mode employs a similar PI controller to maintain a stable output voltage while limiting current to safe levels.

To validate the design, extensive experiments were conducted on a prototype circuit configured for a LiFePO4 battery pack of five 18650 cells. The testing focused on key performance metrics: current control accuracy, efficiency, and protection features. All measurements were taken using precision instruments, including a 6.5-digit multimeter, to ensure reliability. The results are summarized in tables below, which highlight the circuit’s capabilities in managing the LiFePO4 battery pack.

First, the current control accuracy during charging was assessed by setting the charging current \(I_S\) at various levels between 0.9A and 1.5A and measuring the actual current \(I_R\). The error \(E\) was calculated as \(E = \frac{|I_R – I_S|}{I_S} \times 100\%\). As shown in Table 1, the maximum error observed was 0.692%, well within the 2% design target. This demonstrates the effectiveness of the digital PI controller in regulating current for the LiFePO4 battery pack.

Table 1: Current Control Accuracy During Charging for LiFePO4 Battery Pack
Set Current \(I_S\) (A) Measured Current \(I_R\) (A) Error \(E\) (%)
0.90 0.900 0.000
1.00 0.996 0.400
1.10 1.097 0.273
1.20 1.195 0.417
1.30 1.291 0.692
1.40 1.399 0.071
1.50 1.497 0.200

Next, the impact of input voltage variations on charging current was evaluated by fixing \(I_S = 1A\) and varying the input voltage \(V_S\) from 25V to 31V. The measured current \(I_R\) remained stable, with errors not exceeding 0.5%, as detailed in Table 2. This robustness is essential for real-world applications where power source fluctuations may occur, ensuring consistent charging for the LiFePO4 battery pack.

Table 2: Current Stability Under Input Voltage Variations for LiFePO4 Battery Pack
Input Voltage \(V_S\) (V) Measured Current \(I_R\) (A) Error \(E\) (%)
25 0.999 0.1
26 1.001 0.1
27 1.002 0.2
28 1.002 0.2
29 1.000 0.0
30 1.005 0.5
31 1.001 0.1

Efficiency measurements were carried out for both charging and discharging modes. During charging, with \(V_S = 30V\), \(I_S = 1A\), and the LiFePO4 battery pack voltage \(V_o = 24.45V\), the actual charging current \(I_R = 0.999A\) was recorded. The charging efficiency \(\eta_c\) is calculated as:

$$\eta_c = \frac{V_o \cdot I_R}{V_S \cdot I_S} \times 100\%$$

yielding \(\eta_c = 96.99\%\), as shown in Table 3. This high efficiency minimizes energy loss and heat generation, which is beneficial for the longevity of the LiFePO4 battery pack.

Table 3: Charging Efficiency for LiFePO4 Battery Pack
Input Voltage \(V_S\) (V) Input Current \(I_S\) (A) Battery Voltage \(V_o\) (V) Charging Current \(I_R\) (A) Efficiency \(\eta_c\) (%)
29.98 0.840 24.45 0.999 96.99

For discharging, the circuit was configured in boost mode to supply a constant voltage of 30V to a resistive load, with the LiFePO4 battery pack at \(V_o = 18.51V\) and discharging current \(I_R = 1.648A\). The load current \(I_S\) was measured as 0.960A at \(V_S = 29.80V\). The discharging efficiency \(\eta_d\) is given by:

$$\eta_d = \frac{V_S \cdot I_S}{V_o \cdot I_R} \times 100\%$$

resulting in \(\eta_d = 93.78\%\) (Table 4). This confirms the bidirectional converter’s ability to efficiently manage energy flow out of the LiFePO4 battery pack, meeting the design goal of over 90% efficiency in both directions.

Table 4: Discharging Efficiency for LiFePO4 Battery Pack
Battery Voltage \(V_o\) (V) Discharging Current \(I_R\) (A) Output Voltage \(V_S\) (V) Load Current \(I_S\) (A) Efficiency \(\eta_d\) (%)
18.51 1.648 29.80 0.960 93.78

Protection features were also rigorously tested. Over-voltage protection was verified by manually increasing the battery voltage during charging until it reached the threshold of 24.5V, at which point the microcontroller immediately disabled the PWM signals and opened the input relay, halting operation to safeguard the LiFePO4 battery pack. Similarly, over-discharge protection was triggered when the battery voltage dropped below a preset level during discharging, preventing damage from deep discharge. These protections are critical for maintaining the health and safety of LiFePO4 battery packs, which are sensitive to voltage extremes.

In conclusion, the designed microcontroller-based charging and discharging circuit for LiFePO4 battery packs offers a robust and efficient solution for bidirectional energy management. By integrating a synchronous bidirectional DC-DC converter with a STC12C5A60S2 microcontroller, the system achieves precise current and voltage control, high efficiency exceeding 90% in both modes, and comprehensive protection mechanisms. The use of digital PI algorithms enables adaptive regulation, while the hardware design minimizes losses through synchronous switching and optimized component selection. Experimental results validate the circuit’s performance, with current control errors below 2% and efficiencies approaching 97% for charging and 94% for discharging. This design not only enhances the usability and lifespan of LiFePO4 battery packs but also provides a flexible platform for various applications, from renewable energy storage to electric mobility. Future work could focus on incorporating advanced battery management features, such as state-of-charge estimation and cell balancing, to further optimize the performance of LiFePO4 battery systems. Overall, this project underscores the importance of intelligent power electronics in harnessing the full potential of modern energy storage technologies like the LiFePO4 battery.

Scroll to Top