Voltage divider measuring volt correctly

The Rocketry Forum

Help Support The Rocketry Forum:

This site may earn a commission from merchant affiliate links, including eBay, Amazon, and others.

deepfreeze

Well-Known Member
TRF Supporter
Joined
Dec 3, 2022
Messages
55
Reaction score
1
Location
New Jersey, US
I have teensy 4.1 and i am connecting battery from outside as 12 V and connecting 3 resistors and getting the R1's volt but its not correct.

I draw the image and sharing with you.

So normally i will connect the battery to power teensy and i will just calculate the voltage of the battery from the teensy side.
But i have to test if it reads correctly. So i power teensy from the usb without any battery or anything. Then i read it. It gives me values 0.008...etc. It is not so weird i was thinking like okay then no voltage.

But then i connect the battery (usb is also plugged). Then i read the value 0.25..etc. This is weird for me. Why does it happen ?
I looked at the calculation from this website. It should be 0.0060 V.


Here is my code for voltage read, I dont think i am wrong here but i am missing something:
C:
void send_voltage_data(){

      int value;
    float volt;

    value = analogRead( VOLTAGE_PIN );

    volt = value * 5.0 / 1023.0;
    
    DynamicJsonDocument voltage_json(1024);
  voltage_json["CODE"] = "VOLTAGE_DATA";
  voltage_json["voltage"] = volt ;
  voltage_json["value"] = value ;

  serializeJson(voltage_json, Serial);
  Serial.println();
  delay(1000);
 
  }
 

Attachments

  • TEENSY.png
    TEENSY.png
    17.5 KB · Views: 0
That resistor divider is putting 11.99V onto the Teeny's pin 20 when the battery is at 12V. This could KILL the tennsy or at least pin 20.
You are reading the value of an ADC on pin20. The ADC's Ref and MAX Voltage is 5V.
The very low value may be due the the Tennsy's pin20 if fried or the ADC is simply overloaded and return a near zero value. What does it read when pin 20 is connected to a 1.5V battery call (AA battery).
If still near zero then the pin is probable dead.

To convert 12V down to 5V with a resistor divider and the first R = 1k OHm, then the R connect to ground needs to be 720 Ohm.
The divider equation is: Vout = Vin * (R1/(R1 + R2))
Where:
R1 = resistor from input 12V to ADC, pin 20
R2 = resistor from ADC, pin 20 to ground.
Vin is the 12V battery
Vout is the ADC input on pin 20.

Also output the value form the ADC, print(value). See if this value makes sense to verify ADC reading.

Last: A battery with nominal 12V may be higher at full charge. A tyical Laed Acid 12V battery can be 14.4 during charging and 13.5 when fully charged. In this case you need the divider to safely get the highest possible Voltage down the 5V into the Tennsy.
 
I read ADC from pin20 but when i measure the voltage with multimeter it shows me correct value. It shouldnt be 12 V directly going into the teensy. There is a resistor before it goes to pin20. Teensy is working okay. There are other devices conencted to Teensy and they all working okay.

I calculated the voltages from here: https://voltage-disturbance.com/engineering-calculators/voltage-divider-calculator-3-resistors/

So it should be safe. If it fried or something, i would smell it i think :D

I tried with 1.5 volt and it is showing the same value 0.008 still. But if pin is fried, why does it show 0.25 when i put 12V as battery. Btw my usb is still connected, is it something that effects maybe ?
 
Checked that calculator and got a 5.99V drop across each of the 1M Ohm resistor which totals 11.98V from pin 20 to ground. R1 in the calculator is the Resistor from pin 20 to the 12V Battery. it DROPS (Voltage across it) 0.06V which means the Voltage on pin 20 is 0.06V less than the battery's 12V or about 11.94V.

Remove the Tennsy and have those resistor wired. Then measure with your DVM the Voltages on the resistors to prove to yourself what the divider does.

NO 12V into the Tennsy. It is power by 5V so this is the MAXIMUM Voltage. Also, the ADC's Reference Voltage is 5V. Any Voltage higher will be an ADC overload which may simple be the maximum value 0x3ff.

Very possible that the pin is not totally fried but damaged and/or the ADC is damaged.
Does the Tennsy have another pin to an ADC? Try changing the code to use a different pin.

Is the code correctly setting up pin 20 to the ADC? You only posted the function that reads the Voltage.

This is not easy to troubleshoot through the forum so please be patient.
 
As Walt said your voltage divider circuit is wrong. 1K is in series with 2M doesn't divide the voltage much. Try ~500ohm below the 1K ohm resistor.
 
I'm sorry, but you're approaching this incorrectly. If you need a stable +5V on Pin 20, use a voltage reference IC that will eliminate the fluctuations caused by a varying battery voltage. Also when you are using a voltage divider, the current routed into Pin 20 can influence the divider voltage as it is approximating a third resistor in the circuit.
As John D. pointed out the divider circuit with the resister values listed will have about 11.994V at the node. A 14.5k resistor on the battery side and a 10k resistor on the low side will get you close to where you want to be, if you decide to continue using a voltage divider circuit.
 
Last edited:
I'm sorry, but you're approaching this incorrectly. If you need a stable +5V on Pin 20, use a voltage reference IC that will eliminate the fluctuations caused by a varying battery voltage. Also when you are using a voltage divider, the current routed into Pin 20 can influence the divider voltage as it is approximating a third resistor in the circuit.
Yes what is the function of pin 20?
 
Here is a tutorial on using the Teensy's ADC:
https://www.pjrc.com/teensy/adc.html
Do you have the ADC setup correctly?
Using Vcc as Reference, enable A6,

