Voltage Divider non Linear issues - Page 1
Forum section: electronics / general

author: richcj10
author_rank: Unknown
timestamp: March 24, 2013, 06:47:41 pm
content: Hi,I have a PIC18LF46K22 and a voltage divider and when I use this piece of code:Code:result = ((ADC_Get_Sample(1) / 1024.0) * 3.3 )/ 0.3197278911564626I have a good return value at love voltages (EX: 4.2V I get 4.2) but not at high voltages (EX: 8V I get 6.1V).This is a battery powered device so I need as low power as I can get. Attached is the Circuit. Any Ideas?

author: Unknown
author_rank: Unknown
timestamp: March 24, 2013, 06:54:04 pm
content: 6.1 V * 0.3197278911564626 = 1.95V. My psychic debugging skills tell me that you use the internal voltage reference that is set to 2.048 V. Either use Vdd for reference (less accurate that the internal reference, especially if directly powered from battery) or change the division ratio so the voltage to the ADC input remains below 2 V for the input range you care about.

author: richcj10
author_rank: Unknown
timestamp: March 24, 2013, 07:01:26 pm
content: I was using a library provided for a compiler. I just noticed the note:QuoteI will try again....Thanks,Rick

author: Rufus
author_rank: Super Contributor
timestamp: March 24, 2013, 08:08:41 pm
content: Quote from: alm on March 24, 2013, 06:54:04 pmThe calculation requires a 3.3v reference (presumably VDD) and correct results at lower voltages indicate that is what is being used.Assuming my psychic interpretation of the grotty fragment of schematic is correct the problem is the soggy 3v zener across the divider.If you want to save a little battery don't use 3 floating point calculations to measure it while 1 and preferably none will do.

author: richcj10
author_rank: Unknown
timestamp: March 24, 2013, 08:42:00 pm
content: I think if I change R42 to 6K and R30 to 1.4K I will get under the 2v and 1.3mAThe Zener don't affect anything.

author: c4757p
author_rank: Super Contributor
timestamp: March 24, 2013, 08:46:08 pm
content: Quote from: Rufus on March 24, 2013, 08:08:41 pmI was just going to say this. Try changing your code so there's no floating point stuff in it and see how much smaller it magically becomes.

author: richcj10
author_rank: Unknown
timestamp: March 24, 2013, 08:48:50 pm
content: Quote from: c4757p on March 24, 2013, 08:46:08 pmI agree. I am prototyping the code so it is easy to change a value. I will optimize when it is time to be done.

author: richcj10
author_rank: Unknown
timestamp: March 26, 2013, 04:51:28 am
content: Updated code:Code:sub function vinread() as float' Soft_UART_Write("V")' Soft_UART_Write("I")' Soft_UART_Write(10)' Soft_UART_Write(13)bc1v = bc2vbc2v = bc3vbc3v = bc4vbc4v = bc5vbc5v = bc6vbc6v = bc7vbc7v = bc8vbc8v = ADC_Get_Sample(0)vinavg = (bc1v+bc2v+bc3v+bc4v+bc5v+bc6v+bc7v+bc8v)/8result = ((vinavg / 1024.0) * 2.048 )/ 0.2108922363847045end subI changed the Resistors to 6.81k (R1) and 1.82k (R2).I am still getting really bad non-linear issues.Any ideas?thanks,Rick

author: Unknown
author_rank: Unknown
timestamp: March 26, 2013, 08:45:30 am
content: If you measure the voltage at the ADC input pin, is it what you expect? What is the actual value of bc8v you get from the ADC?

author: amspire
author_rank: Super Contributor
timestamp: March 26, 2013, 09:50:23 am
content: Have you tried removing the capacitor and zener?If the capacitor is reversed, it will have leakage that might increase with voltage.If the zener is the wrong value, or if it has a big voltage drop below 1mA (low voltage zeners have soft limiting curves), it can cause non-linearity.

author: richcj10
author_rank: Unknown
timestamp: March 30, 2013, 11:57:18 pm
content: I just tested it w/o the cap and zener, no affect. WTF?

author: David_AVD
author_rank: Super Contributor
timestamp: March 31, 2013, 12:35:57 am
content: Did you measure the voltage at the actual micro pin as asked? This may shed light on what is happening.

author: richcj10
author_rank: Unknown
timestamp: March 31, 2013, 12:43:23 am
content: at 8v in i get 1.69v. This seams right.

author: nctnico
author_rank: Super Contributor
timestamp: March 31, 2013, 12:50:43 am
content: The zener will have too much leakage in these sort of constructions. Better use a MOV or TVS at the circuit's input (with a small series resistor). Besides that you'll probably need a >1nf capacitor at the uC's input pin to supply the current drawn by the sample & hold inside the uC. Without the capacitor you can probably see the sampling pulses with a scope .

author: poorchava
author_rank: Super Contributor
timestamp: March 31, 2013, 12:51:28 am
content: Look at datasheet for the microcontroller, page 464. Total ADC error is +\- 3LSB. It's kind of less shitty than other Microchip MCUs can be, but still shitty.

author: richcj10
author_rank: Unknown
timestamp: March 31, 2013, 02:52:27 am
content: Quote from: nctnico on March 31, 2013, 12:50:43 amzener has been removed. It at this moment not affecting any mesurments taken

author: VanitarNordic
author_rank: Frequent Contributor
timestamp: March 31, 2013, 07:37:42 am
content: You have to make one linear equation between input voltage and ADC. I put multi-turn potentiometer to adjust and acquire the value. You may use the Zener just to protect the PIN.

author: Bloch
author_rank: Unknown
timestamp: March 31, 2013, 08:13:48 am
content: Try look up the input impedance in the pic.if it is like Pic12f675 then that can be a problem for the divider.

author: nctnico
author_rank: Super Contributor
timestamp: March 31, 2013, 02:21:54 pm
content: Quote from: VanitarNordic on March 31, 2013, 07:37:42 amThis a bad idea. A generic zener has a leakage current of several hundred uA and needs close to 1mA to reach its zener voltage. Do the math with these currents to get the divider accurate within 1%.

author: codeboy2k
author_rank: Super Contributor
timestamp: March 31, 2013, 03:26:24 pm
content: most ADC's have a relatively low input impedance, and draw non-negligible current when charging the S-H cap. This charging current may be too much for your divider, and it droops, so you read low.Usually you need to buffer any input to an ADC, for this exact reason. You should find a spare op-amp in your design, or add one in, and simply buffer the ADC input with an op-amp voltage follower.If you use a 3.3V op-amp and tune the divider to keep your max voltage around 2V, then you can use the internal reference and don't need a 3.3V zener anymore at the ADC input, which is a bad idea anyways due to the leakage currents.

author: richcj10
author_rank: Unknown
timestamp: March 31, 2013, 05:15:01 pm
content: Let me scope it out and see what is happening.I use voltage dividers with AVR's all the time w/o issue. Go figure....

author: nctnico
author_rank: Super Contributor
timestamp: April 01, 2013, 02:41:57 pm
content: Microchip's A/D D/A chips are pretty crappy compared to the competition so its no surprise.

author: David_AVD
author_rank: Super Contributor
timestamp: April 01, 2013, 08:50:47 pm
content: If the voltage at the micro pin is correct (scaled), then the code must be bad. Could it be that you're only using the lower 8 bits of the result?