A Battery Management System (BMS) keeps a lithium battery pack safe and healthy. In this guide, you will learn how to design one around an STM32 microcontroller. We cover the requirements, voltage and current sensing, protection, firmware and testing, and we finish with a working 3-cell example.

⏱ 7 min read · 🔧 Includes: diagrams & photos, source code · 💬 Stuck? Ask us on WhatsApp

Advertisements

Working with electricity involves serious risk. Ensure you have the necessary skills and take proper safety precautions before attempting any electrical projects. Proceed at your own risk — the author assumes no responsibility for any damage, injury, or issues resulting from the use or misuse of the information provided.

All content on this website is original and protected by copyright. Please do not copy or reproduce content without permission. While most of the resources shared here are open-source and freely accessible for your learning and benefit, your respect for our intellectual effort is appreciated.

If you find our tutorials helpful, consider supporting us by purchasing related materials or sharing our work — it helps keep the content flowing.

Need help or have questions? Leave a comment below — the author is always happy to assist!

What is BMS?

BMS stands for Battery Management System. It is the electronics that monitors and protects a rechargeable battery pack. You will find a BMS in electric vehicles, solar storage, power banks and almost every lithium battery product.

Its main jobs are simple. First, it measures the voltage of each cell, the pack current and the temperature. Second, it disconnects the battery if any value goes outside safe limits. In addition, it balances the cells and reports the battery state to the user or the host system. As a result, a good BMS improves performance, extends battery life and prevents dangerous failures.

Designing a BMS with STM32
🧮 Free tool for this topic

Importance of BMS

To see why a BMS matters, look at the key jobs it does:

  1. Monitoring: the BMS measures voltage, current and temperature all the time. Therefore, it can spot problems early, before they damage the battery.
  2. Over-charge and over-discharge protection: lithium cells are damaged above about 4.2 V or below about 2.5–3.0 V. If a cell crosses these limits, the BMS disconnects the charger or the load.
  3. Cell balancing: in a series pack, cells drift apart over time. Without balancing, one cell hits full charge first while others are still low, so the pack loses capacity. The BMS bleeds a little charge from the highest cells to keep them level.
  4. Longer battery life: by keeping every cell inside its safe window and in balance, the BMS helps the pack last many more cycles.
  5. Safety: over-current, short-circuit and over-temperature protection prevent fires and damaged equipment.

In short, the BMS is what makes a lithium pack safe and reliable to use.

What is STM32?

STM32 is a family of 32-bit ARM Cortex-M microcontrollers from STMicroelectronics. You will find them in industrial controllers, consumer products and automotive systems.

STM32 chips offer good processing power, low power modes and many peripherals, such as timers, 12-bit ADCs, DACs, CAN, UART, I2C and SPI. They also come in many packages, from 20 to over 200 pins.

For development, you can use free tools such as STM32CubeIDE and STM32CubeMX, or commercial ones such as Keil MDK. Most projects are written in C or C++.

For a BMS, the fast ADC, the CAN interface and the low-power modes make STM32 a natural choice.

Designing a BMS with STM32

Designing a BMS using STM32

