I'm using this ADC library on a Teensy 3.1, but having issues with getting information from A14. Has anyone else experienced this?
I have A14 grounded, so I'm expecting to see a value somewhere near 0, but I'm receiving a negative value and an error flag.
I ran across this issue using ADC.h in another sketch, but all other analog pins I am using are returning valid values except for A14.
When I use adc->analogRead(A14), I get an invalid result, which also happens when I run the example sketch. Using Arduino's standard analogRead(A14), however, I receive a valid, reasonable value.
To test this, I loaded up the example sketch bundled with the ADC library, changed the int values for readPin and readPin2 to A14 like so:
Code:
const int readPin = A14; // ADC0
const int readPin2 = A14; // ADC1
and then ran the sketch. I receive serial output like this:
Code:
Pin: 40, value ADC0: -56.4102564
Pin: 40, value ADC1: -3.5248340
ADC1 error flags: 0x10
Comparison error in ADC1
According to ADC_Module.h, the 0x10 error flag is defined as ADC_ERROR_CALIB. But, I'm not sure how the calibration is invalid, because it returns sane values for all other ADC pins I tested it on. This also happens in the example sketch.
To further test this, I put together the following code, which also produces a wonky value from A14:
Code:
/*
ADC library testing. Comparing adc->analogRead(A14) to Arduino's analogRead(A14)
*/
#include <ADC.h>
ADC *adc = new ADC(); // adc object
void setup() {
pinMode(A14, INPUT); //pin 23 single ended
Serial.begin(9600);
adc->setAveraging(1); // set number of averages
adc->setResolution(12); // set bits of resolution
adc->setConversionSpeed(ADC_HIGH_SPEED); // change the conversion speed
adc->setSamplingSpeed(ADC_HIGH_SPEED); // change the sampling speed
// always call the compare functions after changing the resolution!
adc->enableCompare(1.0/3.3*adc->getMaxValue(ADC_1), 0, ADC_1); // measurement will be ready if value < 1.0V
adc->enableCompareRange(1.0*adc->getMaxValue(ADC_1)/3.3, 2.0*adc->getMaxValue(ADC_1)/3.3, 0, 1, ADC_1); // ready if value lies out of [1.0,2.0] V
Serial.println("End setup");
}
int value;
int value2;
void loop() {
// using ADC library
value = adc->analogRead(A14, ADC_0); // read a new value, will return ADC_ERROR_VALUE if the comparison is false.
// using Arduino's built in function
value2 = analogRead(A14);
Serial.print("adc->analogRead() result: ");
Serial.print(value);
Serial.print(", voltage: ");
Serial.println(value*3.3/adc->getMaxValue(ADC_0), DEC);
Serial.print("Arduino analogRead() result: ");
Serial.print(value2);
Serial.print(", voltage: ");
Serial.println(value2*3.3/adc->getMaxValue(ADC_0), DEC);
delay(500);
}
And the results look something like this:
Code:
adc->analogRead() result: -70000, voltage: -56.4102564
Arduino analogRead() result: 9, voltage: 0.0072527
adc->analogRead() result: -70000, voltage: -56.4102564
Arduino analogRead() result: 9, voltage: 0.0072527
adc->analogRead() result: -70000, voltage: -56.4102564
Arduino analogRead() result: 10, voltage: 0.0080586
But, in this sketch, I'm testing both adc->analogRead() and the regular Arduino analogRead() function. Arduino's function returns sane values, whereas the adc->analogRead() returns -70000, probably due to the calibration error I mentioned earlier.
Is there a trick I'm missing to be able to use the ADC library on A14, or is this a bug?