Shure, here's the sketch being used (a modified version of a logger-example by Jonathan Davies at github), The part of interest is the ADC enumerations at the very end of setup {}, with the Vref ones being temporarily disabled in this case.
/*
Simple Logger using internal RTC for Arduino Zero
Created by: Jonathan DAvies
Date: 21 Oct 2015
Version: 0.1
*/
#include <SPI.h>
#include <SD.h>
#include <RTCZero.h>
#define cardSelect 4 // Set the pins used
//#define VBATPIN A7 // Battery Voltage on Pin A7
#define SENSOR1 A1 // Sensor on Pin A1
#define SENSOR2 A2 // Sensor on Pin A2
#define SENSOR3 A3 // Sensor on Pin A3
File logfile; // Create file object
RTCZero rtc; // Create RTC object - strangely setting time doesn't work. Output is always starting at 12:00??
/* Change these values to set the current initial time */
const byte hours = 07;
const byte minutes = 30;
const byte seconds = 0;
/* Change these values to set the current initial date */
const byte day = 04;
const byte month = 07;
const byte year = 16;
////////////// Setup ///////////////////
void setup() {
rtc.begin(); // Start the RTC
// while (! Serial); // Wait until Serial is ready
// Serial.begin(115200);
// Serial.println("\r\nAnalog logger test");
pinMode(13, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(cardSelect)) {
Serial.println("Card init. failed!");
error(2);
}
char filename[15];
strcpy(filename, "ANALOG00.CSV");
for (uint8_t i = 0; i < 100; i++) {
filename[6] = '0' + i/10;
filename[7] = '0' + i%10;
// create if does not exist, do not open existing, write, sync after write
if (! SD.exists(filename)) {
break;
}
}
logfile = SD.open(filename, FILE_WRITE);
if( ! logfile ) {
Serial.print("Couldnt create ");
Serial.println(filename);
error(3);
}
Serial.print("Writing to ");
Serial.println(filename);
pinMode(13, OUTPUT);
pinMode(8, OUTPUT);
Serial.println("Ready!");
ADC->INPUTCTRL.bit.GAIN = ADC_INPUTCTRL_GAIN_1X_Val; // Gain Factor Selection, 1x,2x,4x,8x,16x
//ADC->REFCTRL.bit.REFSEL = ADC_REFCTRL_REFSEL_INTVCC0_Val; // 1/1.48 VDDANA = 1/1.48* 3V3 = 2.2297
//ADC->REFCTRL.bit.REFSEL = ADC_REFCTRL_REFSEL_INT1V_Val; // 1.0V voltage reference
analogReadResolution(12); // change the resolution to 12 bits
}
uint8_t i=0;
/////////////// Loop //////////////////
void loop() {
digitalWrite(8, HIGH);
//int 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
*/
int measure1 = analogRead(SENSOR1);
int measure2 = analogRead(SENSOR2);
int measure3 = analogRead(SENSOR3);
// Print RTC Time
Serial.print(rtc.getHours());
Serial.print(":");
Serial.print(rtc.getMinutes());
Serial.print(":");
Serial.print(rtc.getSeconds());
Serial.println(",");
//Serial.print("Battery:");
//Serial.println(measuredvbat); // Print battery voltage
Serial.print("Sensor1:");
Serial.println(measure1); // Print Sensor1 measurement
Serial.print("Sensor2:");
Serial.println(measure2); // Print Sensor2 measurement
Serial.print("Sensor3:");
Serial.println(measure3); // Print Sensor3 measurement
// Print Data to SD card
logfile.print(rtc.getHours());
logfile.print(":");
logfile.print(rtc.getMinutes());
logfile.print(":");
logfile.print(rtc.getSeconds());
//logfile.print(", ");
//logfile.println(measuredvbat); // Print battery voltage
logfile.print(", ");
logfile.println(measure1); // Print Sensor1 measurement
logfile.print(", ");
logfile.println(measure2); // Print Sensor2 measurement
logfile.print(", ");
logfile.println(measure3); // Print Sensor3 measurement
logfile.flush();
digitalWrite(8, LOW);
delay(5000);
}
/////////////// Functions //////////////////
// blink out an error code
void error(uint8_t errno) {
while(1) {
uint8_t i;
for (i=0; i<errno; i++) {
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
delay(100);
}
for (i=errno; i<10; i++) {
delay(200);
}
}
}
I'd like to correctly understand how the gain and Vref modes alter the analog-read-value.
Especially at the low end: Is it normal that at a steady 2.5mV input, readings via SAMD21 differ in a range from 2-8 (out of 4096). How stable/reliable can a reading be expected to be?
I just tried another time with the following results, all being based at an almost steady 2.5mV input signal:
Readings at:
gain 1x, VRef 3.3V: vary between 2-7 (1.61-5.64mV)
gain 8x, VRef 3.3V: vary between 0-29 (0-2.92mV)
gain 1x, VRef internal 1V: between 0-7 with mostly 0 (0-1.71mV)
gain 1x, VRef internal 2.23V: between 7-12 (3,81-6.53mV)
-It seems as gain is doing fine for higher readings (>100mV), but on the lower end the insignificance of readings is amplified as well.
-On the other hand, altering the internal VRef behaves similar: proportionally raising higher inputs while the lower ones seem rather strangely affected.
This seems to be the bottleneck to my case, as I was hoping both, another internal Vref and gain mode would help me to get reliable readings.
BTW, open inputs at the ADC are always measured around half the 3.3VRef, how is that?