Designing a BMS with an STM32 follows these steps. First, define the requirements:

  1. Define the BMS requirements:
    • Battery chemistry, nominal voltage and capacity.
    • Number of cells in series and in parallel.
    • Maximum charge and discharge currents.
    • Required protections: over-voltage, under-voltage, over-current, short-circuit and over-temperature.
    • Communication needs, for example CAN, UART or RS-485 to a host or charger.
  2. Choose the STM32 microcontroller based on the BMS requirements:
    • Next, choose the microcontroller. Many STM32 families fit; an STM32F1, F4 or G4 is a common choice.
    • Count the ADC channels, GPIOs and communication interfaces you need, and pick a part with some spare.
  3. Design the BMS hardware:
    • Then, design the hardware, with the STM32 as the brain of the BMS.
    • Add voltage sensing for each cell and a current shunt with an amplifier for the pack current. For more than 3–4 cells, use a dedicated BMS front-end IC, such as the TI BQ76920/BQ76940 or the ADI LTC6811. It measures every cell accurately and handles balancing.
    • Add the power path: back-to-back MOSFETs for charge and discharge, a fuse, and fast short-circuit protection in hardware.
    • Connect the STM32 to the host over CAN, UART or I2C.
  4. Write the firmware:
    • After that, write the firmware in STM32CubeIDE.
    • Implement the charge logic for your chemistry, for example CC/CV charging of lithium cells via the charger.
    • Implement the protections, with clear trip limits, delays and hysteresis.
    • Implement the communication protocol for the host or charger.
  5. Test and debug:
    • Finally, test the BMS with a real battery pack, ideally behind a current-limited supply first.
    • Check that the voltage and current readings match a calibrated meter, and that every protection trips at the right point.
    • Fix any issues you find, then repeat the tests.
  6. Deploy the BMS:
    • Install the BMS in the final product, such as an e-bike, solar storage unit or portable device.
    • Keep monitoring it in the field, and update the firmware when needed.

Example

Here is example code for a simple 3-cell (3S) Li-ion monitor and protection board on an STM32F4. It uses CubeMX-generated ADC and DMA set-up:

/* 3S Li-ion monitor and protection example for STM32F4 (HAL).
   STM32CubeMX settings:
     ADC1: 12-bit, scan mode, continuous conversion, 5 regular ranks
           (IN0..IN4), 84-cycle sampling, DMA circular (half-word).
   Inputs:
     IN0..IN2 (PA0..PA2): resistor dividers from the cell taps B1+, B2+, B3+
     IN3 (PA3): current-sense amplifier output (shunt + amplifier, e.g. INA180)
     IN4 (PA4): NTC divider (10 kΩ NTC to GND, 10 kΩ to 3.3 V)
   Output:
     PB1: enables the protection MOSFET driver (1 = battery connected)      */
#include "main.h"
#include <math.h>

extern ADC_HandleTypeDef hadc1;

#define VREF       3.3f
#define ADC_FULL   4095.0f

/* (Rtop + Rbottom) / Rbottom of each tap divider. Calibrate with a multimeter. */
static const float TAP_RATIO[3] = { 2.0f, 4.0f, 6.0f };

#define AMP_V_PER_A    0.05f   /* 1 mΩ shunt x gain 50 = 50 mV per amp */
#define AMP_ZERO_V     0.0f    /* amplifier output at 0 A */

#define CELL_OV_V      4.20f   /* over-voltage trip, per cell */
#define CELL_UV_V      3.00f   /* under-voltage trip, per cell */
#define HYST_V         0.10f   /* must recover this far before reconnecting */
#define MAX_CURRENT_A  10.0f
#define MAX_TEMP_C     50.0f

static volatile uint16_t adc_raw[5];   /* filled continuously by DMA */

static float to_volts(uint16_t raw) { return raw * VREF / ADC_FULL; }

static float ntc_celsius(uint16_t raw)
{
  if (raw == 0 || raw >= 4095) return -273.0f;              /* open or shorted sensor */
  float r_ntc = 10000.0f * raw / (ADC_FULL - raw);           /* 10 kΩ pull-up */
  float inv_t = 1.0f / 298.15f + logf(r_ntc / 10000.0f) / 3950.0f;  /* B = 3950 K */
  return 1.0f / inv_t - 273.15f;
}

static void battery_connect(int on)
{
  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, on ? GPIO_PIN_SET : GPIO_PIN_RESET);
}

