ADC voltage measurements on voltage divider wrong and jumping on Teensy 4.1
Hi,
I have a Teensy 4.1 connected to my PC via USB. With its ADC I want to measure the voltage of an external power supply (for this test I am using a SK1730SL3A lab PSU so I can vary the voltage). I want to be able to measure voltages from 0-15 VDC, so I have selected the following resistor values for my voltage divider. R1=470kOhm, R2=100kOhm. If I do the math correct, 0.0V ext. PSU voltage will then be 0.0V (ADC raw value=0) on the Teensy ADC input pin, and 18.74V PSU voltage would be the highest voltage that could be measured with that circuit, which should be around 3.3V (ADC raw value=1023) at the Teensy ADC input. I want to measure 0-15VDC, so that should be fine.
So to make sure my voltage divider works correctly, I probe with a multimeter the voltage divider output as I sweep the external PSU voltage from 0-18.74V and it indeed translates to 0-3.3V as expected. A quick sanity check about linearity shows that 12V input=2.09V output and 3.3V input=0.58V at the voltage divider output, so it works fine.
Now I am ready to read this voltage with the Teensy, so I connect the output of the voltage divider to my Teensy ADC input (A2, which is pin 16).
I run the following code to read the raw ADC value (and from that calculated PSU input voltage) and print that out to the serial port:
I watch the serial console output, which prints every 100ms an update of the currently measured ADC raw value as well as the calculated PSU input voltage.
Now the weird thing happens: With the Teensy ADC input now connected to the voltage divider, the output voltage of the voltage divider has changed. 12.0V PSU input voltage is now 2.52V at the voltage divider output (it should be 2.09V). 3.3V PSU input voltage is now 0.36V on the voltage divider output (it should be 0.58V). The teensys displayed values are wrong accordingly.
Another interesting thing is when ramping up the PSU voltage from 0-18.7V that when the ADC raw value reaches 497 it instantly jumps to 879. Probing with my multimeter the voltage on the voltage divider output confirms that jump, its also visible there. When ramping down from 18.7-0.0V PSU voltage there is a jump at ADC raw value 531, which then jumps down instantly to 160 (again multimeter confirms the voltage at the output of the voltage divider really jumps).
What is happening here? How does connecting the Teensy ADC pin to the voltage divider output mess around this much with the voltage divider output voltage and cause these weird jumps? Did I select wrong voltage divider resistor values? I have tried other ADC pins on the Teensy and its the same problem. Probing with my multimeter the teensy ADC input pin when not connected to anything measures ~3.23V, I dont know if that is to be expected.
Any suggestions would be very welcome.
Hi,
I have a Teensy 4.1 connected to my PC via USB. With its ADC I want to measure the voltage of an external power supply (for this test I am using a SK1730SL3A lab PSU so I can vary the voltage). I want to be able to measure voltages from 0-15 VDC, so I have selected the following resistor values for my voltage divider. R1=470kOhm, R2=100kOhm. If I do the math correct, 0.0V ext. PSU voltage will then be 0.0V (ADC raw value=0) on the Teensy ADC input pin, and 18.74V PSU voltage would be the highest voltage that could be measured with that circuit, which should be around 3.3V (ADC raw value=1023) at the Teensy ADC input. I want to measure 0-15VDC, so that should be fine.
So to make sure my voltage divider works correctly, I probe with a multimeter the voltage divider output as I sweep the external PSU voltage from 0-18.74V and it indeed translates to 0-3.3V as expected. A quick sanity check about linearity shows that 12V input=2.09V output and 3.3V input=0.58V at the voltage divider output, so it works fine.
Now I am ready to read this voltage with the Teensy, so I connect the output of the voltage divider to my Teensy ADC input (A2, which is pin 16).
I run the following code to read the raw ADC value (and from that calculated PSU input voltage) and print that out to the serial port:
Code:
// Analog inputs:
const int analogInPin_PSU_VOLTAGE = A2; // Analog input pin A2 is pin 16 on PCB
const float adc_scaling = 3.3 / 1024.0; // TODO: divide by 1023 or 1024 ? // TODO: use measured ~3.28V here instead?
// Variables
float psu_voltage = 0.0;
void setup() {
// init serial comms at 9600 bps
Serial.begin(9600);
}
void loop() {
if (Serial.available()) {
float psu_voltage = readADC(analogInPin_PSU_VOLTAGE);
Serial.print("PSU voltage: ");
Serial.println(psu_voltage);
delay(100); // Sample every 100ms
}
}
float readADC(int analogPin){
int adc_value;
float adc_voltage;
float measured_voltage;
adc_value = analogRead(analogPin);
Serial.print("RAW ADC value: ");
Serial.println(adc_value);
adc_voltage = adc_value * adc_scaling;
measured_voltage = (adc_voltage * (470000.0+100000.0)) / 100000.0;
return measured_voltage;
}
I watch the serial console output, which prints every 100ms an update of the currently measured ADC raw value as well as the calculated PSU input voltage.
Now the weird thing happens: With the Teensy ADC input now connected to the voltage divider, the output voltage of the voltage divider has changed. 12.0V PSU input voltage is now 2.52V at the voltage divider output (it should be 2.09V). 3.3V PSU input voltage is now 0.36V on the voltage divider output (it should be 0.58V). The teensys displayed values are wrong accordingly.
Another interesting thing is when ramping up the PSU voltage from 0-18.7V that when the ADC raw value reaches 497 it instantly jumps to 879. Probing with my multimeter the voltage on the voltage divider output confirms that jump, its also visible there. When ramping down from 18.7-0.0V PSU voltage there is a jump at ADC raw value 531, which then jumps down instantly to 160 (again multimeter confirms the voltage at the output of the voltage divider really jumps).
What is happening here? How does connecting the Teensy ADC pin to the voltage divider output mess around this much with the voltage divider output voltage and cause these weird jumps? Did I select wrong voltage divider resistor values? I have tried other ADC pins on the Teensy and its the same problem. Probing with my multimeter the teensy ADC input pin when not connected to anything measures ~3.23V, I dont know if that is to be expected.
Any suggestions would be very welcome.
Last edited: