I have encountered a problem using the ADC library to measure pins 20 and 22 on a Teensy 3.6. I attempted to create two ADC instances that would have different settings -- namely, I wanted pin 22 to use the 3.3 V internal reference and pin 20 to use the 1.2 V reference. I found that this did not work as I expected. Can anyone suggest an alternative method to accomplish this? I cannot change pins at this point.
Code:
#include "Arduino.h"
#include "ADC.h"
ADC* adc1;
ADC* adc2;
elapsedMillis dt;
void setup()
{
delay(6000);
Serial.begin(115200);
delay(3000);
adc1 = new ADC();
adc2 = new ADC();
pinMode(20, INPUT);
pinMode(22, INPUT);
pinMode(23, OUTPUT);
pinMode(21, OUTPUT);
digitalWrite(23, HIGH);
digitalWrite(21, HIGH);
adc1->setAveraging(8);
adc1->setConversionSpeed(ADC_CONVERSION_SPEED::MED_SPEED);
adc1->setSamplingSpeed(ADC_SAMPLING_SPEED::MED_SPEED);
adc1->setResolution(16);
adc1->setReference(ADC_REFERENCE::REF_1V2);
adc2->setAveraging(8);
adc2->setConversionSpeed(ADC_CONVERSION_SPEED::MED_SPEED);
adc2->setSamplingSpeed(ADC_SAMPLING_SPEED::MED_SPEED);
adc2->setResolution(16);
adc2->setReference(ADC_REFERENCE::REF_3V3);
dt = 0;
}
void loop()
{
if (dt >= 5000) {
dt = 0;
Serial.print("Level Voltage: ");
Serial.print(adc1->analogRead(20)*1.2/adc1->getMaxValue(ADC_0), 4);
Serial.print(" V ; ");
Serial.print("Temp Voltage: ");
Serial.print(adc2->analogRead(22)*3.3/adc2->getMaxValue(ADC_0), 4);
Serial.print(" V");
Serial.println();
}
}