arduino / arduino/ArduinoCore-samd
(MKR Zero) Reading ADC_BATTERY via Serial Monitor without a battery attached returns the USB voltage
- Dominant language
- C
- Stars
- 502
- Forks
- 740
- PR merge metrics
- No merged PRs in 30d
Description
Hi there!
I found some unexpected behavior on an MKR Zero when measuring battery voltage with no battery attached. From the attached code, it seems ADC_BATTERY may be returning the USB voltage when no battery is attached. This is confusing because I would not expect a battery-voltage pin to be connected to the USB power supply at all.
I'm attempting to learn how ADC_BATTERY works, so I decided to use the attached code below. This code comes from an [online tutorial](https://www.hackster.io/Arduino_Genuino/mkr-zero-read-battery-voltage-4853ac) showing how to measure the battery voltage of an MKR Zero. I decided to test what would happen if I attempted to measure the voltage of a battery that does not exist. No other sources of power are attached to the board besides USB. There are no external components attached to the MKR Zero either.
Expected Behavior: Either (1) display 0 volts because no battery is present to power ADC_BATTERY, or (2) display an unstable, fluctuating voltage value because I would expect the battery-voltage pin is floating.
Actual Behavior: The program displays a consistent voltage of 4.20V to 4.21V. Voltage levels are shockingly precise given that no battery is attached, just the USB connection.
Sample Code (from the [linked tutorial](https://www.hackster.io/Arduino_Genuino/mkr-zero-read-battery-voltage-4853ac)):
```
/*
This code is based on the readAnalogVoltage tutorial. For a deeper explanation about the code in this tutorial please refer to it.
ReadBatteryVoltage
Reads the analog input connected to the battery output on a MKRZero or MKR1000, converts it to voltage, and prints the result to the serial monitor.
Graphical representation is available using serial plotter (Tools > Serial Plotter menu)
This example code is in the public domain.
*/
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(ADC_BATTERY);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 4.3V):
float voltage = sensorValue * (4.3 / 1023.0);
// print out the value you read:
Serial.print(voltage);
Serial.println("V");
}
```
Output:
Contributor guide
No contributing guide indexed for this repository
Research direction
Reproduce the behavior with the supplied sketch, using analogRead(ADC_BATTERY) on an MKR Zero connected only by USB. Then trace how ADC_BATTERY is defined and how the board routes that input, consulting the linked tutorial and board information. Done means explaining the stable 4.20–4.21V reading and either correcting the behavior or documenting the expected result.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- c
- Domain
- embedded-iot
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100