Example Code for Arduino-Measuring Battery

Last revision 2026/01/13

This guide demonstrates how to measure battery voltage using Arduino with a Firebeetle 2 M0, including hardware setup, software installation, and example code.

Hardware Preparation

Software Preparation

Other Preparation Work

Connect the Li-ion battery to FireBeetle M0.

NOTE:

Li-ion batteries are "maxed out" at 4.2V and stick around 3.7V for much of the battery life, then slowy sink down to 3.2V or so before the protection circuitry cuts it off. By measuring voltage, you can quickly tell when you're heading below 3.7V. To make this easy, we stuck a double-100k resistor divider on the li-ion port, and connected it to A2. Then you can get the battery electric capacity information.

Sample Code

Read battery voltage on A2 pin, convert and print via serial.

#define VBATPIN A2
void setup() {
  // put your setup code here, to run once:
}
void loop() {
  float measuredvbat = analogRead(VBATPIN);
  measuredvbat *= 2;    // we divided by 2, so multiply back 
  measuredvbat *= 3.3;  // Multiply by 3.3V, our reference voltage
  measuredvbat /= 1024; // convert to voltage
  Serial.print("VBat: " ); Serial.println(measuredvbat);
}

Result

The serial port outputs the battery voltage information.

Was this article helpful?

TOP