Voltage Divider Issue Teensy 4.1 -> SOLVED

fbartos

Member
Hello!

I created a voltage divider (schematic below) and tied it with an Arduino first, and I was able to get an accurate reading. The code was as follows:

float batteryVoltage;

void setup() {
Serial.begin(9600);
}

void loop() {
int sensorValue = analogRead(A0);
float batteryVoltage = (sensorValue * (5 / 1023.00))/0.090909; //0.090909 is (R2/R1+R2)

Serial.print(batteryVoltage,1); Serial.println("V");
Serial.println(sensorValue);
delay(500);
}

Tried the same code with my Teensy 4.1, just switched out the Analog pin to analogRead(A13);

On the serial monitor I got a reading of ~220-230 as the sensorValue and a voltage of 11.7-12.1V with nothing connected other than a cable to a laptop for serial monitoring. When I attached the ground and and analog cable to the board, with 14V going through the divider it would read 425 as the analog value and 22.8V.

Not sure what I'm doing wrong. Worked with one, not the other.
1734637329743.jpeg
 
float batteryVoltage = (sensorValue * (5 / 1023.00))/0.090909; //0.090909 is (R2/R1+R2)

This assumes the reference voltage is 5V, for Teensy 4.x it is 3.3V.

Also make sure you have Teensyduino 1.59 or higher installed; earlier versions don't automatically disable the pin keeper on the analog pins which can interfere with readings.
 
float batteryVoltage = (sensorValue * (5 / 1023.00))/0.090909; //0.090909 is (R2/R1+R2)

This assumes the reference voltage is 5V, for Teensy 4.x it is 3.3V.

Also make sure you have Teensyduino 1.59 or higher installed; earlier versions don't automatically disable the pin keeper on the analog pins which can interfere with readings.
I double checked and I do have version 1.59 installed. Before when I tried 3.3V, I didn't have anything connected it to the pin and it read ~7.8V and I thought it didn't work. Tried it now with the 3.3/1023 and got dead on readings with something connected to the pin. When I disconnect it shoots up to the ~7.8V reading which I assume is just noise from the board.

But it works and thats what matters because once the pin is soldered to, it should get noise. Thank you for your help!
 

Attachments

  • Screenshot 2024-12-19 at 2.51.08 PM.png
    Screenshot 2024-12-19 at 2.51.08 PM.png
    186.6 KB · Views: 18
Add a small capacitor (10nF to 100nF sort of value) between A13 and ground in parallel with the 100k resistor so that the ADC sees a low impedance - otherwise you could be picking up noise and see cross-talk from other analog inputs.
 
Back
Top