Example Code for Arduino - Battery Voltage Measurement

This example demonstrates how to measure the FireBeetle 2 ESP32-C6’s battery voltage.

Hardware Preparation

Software Preparation

Sample Code

void setup() {
  // Initialize serial communication at 115200 bits per second:
  Serial.begin(115200);

  // Set ADC resolution to 12 bits (0–4095)
  analogReadResolution(12);
}

void loop() {
  // Read raw ADC value on pin 0
  int analogValue = analogRead(0);
  int analogVolts = analogReadMilliVolts(0);

  // Print raw ADC value
  Serial.print("ADC analog value = ");
  Serial.println(analogValue);

  // Print measured voltage (in millivolts)
  Serial.print("ADC millivolts value = ");
  Serial.print(analogVolts);
  Serial.println(" mV");

  // Apply correction coefficient based on hardware voltage divider
  Serial.print("BAT millivolts value = ");
  Serial.print(analogVolts * 2);
  Serial.println(" mV");

  Serial.println("--------------");
  delay(500);
}

Was this article helpful?

TOP