I just purchased a couple of SAMD21 boards and the first test is a short program to measure DC voltage of an ordinary AA battery. The results are very inconsistent and I have to fix this before I can move onto the real reason I bought the boards. The battery is connected between pin 6 and ground. Power comes from the USB-C connection.
My program is this;
/*
Simple way to read DC voltage up to 3.3V with the
Seeed XIAO SAMD21 development board.
*/
#define ANALOG_IN_PIN A6
int input_val;
float voltage;
void setup() {
analogReadResolution(12);
SerialUSB.begin(9600);
}
void loop() {
input_val = analogRead(ANALOG_IN_PIN);
voltage = (float)input_val/4096*3.3;
SerialUSB.print(voltage);
SerialUSB.println(" V");
delay(2500);
}
...and even though the battery voltage is around 1.4V, my output is all over the map. Here is a sample;
0.41 V
0.39 V
0.40 V
0.45 V
1.14 V
0.60 V
2.00 V
0.88 V
1.57 V
3.01 V
2.44 V
1.42 V
2.52 V
1.94 V
1.68 V
0.95 V
1.42 V
1.42 V
1.42 V
1.41 V
1.45 V
1.35 V
1.48 V
2.20 V
2.45 V
2.70 V
1.43 V
1.71 V
1.64 V