int main(void)
{
  float node[3], cell[3], current_a, temp_c;
  int tripped = 0;

  HAL_Init();
  SystemClock_Config();
  MX_GPIO_Init();
  MX_DMA_Init();
  MX_ADC1_Init();
  HAL_ADC_Start_DMA(&hadc1, (uint32_t *)adc_raw, 5);

  while (1) {
    /* Tap voltages measured to ground, then each cell = difference of two taps */
    for (int i = 0; i < 3; i++) node[i] = to_volts(adc_raw[i]) * TAP_RATIO[i];
    cell[0] = node[0];
    cell[1] = node[1] - node[0];
    cell[2] = node[2] - node[1];

    current_a = (to_volts(adc_raw[3]) - AMP_ZERO_V) / AMP_V_PER_A;
    temp_c    = ntc_celsius(adc_raw[4]);

    int fault = 0, all_ok = 1;
    for (int i = 0; i < 3; i++) {
      if (cell[i] > CELL_OV_V || cell[i] < CELL_UV_V) fault = 1;
      if (cell[i] > CELL_OV_V - HYST_V || cell[i] < CELL_UV_V + HYST_V) all_ok = 0;
    }
    if (current_a > MAX_CURRENT_A || temp_c > MAX_TEMP_C || temp_c < -20.0f) fault = 1;
    if (temp_c > MAX_TEMP_C - 5.0f) all_ok = 0;

    if (fault) tripped = 1;                 /* trip immediately */
    else if (tripped && all_ok) tripped = 0; /* reconnect only with margin */

    battery_connect(!tripped);
    HAL_Delay(50);                          /* 20 checks per second */
  }
}

How it works

The ADC scans five inputs continuously, and DMA copies the results to memory. Each cell tap goes through a resistor divider, so the code first calculates the tap voltages. Then it finds each cell voltage as the difference between two taps. Next, it converts the shunt amplifier output to amps and the NTC reading to °C. If any value is outside its limit, it switches the protection MOSFET off at once. It switches it back on only when every value has recovered with some margin (hysteresis).

Conclusion

Keep in mind that this is a learning example. Measuring stacked cells through dividers adds error that grows with the number of cells, and the dividers slowly drain the pack. Therefore, a real product should use a BMS front-end IC for cell measurement and balancing, and a hardware short-circuit trip that reacts in microseconds.

Also, calibrate each divider ratio, the current offset and the gain with a multimeter. The code assumes a 3.3 V ADC reference; if your board differs, change VREF.

Finally, a complete BMS also needs cell balancing, state-of-charge estimation and communication with a charger or host. You can add these step by step once the basic protection works.

Read more on:

Liked this article? Subscribe to our newsletter:

Loading

or,

Visit LabProjectsBD.com for more inspiring projects and tutorials.

Thank you!

Have a project in mind? LabProjectsBD designs power electronics, embedded firmware, IoT devices and production-ready PCBs for clients. Message us on WhatsApp, use the contact form, or see our services and portfolio.


MKDas

Mithun K. Das. B.Sc. in Electrical and Electronic Engineering (EEE) from KUET. Senior Embedded Systems Designer at a leading international company. Welcome to my personal blog! I share articles on various electronics topics, breaking them down into simple and easy-to-understand explanations, especially for beginners. My goal is to make learning electronics accessible and enjoyable for everyone. If you have any questions or need further assistance, feel free to reach out through the Contact Us page. Thank you for visiting, and happy learning!

8 Comments

juno · 17/04/2023 at 7:10 pm

good morning i would like to buy the bms how should i do it please

    MKDas · 17/04/2023 at 8:18 pm

    It’s opensourced for learning. You can make it yourself.

bengisu · 04/09/2023 at 8:06 pm

Thank you for this project. I have a question. How should I combine stm and control circuits? Should I use the I/O pins?

    MKDas · 04/09/2023 at 8:31 pm

    you can use STM stand alone with proper circuit mechanism. ADCs of course

chinmayee · 08/10/2023 at 10:14 pm

can i know what sensors (componets) are used in above project? and also this code have errors how can i resolve it?

    MKDas · 10/10/2023 at 8:38 pm

    This is a concept of work. You need to edit according to your need and then you can select components.

micro · 27/12/2023 at 2:23 am

Please put the circuit diagram

    MKDas · 28/12/2023 at 11:52 am

    its a concept. you can draw based on the concept.

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *