Inconsistent AnalogRead results with SAMD21

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() {

void loop() {
  input_val = analogRead(ANALOG_IN_PIN);  
  voltage = (float)input_val/4096*3.3;
  SerialUSB.println(" V");

...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

I have tried your sketch and can measure the battery voltage without any problem.
Can you stably measure the voltage on the 3.3V pin or the GND pin?

After some testing, I found out that the connections to my 1.5V test battery weren’t reliable. I solved that problem and now the measurements are accurate and consistent.

Your code is properly configured to read analog voltage on pin 6, and that you are using the correct units and scaling factors for your measurements.