stylesuxx / Oversample

Oversample Arduinos ADC to get resolutions up to 16 Bit.
16 stars 4 forks source link

ADC should operate between 50-200kHz for precise results #2

Open Fuzzyma opened 6 years ago

Fuzzyma commented 6 years ago

In your readme you state, that you can safely set the prescaler to a value of 16 to get a cock of 1MHz. However in the datasheet is stated:

By default, the successive approximation circuitry requires an input clock frequency between 50kHz and >200kHz. If a lower resolution than 10 bits is needed, the input clock frequency to the ADC can be as high >as 1000kHz to get a higher sample rate.

So only if you want bad results (like 8 bit bad) you should go with 1MHz. With oversampling you get back the bits you just lost.

However I can see in your projects that you indeed got more precision with prescaling - so the question is: can it be used anyway?

stylesuxx commented 6 years ago

This is a very interesting point you bring up here regarding the clock. Unfortunately the project I was doing this research for "died" and I never came around to do the comparisons with the higher resolution ADC's - so I would absolutely not rely on those measurements without doing the according comparisons.

For me it "seemed" to work, but again, had a bitter taste since I never did the comparisons.

If you have any insight to provide, I'll be more than happy to merge any findings you have.

Can you tell me about the application you are intending to use it for, or is it just general interest?

Fuzzyma commented 6 years ago

We use the controllino in our company to do lots of measurements in our devices. Its always nice to be more precide so we looked into the oversampling stuff.

I just tried a sketch to test all those prescalers and it seems like the values are indeed stable when used with a prescaler of 16. I am not sure why the datasheet would suggest otherwise.

Here is the output of the sample:

prescaler    5V     0V      2.5V
  2         992    391       455
  2         992    391       455
  2         992    384       455
  2         992    384       455
  2         992    391       455
  2         992    384       455
  2         992    384       448
  2         992    384       455
  2         992    384       455
  2         992    391       455
  2         992    384       455
  2         992    384       455
  2         992    391       455
  2         992    384       455
  2         992    384       455
  2         992    391       455
  2         992    384       455
  2         992    384       448
  2         992    384       455
  2         992    384       455
  2         992    391       455
  4        1016    224       487
  4        1016    312       487
  4        1016    224       487
  4        1016    312       487
  4        1016    224       487
  4        1016    312       487
  4        1016    312       487
  4        1016    312       487
  4        1016    312       487
  4        1016    224       483
  4        1016    312       487
  4        1016    224       487
  4        1016    312       487
  4        1016    312       487
  4        1016    224       487
  4        1016    312       487
  4        1016    224       487
  4        1016    312       487
  4        1016    312       487
  4        1016    312       487
  4        1016    312       487
  8        1023     92       504
  8        1022    143       504
  8        1023     46       508
  8        1022     46       508
  8        1022    143       504
  8        1022     46       508
  8        1023     46       508
  8        1022    143       502
  8        1022     46       508
  8        1022    143       504
  8        1023     65       504
  8        1023    115       508
  8        1021     46       508
  8        1022     46       508
  8        1023     46       508
  8        1022     46       508
  8        1022    143       504
  8        1022    143       500
  8        1023     65       504
  8        1022    115       508
  8        1023    115       508
 16        1023      0       511
 16        1023      0       511
 16        1023      0       511
 16        1023      0       511
 16        1023      0       511
 16        1023      0       511
 16        1023      0       511
 16        1023      0       511
 16        1023      0       511
 16        1023      0       511
 16        1023      0       511
 16        1023      0       511
 16        1023      0       511
 16        1023      0       511
 16        1023      0       511
 16        1023      0       511
 16        1023      0       511
 16        1023      0       511
 16        1023      0       511
 16        1023      0       511
 16        1023      0       511
 32        1023      0       511
 32        1023      0       511
 32        1023      0       511
 32        1023      0       511
 32        1023      0       511
 32        1023      0       511
 32        1023      0       511
 32        1023      0       511
 32        1023      0       511
 32        1023      0       511
 32        1023      0       511
 32        1023      0       511
 32        1023      0       511
 32        1023      0       511
 32        1023      0       511
 32        1023      0       511
 32        1023      0       511
 32        1023      0       511
 32        1023      0       511
 32        1023      0       511
 32        1023      0       511
 64        1023      0       511
 64        1023      0       511
 64        1023      0       511
 64        1023      0       511
 64        1023      0       511
 64        1023      0       511
 64        1023      0       511
 64        1023      0       511
 64        1023      0       511
 64        1023      0       511
 64        1023      0       511
 64        1023      0       511
 64        1023      0       511
 64        1023      0       511
 64        1023      0       511
 64        1023      0       511
 64        1023      0       511
 64        1023      0       511
 64        1023      0       511
 64        1023      0       511
 64        1023      0       511
128        1023      0       511
128        1023      0       511
128        1023      0       511
128        1023      0       511
128        1023      0       511
128        1023      0       511
128        1023      0       511
128        1023      0       511
128        1023      0       511
128        1023      0       511
128        1023      0       511
128        1023      0       511
128        1023      0       511
128        1023      0       511
128        1023      0       511
128        1023      0       511
128        1023      0       511
128        1023      0       511
128        1023      0       511
128        1023      0       511
128        1023      0       511
MauricioFilgueiras commented 6 years ago

I think you are taking 2 bits of precision back. And may be spending some cycles in the process. Some quotes:

"The device features a 10-bit successive approximation ADC." [1]

"For optimum performance, the ADC clock should not exceed 200 kHz. However, frequencies up to 1 MHz do not reduce the ADC resolution significantly." [2]

"At a certain point, a higher ADC clock will decrease the accuracy of the conversion as the Effective Number Of Bits, ENOB, will decrease." [3]

My two cents...

I think part of the drift in your new readings in the develop branch may be when you call "total += analogRead(_pin);" because you miss some data. [4] An possible alternative may be Free Running Conversion.

I think you did a great job and keep using you library. May I drop some lines to help to improve it. BTW. I'm using to read a sensor to act as feedback loop to adjust one stepper motor.

Thanks.

@Fuzzyma I think you need to changed the title another time ... kHz... "200kHz for precise results."

PS: you guys make me dig in datasheets for hours, ty :P

. [1] 28.2. Overview, Atmel ATmega328/P [DATASHEET] Atmel-42735B-ATmega328/P_Datasheet_Complete-11/2016 [2] 2.8 Temperature, frequency and supply voltage dependencies, AVR120: Characterization and Calibration of the ADC on an AVR 2559D-AVR-02/06 [3] 2.1 Sampling frequency, AVR121: Enhancing ADC resolution by oversampling 8003A-AVR-09/05 [4] Main features of the new digital I/O implementation, https://www.codeproject.com/Articles/732646/Fast-digital-I-O-for-Arduino

Fuzzyma commented 6 years ago

PS: you guys make me dig in datasheets for hours, ty :P

Hehe, hope you learned something on the way :D

stylesuxx commented 6 years ago

I think you did a great job and keep using you library. May I drop some lines to help to improve it. BTW. I'm using to read a sensor to act as feedback loop to adjust one stepper motor.

@MauricioFilgueiras Yes, please do so - I would also love to see your research in the README. Will happily merge your PR.