NuclearPhoenixx / Arduino-Pico-Analog-Correction

📚🪛 Arduino library to calibrate and improve ADC measurements with the Raspberry Pi Pico.
MIT License
21 stars 0 forks source link
adc analog arduino arduino-ide arduino-library calibration raspberry-pi raspberry-pi-pico

Arduino-Pico-Analog-Correction

arduino-library-badge latest version issues open pr

Arduino library to calibrate and improve ADC measurements with the Raspberry Pi Pico. Can compensate ADC offsets, calculate the arithmetic mean of any number of measurements and temporarily disable the power-saving mode when analog-reading to improve the power supply ripple and noise. There is also a function for reading the temperature sensor depending on your ADC resolution and reference voltage.

This library is also available in the Arduino IDE, see Arduino Library List.

Using

You can use the example calibration sketch to grab the calibration values, just follow the instructions given in the sketch.

You can serial print the calibration values anytime using this command:

void returnCalibrationValues();

Other than that, you can take non-calibrated and calibrated measurements using the corresponding functions:

int analogRead(size_t pin);
int analogCRead(size_t pin, size_t avg_size=1);

Both of these temporarily disable the power-saving mode to improve noise. Only the second function allows taking an arbitrary number of measurements and returning the arithmetic mean while also using the linear calibration.

You can also read the correct temperature from the temp sensor, even if you're not using the default resolution and reference voltage. Be sure that you supply the constructor with the correct values, then simply call:

float analogReadTempCorrect();

This defaults to Celsius, you can also print Fahrenheit:

float analogReadTempCorrect(PAC_F);

Limitations

This library is limited to a very simple linear calibration since there are practically only two trusted voltages available to be measured: GND (0V) and VCC (3.3V). This means that the calibration will ensure that the ADC measures GND as 0 and VCC as the maximum channel number (depending on your resolution, e.g. 4095) with any value in between being distributed linearly within these two.

In addition, as the very helpful Raspberry Pi Pico datasheet suggests, the on-board power supply noise and accuracy are not the best. To get the best results possible, you need to use an external voltage reference. This, however, introduces additonal drawbacks.

(Temporarily) Disabling the power-saving mode of the regulator is therefore a must-have to significantly reduce ripple on the ADC supply and is exactly what this library does independently from the calibration.

More info about the Pico's ADC can be found on this website by @ferret-guy, who did extensive testing to find out the exact specifications.