Issues with voltage measurement on Teensy 4.0

just4phil

New member
Hey Folks,

i have an issue that i cannot solve myself anymore.
I read lots of forum posts and tried the ideas but no solution until now :(

I have a teensy 4.0 project that gets power from a 12v lipo. I want to measure the voltage to avoid damaging the lipo.

Power Supply is as follows:
12v lipo-> |-> step-down-converter -> 5v -> Tensy 4.0
................|-> voltage divider -> Teensy ADC PIN

To measure the voltage i convert the 12v by a voltage divider so that there will be max. 3,3 volts at the teensy adc pin, because that should be the maximum the pins can handle.

i calculated with a max. voltage of 27v and have a 33 kOhm and a 4.7 kOhm resistor, so that there are max. 3.3v @27v at the adc pin. All that works fine. i checked with my voltage meter.

setup:
=====
pinMode(LIPO_PIN, INPUT);
volt = analogRead(LIPO_PIN);
voltageSmooth = map(volt, 0, 1023, 0, 270); // 270 shall represent 27.0 volts

loop:
=====
volt = analogRead(LIPO_PIN);
voltageSmooth = 0.7 * voltageSmooth + 0.3 * map(volt, 0, 1023, 0, 270);

The measurement happens only once every second!
"0.7 * voltageSmooth + 0.3 * ...." is used as a smoothing function.

As far as is understand i should get a measured value of 1023 when my input voltage is 27v and the pin has 3.3v. but there are 2 problems:
1) the measured value is fluctuating very strongly and is unstable.
2) i never measure something near 1023 @3.3v at the pin:
........27v input: volt: 870 - 930 instead of 1023 / voltageSmooth is around 210
........12v input: volt: 300 - 340 / voltageSmooth: around 165
........8v input: volt: 140 - 160 / voltageSmooth: around 150

Why does this not work as expected/intended?

Any help is appreciated !!
thx a lot!

bye
andre
 
map with integer arguments works in integers I think, I'd suggest using:

Code:
  volt = analogRead(LIPO_PIN) * 27.0 / 1024 ;  // float calculation
That may reduce quantization error.

I'd code the digital filter as
Code:
  voltageSmooth += 0.3 * (volt - voltageSmooth) ;
as in that form only one constant is needed and you can't make a mistake changing the values (the 0.3 and 0.7 in your
version must sum to 1 for the code to work, which is a brittle state of affairs).

If the value is definitely wrong, check the actual voltage at the analog pin - setting the pin to INPUT ought to switch off any
internal pullups or pulldowns, but its good to be sure the pin isn't affecting the voltage. From what I remember different
Teensys have different behaviour for pinMode in this area.
 
Try to measure on the Teensy pins directly.
I recently had similiar issues that I could trace down to a very bad breadboard and relatively high contact resistances.
 
What happens if this line is removed? : pinMode(LIPO_PIN, INPUT);

Analog use does its own setup.

The pins are set to keeper voltage with a hysteresis applied. Seems it was posted about the best way to disable this.
 
Try to measure on the Teensy pins directly.
I recently had similiar issues that I could trace down to a very bad breadboard and relatively high contact resistances.

Thx!
This seems to be true!
I had to Go up to 31v to see 3.3v at the Pin!

But it seems to not degrade linear when i Turn down the Input voltage.
But at least one issue ist solved :)
 
Back
Top