According to this pin out diagram:
https://www.pjrc.com/teensy/pinout.html
Pin 20 is ADC #6.
Does this diagram match the Teensy you have?


1671137103087.png
Here is a Voltage divider that may be easier to understand. 12V at top of R1 with Ground (0V) at bottom of R2. Between R1 and R2 is a Voltage between 12 & 0 depending on the ratio of the resistor values.
R3 is the internal resistance of ADC as Johnly posted.

You can think of R3 as the ADC.

One other consideration is the current the divider draws. Ohm's Law states this as I = E/R = 12/1720 = 6.9mA
This is low enough not to drain the battery quickly.
 
Last edited:
The Teensy probably has a resistor of its own in series with the ADC input voltage... you may need to take that into account with your calculations. It may also have an internal ADC voltage limit lower that Vcc. A better value for the voltage divider would be a 1K resistor to GND and a 22K resistor to your battery... that should put it under 1V to your ADC for any battery that you're likely to use. (Vout = Vin/23) If the Teensy's ADC can handle more than 1V, scale up the 22K resistor and change the voltage ratio accordingly. And as waltr said, you need to look at the current draw of your voltage divider... the 1K/22K resistors will draw Vbattery/23,000 Amps, or about 1/2 mA.
 
Hi @deepfreeze

I am not sure if you figured out your issues, but here are a few additional considerations... for you or future travelers.

1) The T4.1 has a 3.3v bus and is very sensitive. If you put anything higher than 3.3v across a GPIO pin (even momentarily) you will likely fry the pin and possibly the bus and/or other pins. The board will likely still work, but not that pin or other pins impacted.
2) If you truly have 12vdc (not a car battery that goes up to 14v) then you should use a 30K resistor on the + side and a 10K on the negative side or the equivalent proportion. This will divide 12v down to 3v, which is safe.
3) On the T4.1 you do not need to declare a PinMode for analog pins, but you do need to use analogRead(pin#) to get the values. The ADC will return 0-1024 for 0-3.3v on the pin.
4) Also, be aware that the T4.1 has an anomaly where directly energizing the pins (e.g., a voltage analog read) before the processor comes up to full power can result in some unusual behavior. Most of the time it is fine, but there are a lot of threads over on the teensy boards complaining of anomalies. I've switched over to external (I2C) ADCs in my T4.1 projects for this reason (and because they are way better than the stock T4.1 ADC).

-Mike
 
Hello guys sorry for the late response. I think i know where the problem was. The problem was me understanding the concept of voltage divider. as @waltr said when i calculate the voltage across the resistor, i thought it is the value i will read after the resistor but actually it should be 12V - 0.006 V so it is 11.96 or so. That's why i fried my pin i think.
 
Hi @deepfreeze

I am not sure if you figured out your issues, but here are a few additional considerations... for you or future travelers.

1) The T4.1 has a 3.3v bus and is very sensitive. If you put anything higher than 3.3v across a GPIO pin (even momentarily) you will likely fry the pin and possibly the bus and/or other pins. The board will likely still work, but not that pin or other pins impacted.
2) If you truly have 12vdc (not a car battery that goes up to 14v) then you should use a 30K resistor on the + side and a 10K on the negative side or the equivalent proportion. This will divide 12v down to 3v, which is safe.
3) On the T4.1 you do not need to declare a PinMode for analog pins, but you do need to use analogRead(pin#) to get the values. The ADC will return 0-1024 for 0-3.3v on the pin.
4) Also, be aware that the T4.1 has an anomaly where directly energizing the pins (e.g., a voltage analog read) before the processor comes up to full power can result in some unusual behavior. Most of the time it is fine, but there are a lot of threads over on the teensy boards complaining of anomalies. I've switched over to external (I2C) ADCs in my T4.1 projects for this reason (and because they are way better than the stock T4.1 ADC).

-Mike
Thank you it is very helpful.
 
Checked that calculator and got a 5.99V drop across each of the 1M Ohm resistor which totals 11.98V from pin 20 to ground. R1 in the calculator is the Resistor from pin 20 to the 12V Battery. it DROPS (Voltage across it) 0.06V which means the Voltage on pin 20 is 0.06V less than the battery's 12V or about 11.94V.

Remove the Tennsy and have those resistor wired. Then measure with your DVM the Voltages on the resistors to prove to yourself what the divider does.

NO 12V into the Tennsy. It is power by 5V so this is the MAXIMUM Voltage. Also, the ADC's Reference Voltage is 5V. Any Voltage higher will be an ADC overload which may simple be the maximum value 0x3ff.

Very possible that the pin is not totally fried but damaged and/or the ADC is damaged.
Does the Tennsy have another pin to an ADC? Try changing the code to use a different pin.

Is the code correctly setting up pin 20 to the ADC? You only posted the function that reads the Voltage.

This is not easy to troubleshoot through the forum so please be patient.
Thank you this solved my issue of understanding.
 
Hello guys sorry for the late response. I think i know where the problem was. The problem was me understanding the concept of voltage divider. as @waltr said when i calculate the voltage across the resistor, i thought it is the value i will read after the resistor but actually it should be 12V - 0.006 V so it is 11.96 or so. That's why i fried my pin i think.
In general, on-chip ADC's can't handle Vin > Vcc, and there may also be a separate reference voltage pin for the ADC to indicate "100%". Your processor may be different, though... some have a fixed maximum input voltage regardless of Vcc. Very few (if any...) processors can handle a Vin of 12V without frying the ADC... which is why you need the voltage divider in the first place.
 
Back
Top