Hackscribble:
Maybe the OP can confirm this, but those readings of (roughly) 1V, 0.5V and 0.25V may be explained as follows.Grumpy_Mike said he changed one line in the program, to select resolution. The outputs in Volts are calculated by multiplying the ADC reading by a defined constant. I think this also needs to be changed to match the max reading at a given resolution.
That is correct. Refer to my Example file called "analogReadXXbit_full_demo.ino" and you will see the following, for example:
analog_reading = adc.analogReadXXbit(pin,bits_of_precision,num_samples);
V = analog_reading/MAX_READING_16_bit*5.0; //voltage
Notice the MAX_READING_16_bit constant. For other resolution readings, this constant is as follows:
//constants required to determine the voltage at the pin
const float MAX_READING_10_bit = 1023.0;
const float MAX_READING_11_bit = 2046.0;
const float MAX_READING_12_bit = 4092.0;
const float MAX_READING_13_bit = 8184.0;
const float MAX_READING_14_bit = 16368.0;
const float MAX_READING_15_bit = 32736.0;
const float MAX_READING_16_bit = 65472.0;
const float MAX_READING_17_bit = 130944.0;
const float MAX_READING_18_bit = 261888.0;
const float MAX_READING_19_bit = 523776.0;
const float MAX_READING_20_bit = 1047552.0;
const float MAX_READING_21_bit = 2095104.0;
He simply forgot to change that constant when changing the "bits_of_precision" parameter.
It can easily be explained as follows: if you leave that 16-bit constant in there, but use a 14-bit resolution setting, for instance, you can expect that a 1V reading would return a 14-bit reading of 16368/5V = 3273. So, it returned ~3273. When he then erroneously divided that by MAX_READING_16_bit, and multiplies by 5, he gets 3273/65472*5 = 0.24995V, which is close to his value of 0.2468V. It's user error.
Like any library, to use it correctly you must carefully read the examples and use the library appropriately. This is simple folks: analogRead() doesn't return a voltage, it returns a 10-bit reading. You must use your knowledge of that to calculate a voltage. Similarly, adc.analogReadXXbit(pin,bits_of_precision,num_samples) doesn't return a voltage, it returns an analog reading, with the precision you specify by "bits_of_precision." However, due to some nuances of how oversampling works, you must divide by my constants specified above to get the right answer (rather than dividing by 2^n, for instance, where n is the bits_of_precision).
Grumpy_Mike made a mistake in this, then later implied oversampling doesn't work and my library is broken, when in actuality it is user error.
Thanks Hackscribble for carefully reading my code, well enough to understand the examples, and catching that.
Grumpy_MIke, I hope that despite your large number of posts you can respect that others with fewer posts know things too. I could be misreading you, but it seems you spend a lot of time posting things and feel I may have "encroached" upon your territory. I'm not a computer scientist, I'm an an aerospace engineer with a deep love of, and a lot of knowledge in, electrical engineering and computer science, and I am a pretty meticulous guy and I really like to learn. Let's just all get along and learn from each other please.
And yes, your 20 min. test was very useful to me, and yes, it did prove to me that the 16-bit oversampling produced by the library, according to AVR121, really is returning discrete values with the resolution expected for a 16-bit sample (thanks to Hackscribble [Ray] for his plots and for pointing that out in Reply #18). This is looking good. I do appreciate both of your contributions like these.
Sincerely,
Gabriel Staples