Author Topic: STM32F4 ADC a bit off  (Read 11585 times)

0 Members and 1 Guest are viewing this topic.

Offline MonostableTopic starter

  • Contributor
  • Posts: 18
  • Country: fr
STM32F4 ADC a bit off
« on: August 24, 2015, 09:57:45 am »
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. :scared:
I did a small constant current load on a breadboard (the schematics are attached to the post).
I had some STM32F4 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 value the 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.

 

Offline Ribster

  • Frequent Contributor
  • **
  • Posts: 250
  • Country: be
  • Electronics prototyper. Design. Prototype. Consult
    • Ash Labs
Re: STM32F4 ADC a bit off
« Reply #1 on: August 24, 2015, 10:31:03 am »
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.
www.ashlabs.be
Design and manufacturing of embedded hard- and software
 

Offline Brutte

  • Frequent Contributor
  • **
  • Posts: 614
Re: STM32F4 ADC a bit off
« Reply #2 on: August 24, 2015, 10:34:39 am »
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.
 

Offline MonostableTopic starter

  • Contributor
  • Posts: 18
  • Country: fr
Re: STM32F4 ADC a bit off
« Reply #3 on: August 24, 2015, 11:54:09 am »
Try adding an RC passive filter after your current sensing.
The high frequency noise is passing right through.
A 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)

I'd suggest verifying your ADC works as expected first.
Good 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
 

Offline tszaboo

  • Super Contributor
  • ***
  • Posts: 8560
  • Country: nl
  • Current job: ATEX product design
Re: STM32F4 ADC a bit off
« Reply #4 on: August 24, 2015, 12:03:26 pm »
Try adding an RC passive filter after your current sensing.
The high frequency noise is passing right through.
A 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)

I'd suggest verifying your ADC works as expected first.

Good 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
http://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 error
You dont have a real reference voltage.
You probably are not using 4 wire measurement when doing current measurement.
I think the ADC works expected.
 

Offline Ribster

  • Frequent Contributor
  • **
  • Posts: 250
  • Country: be
  • Electronics prototyper. Design. Prototype. Consult
    • Ash Labs
Re: STM32F4 ADC a bit off
« Reply #5 on: August 24, 2015, 12:25:37 pm »
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

What 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-ND
Have a look.
www.ashlabs.be
Design and manufacturing of embedded hard- and software
 

Offline MonostableTopic starter

  • Contributor
  • Posts: 18
  • Country: fr
Re: STM32F4 ADC a bit off
« Reply #6 on: August 24, 2015, 12:43:27 pm »
+/- 2,5 LSB offset error
+/-3 LSB gain error
+/-2 LSB DNL error
+/-3 LSB INL error
I'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 ?

What 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-ND
Have a look.
Lovely 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 use
I was thinking the maple leaf seems great and they're quite cheap  :D http://fr.aliexpress.com/item/5PCS-LOT-leaflabs-Leaf-maple-mini-ARM-STM32-compatibility/1400682373.html

I should add that I just want a measure precise at the mV level (+/-5mV).
 

Offline tszaboo

  • Super Contributor
  • ***
  • Posts: 8560
  • Country: nl
  • Current job: ATEX product design
Re: STM32F4 ADC a bit off
« Reply #7 on: August 24, 2015, 01:01:23 pm »
+/- 2,5 LSB offset error
+/-3 LSB gain error
+/-2 LSB DNL error
+/-3 LSB INL error
I'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 ?

What 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-ND
Have a look.
Lovely 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 use
I was thinking the maple leaf seems great and they're quite cheap  :D http://fr.aliexpress.com/item/5PCS-LOT-leaflabs-Leaf-maple-mini-ARM-STM32-compatibility/1400682373.html

I should add that I just want a measure precise at the mV level (+/-5mV).
Well, 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.
 

Offline FrankBuss

  • Supporter
  • ****
  • Posts: 2379
  • Country: de
    • Frank Buss
Re: STM32F4 ADC a bit off
« Reply #8 on: August 24, 2015, 01:19:45 pm »
I'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.
See 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.
Quote
Is there any ways to improve the measurement ? should I use an external ADC ?
Depends 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.
So Long, and Thanks for All the Fish
Electronics, hiking, retro-computing, electronic music etc.: https://www.youtube.com/c/FrankBussProgrammer
 

Offline hamster_nz

  • Super Contributor
  • ***
  • Posts: 2829
  • Country: nz
Re: STM32F4 ADC a bit off
« Reply #9 on: August 24, 2015, 09:17:05 pm »
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: [Select]
   ADC1->CR2 =.... other options ... | ADC_CR2_CAL  | ADC_CR2_ADON;

   // Wait for a calibration to finish
   while(ADC1->CR2 & ADC_CR2_CAL)
      ;
Gaze not into the abyss, lest you become recognized as an abyss domain expert, and they expect you keep gazing into the damn thing.
 

Offline Bruce Abbott

  • Frequent Contributor
  • **
  • Posts: 628
  • Country: nz
    • Bruce Abbott's R/C Models and Electronics
Re: STM32F4 ADC a bit off
« Reply #10 on: August 24, 2015, 11:12:44 pm »
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 issue is that the value the 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".
Are 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.
 
 

Offline MonostableTopic starter

  • Contributor
  • Posts: 18
  • Country: fr
Re: STM32F4 ADC a bit off
« Reply #11 on: August 25, 2015, 07:07:26 am »
Are 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).
I'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 ;D)
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.

Have you performed an ADC calibration?
Apparently the calibration is done automatically at the start on the F0 and F4 series
ST Forums

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.
Great advice but if I measure VREF with the ADC wouldn't it return the max value all the time ?

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.
I 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 ! :)

 

Offline FrankBuss

  • Supporter
  • ****
  • Posts: 2379
  • Country: de
    • Frank Buss
Re: STM32F4 ADC a bit off
« Reply #12 on: August 25, 2015, 09:33:09 am »
Great advice but if I measure VREF with the ADC wouldn't it return the max value all the time ?
Yes, 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.
So Long, and Thanks for All the Fish
Electronics, hiking, retro-computing, electronic music etc.: https://www.youtube.com/c/FrankBussProgrammer
 

Offline tszaboo

  • Super Contributor
  • ***
  • Posts: 8560
  • Country: nl
  • Current job: ATEX product design
Re: STM32F4 ADC a bit off
« Reply #13 on: August 25, 2015, 09:56:39 am »
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.
I 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 ! :)
:-+
 

Offline Gabri74

  • Regular Contributor
  • *
  • Posts: 112
  • Country: it
Re: STM32F4 ADC a bit off
« Reply #14 on: September 10, 2015, 08:09:25 am »
Try to increase the sampling time!
We had similar problems in the past because the sampling capacitor
in the ADC was not charged for enough time. We also had ghost readings from previous channel
when 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 to ADC_SampleTime_144Cycles because
we 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 us

fADC depends on your clock settings, obviously.

Check the reference manual for more information.

Hope it helps.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf