STM32F4 ADC a bit off - Page 1
Forum section: electronics / general

author: Monostable
author_rank: Contributor
timestamp: August 24, 2015, 09:57:45 am
content: Hello all !After watching a lot of Dave's videos on the µSupply and on his Dummy constant current load, I finally decided to get my hands dirty.I did a small constant current load on a breadboard (the schematics are attached to the post).I had someSTM32F4 Discovery board(based on the STM32F407vg) laying around, which was perfect, they have mutiple ADC, and some nice timer able to generate some PWM.I choosed the value of 0.33? for the RSENSE because I wanted my microcontroller to read the value up to ~9Amps (the ADC goes up to 3V max).And the filter right after the PWM is just for test at the moment, but has a cut off frequency of 500Hz (the PWM has a frequency of 20kHz).And the issue is that the valuethe ADC gives me is a bit off from the actual value(which I measure using a Fluke 189) which is annoying specially when the load is taking 3 amps and the micro tells me "2.8 amps".So I though, I should increase the number of samples, and then apply an average filter (I get a value averaged every 8ms ~125 Hz)But the error remain.Also I configured the pin internally without any "pull up / pull down".Would it be useful if I added an OP Amp as a buffer between the ADC and the RSENSE ?TLDR : Have a shunt resistor getting 3 amps and the ADC connected to it give me a value close of it with an error.I'm a bit out of idea here, if you have any advice it would be great.

author: Ribster
author_rank: Frequent Contributor
timestamp: August 24, 2015, 10:31:03 am
content: Try adding an RC passive filter after your current sensing.The high frequency noise is passing right through.Also, you are depending on the internal clamping of the input.Add external clamping diodes for protection.

author: Brutte
author_rank: Frequent Contributor
timestamp: August 24, 2015, 10:34:39 am
content: I'd suggest verifying your ADC works as expected first. Connect (AVCC-AVSS)/2 to an analog IO and do some tests. Should return something close to 0x800. Mind AVCC is not VCC.As the next experiment I'd suggest connecting an analog IO to the second terminal of a 0R33 resistor. Does it return 0x000 at 9A? Unlikely.

author: Monostable
author_rank: Contributor
timestamp: August 24, 2015, 11:54:09 am
content: Quote from: Ribster on August 24, 2015, 10:31:03 amA good lead but I tried it, and it didn't changed much.I tried visualising the waveform on an oscilloscope, it seems completely flat (when I'm not modifying the current limiting)Quote from: Brutte on August 24, 2015, 10:34:39 amGood advice I just did that with my variable power supply, modifying the voltage, from 0 to 2.9V and well from 0 to 0.5V the error is small (<10mV max) and then it just goes crazy I put 1V and it display 1.03 (30mV !!!).So I think, I'll change the pin used, I'm also wondering if it could the Discovery board itself being faulty ?Also I think I should add that I'm using ADC1 of the STM32F4, on the pin PC2, at a resolution of 10bits

author: tszaboo
author_rank: Super Contributor
timestamp: August 24, 2015, 12:03:26 pm
content: Quote from: Monostable on August 24, 2015, 11:54:09 amhttp://www.st.com/web/en/resource/technical/document/datasheet/DM00037051.pdf+/- 2,5 LSB offset error+/-3 LSB gain error+/-2 LSB DNL error+/-3 LSB INL errorYou dont have a real reference voltage.You probably are not using 4 wire measurement when doing current measurement.I think the ADC works expected.

author: Ribster
author_rank: Frequent Contributor
timestamp: August 24, 2015, 12:25:37 pm
content: Quote from: Monostable on August 24, 2015, 11:54:09 amWhat i could suggest, is the use of a self-made devboard. That way you can lay out the tracks for the ADC at a correct way.Also, you can play with the voltage reference to the ADC.If that's too much time / trouble, i've found a nice board at digikey. (Also other vendors, because it is from MIKROE).http://www.digikey.fr/product-search/fr?keywords=1471-1021-NDHave a look.

author: Monostable
author_rank: Contributor
timestamp: August 24, 2015, 12:43:27 pm
content: Quote from: NANDBlog on August 24, 2015, 12:03:26 pmI've done some math on excel, and you're right.If I remember correctly the VREF is tied to the regulator of the Discovery Board, wouldn't that be enough ?For instance if I apply a 1.6V to the ADC and it gives me "555" playing with the errors on the LSB I do get something within the range of the measurement.Is there any ways to improve the measurement ? should I use an external ADC ?Quote from: Ribster on August 24, 2015, 12:25:37 pmLovely boards, but unfortunately I do not own an oven, nor the tools required to solder it properly.So I might have to go for something already ready to useI was thinking the maple leaf seems great and they're quite cheaphttp://fr.aliexpress.com/item/5PCS-LOT-leaflabs-Leaf-maple-mini-ARM-STM32-compatibility/1400682373.htmlI should add that I just want a measure precise at the mV level (+/-5mV).

