- Thu Apr 12, 2018 4:35 pm
#75333
When I said remotely calibrate I didn't mean automatically calibrate without any external measurement.
Rather I meant that I use a variable for the calibration factor that is stored locally but I can control remotely. I found that more convenient than fixing it in the firmware particularly if I have several devices using the same code.
A simple example would be
Code: Select allvoid checkBattery() {
int adc;
adc = analogRead(A0);
battery_volts = (battery_volts * 15 + battery_mult * adc_cal * adc) / 16;
}
}
where the function is called periodically in the loop to update the measured battery voltage (float battery_volts;).
battery_mult is a fixed factor reflecting the potential divider scaling the battery volts down.
e.g. float battery_mult = 57.0/10.0/1024; //47K 10K divider
adc_cal is the calibration factor.
I normally store this in a SPIFFS file along with any other config data. It is read during setup but can be updated by a server call. So I measure the battery voltage with a voltmeter then compare that against the battery_volts value to calculate the appropriate adc_cal value.
The function above contains a simple running average to reduce residual noise from the ADC