PIC ADC wrong value - Page 1
Forum section: electronics / general

author: raphaeldavidf
author_rank: Contributor
timestamp: August 10, 2015, 10:43:04 pm
content: Hi guys,I'm having a little trouble with the adc in my pic16f676. I'm trying to read the voltage of a li-on battery and it kinda works: when the battery has no load everything is right and I get the right value, but when I plug a load in the battery the number is of by quite a bit. I'm also measuring the battery current with a resistor, using the ADC, and it works ok. At first I thought that the problem was the wires and jumper cables connecting everything, so I measured the resistance of all the cables and tried to compensate via software, using the current.Here it is an example of my reading, with the compensation:(MULTIMETER / PIC ADC)3.59V / 3.3V3.59V / 3.4VThanks a lot!

author: Andy Watson
author_rank: Super Contributor
timestamp: August 10, 2015, 11:04:11 pm
content: Could you sketch-out a diagram of how the appropriate parts - it would make it easier to understand what is happening.

author: Maxlor
author_rank: Frequent Contributor
timestamp: August 10, 2015, 11:04:18 pm
content: A schematic of your circuit would help.

author: raphaeldavidf
author_rank: Contributor
timestamp: August 11, 2015, 12:01:05 am
content: Quote from: Andy Watson on August 10, 2015, 11:04:11 pmQuote from: Maxlor on August 10, 2015, 11:04:18 pmSorry the poor schematic, made in a hurryThe pic in the schematic is wrong, the right one is the pic16f676, but the number of pins is right.Pins 12 and 13: ADCPin 11: turn transistor onsense resistor: 1ohmthanks to you both for the help

author: tomlut
author_rank: Regular Contributor
timestamp: August 11, 2015, 01:14:17 am
content: Are you waiting the required settling time after selecting the ADC input? A small amount of time is required for the sampling capacitor to charge. You have a nice low impedance connection to the battery so it shouldn't take very long. Check the data sheet for the calculation (section 7.2).Are you using the battery for your Vcc supply? Is Vcc used as your ADC reference? Is it sagging under load?Also moving the load to the high side (collector) will ensure the transistor can turn on fully. Not required if your Vcc is 5v, but if it is 3.3v you might not be in the CE saturation region.

author: raphaeldavidf
author_rank: Contributor
timestamp: August 11, 2015, 01:27:06 am
content: Hey tomlut, thanks for the tipsQuote from: tomlut on August 11, 2015, 01:14:17 amI'm waiting 1us, and 1us for channel changing. I'll try increasing the time to see if it makes any difference.Quote from: tomlut on August 11, 2015, 01:14:17 amI'm using a power supply(5V), and the Vcc is the ADC ref. I didn't quite get what did u mean by sagging. Is it about the battery voltage drop when the transistor turn on?Quote from: tomlut on August 11, 2015, 01:14:17 amMy Vcc is 5V

author: tomlut
author_rank: Regular Contributor
timestamp: August 11, 2015, 01:37:05 am
content: Yes by sagging I meant the drop in battery voltage under load.Your readings will only be as accurate as your ADC reference. If the reference (Vcc) is changing as you switch the load you may get strange readings. Monitor the Vcc rail with an oscilloscope to make sure it's nice and stable. As you are not using the battery for your Vcc this is probably not the problem but worth checking.Also disregard the my other point about moving the load. 5v will switch it nicely.

author: raphaeldavidf
author_rank: Contributor
timestamp: August 11, 2015, 06:44:25 am
content: Quote from: tomlut on August 11, 2015, 01:37:05 amChecked the Vcc and it is pretty stable.I think I know the problem now: bad connection with the battery + resistance of the wires. Solved the problem by using a neodymium magnet to ensure a strong connection and used a software calibration to compensate. Now the error is low enough.The software calibration used is a linear equation, and i got the parameters by subtracting the value measured by the ADC from the value from my multimeter, then plot the Voltage(Vadc - Vmultimeter) vs current graph, and traced the linear trend.Thank you tomlut for all the help and tips