author: tszaboo
author_rank: Super Contributor
timestamp: August 24, 2015, 01:01:23 pm
content: Quote from: Monostable on August 24, 2015, 12:43:27 pmWell, depends on the accuracy you are aiming for. I usually only use the internal 10-12 bit ADCs for checking supply voltages and temperatures. It really depends on the accuracy you are aiming for. Next step would be connecting it to a INA226 (for example), than you can read the current through I2C with some 0,1-0,2% accuracy.

author: FrankBuss
author_rank: Unknown
timestamp: August 24, 2015, 01:19:45 pm
content: Quote from: Monostable on August 24, 2015, 12:43:27 pmSee the user manual, page 33. It is nicely decoupled with a coil, resistor and capacitors, for a low-pass filter. This means it is short-term stable, but the voltage regulators might not be the best.QuoteDepends on how accurate you need it. I would at least add an external voltage reference and use this as VREF and VDDA+, or connect an external voltage reference to another ADC input. Then for one measure cycle first measure the external voltage reference, then your real signal and then you can calculate and eliminate the error. You have to measure always both, because VREF might drift with time.

author: hamster_nz
author_rank: Super Contributor
timestamp: August 24, 2015, 09:17:05 pm
content: Have you performed an ADC calibration? I was getting a DC offset of about 13/4095 without calibration.It is pretty easy to do...Code:ADC1->CR2 =.... other options ... | ADC_CR2_CAL | ADC_CR2_ADON;// Wait for a calibration to finishwhile(ADC1->CR2 & ADC_CR2_CAL);

author: Bruce Abbott
author_rank: Frequent Contributor
timestamp: August 24, 2015, 11:12:44 pm
content: Quote from: Monostable on August 24, 2015, 09:57:45 amAre you sure the resistor is accurate? 3A through 0.33 Ohms is 3W. The resistor may be getting hot and changing value. Also a considerable temperature gradient may occur which can generate voltage (thermocouple effect).To measure 9A you will need a much smaller sense resistor (eg. 0.01 Ohms). Of course this makes the voltage drop smaller, so you might need an op amp to amplify it.

author: Monostable
author_rank: Contributor
timestamp: August 25, 2015, 07:07:26 am
content: Quote from: Bruce Abbott on August 24, 2015, 11:12:44 pmI'm using three 1 Ohm resistors in parallele, all 3W. They do get hot though, nothing more than 60°C, I can still bear touching it for a few seconds (not really scientific I know)I checked them on a Fluke PM6304 (programmable RLC meter), and everything seems fine, but yeah I intend to use some small resistors in the future, right now I'm just using the part I can find close to me.Quote from: hamster_nz on August 24, 2015, 09:17:05 pmApparently the calibration is done automatically at the start on the F0 and F4 seriesST ForumsQuote from: FrankBuss on August 24, 2015, 01:19:45 pmGreat advice but if I measure VREF with the ADC wouldn't it return the max value all the time ?Quote from: NANDBlog on August 24, 2015, 01:01:23 pmI love this chip, it is so simple, thank you for the recommendation, I think I'm going to go with this solution, thanks a lot !

author: FrankBuss
author_rank: Unknown
timestamp: August 25, 2015, 09:33:09 am
content: Quote from: Monostable on August 25, 2015, 07:07:26 amYes, this was the solution where VREF is connected to the internal supply and you connect the external voltage reference to an ADC pin, so that you don't have to solder on the STM32F4 Discovery board. If you connect the external voltage reference to VREF, you don't need to measure it.

author: tszaboo
author_rank: Super Contributor
timestamp: August 25, 2015, 09:56:39 am
content: Quote from: Monostable on August 25, 2015, 07:07:26 am

author: Gabri74
author_rank: Regular Contributor
timestamp: September 10, 2015, 08:09:25 am
content: Try to increase the sampling time!We had similar problems in the past because the sampling capacitorin the ADC was not charged for enough time. We also had ghost readings from previous channelwhen doing multiple conversions in sequence.Maybe you can add a current buffer between the ADC and the device under measurement if you really need fast acquisitions.For example we needed to increase sampling time toADC_SampleTime_144Cyclesbecausewe observed that the minimum time to get a correct measure was 5us in out design:So (from the ADC chapter):sampling_time = (12 + Cycles) / fADC = (12 + 144) / 21MHz = 7.4 usfADC depends on your clock settings, obviously.Check the reference manual for more information.Hope it helps.