Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ADC improvement & fix #1314

Merged
merged 7 commits into from
Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions main/ZsensorADC.ino
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ ADC_MODE(ADC_TOUT);

//Time used to wait for an interval before resending adc value
unsigned long timeadc = 0;

unsigned long timeadcpub = 0;
void setupADC() {
Log.notice(F("ADC_GPIO: %d" CR), ADC_GPIO);
}
Expand All @@ -49,14 +49,26 @@ void MeasureADC() {
timeadc = millis();
static int persistedadc;
int val = analogRead(ADC_GPIO);
int sum_val = val;
if (NumberOfReadingsADC > 1) { // add extra measurement for accuracy
for (int i=1; i<NumberOfReadingsADC; i++){
delay(7);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to average the accuracy without having a blocking delay() ? I'm afraid about the impacts on the main loop and on other modules.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, its just 7ms per read so for recommended 10 reads its 70ms in total (still optional) and guess delay actually yields every ms. Btw check RFB, theres is delay in loop on sending as well
Currently I'm running that (10 readings) with RF and DS1820 temp sensor with no issues so far...
Maybe we can short the delay to 1ms or remove it at all and also add one dummy analogRead as its recommended for 8266:

the reliability of the readings can be significantly improved by discarding an initial reading and then averaging the results

I'm afraid average values across loops adds another noise for already poor ADC in 8266 so better to read all in once

sum_val += analogRead(ADC_GPIO);
}
val = sum_val / NumberOfReadingsADC;
}
if (isnan(val)) {
Log.error(F("Failed to read from ADC !" CR));
} else {
if (val >= persistedadc + ThresholdReadingADC || val <= persistedadc - ThresholdReadingADC) {
if ( val >= persistedadc + ThresholdReadingADC || val <= persistedadc - ThresholdReadingADC || (MinTimeInSecBetweenPublishingADC > 0 && millis() > (timeadcpub + (MinTimeInSecBetweenPublishingADC * 1000UL))) ) {
timeadcpub = millis();
Log.trace(F("Creating ADC buffer" CR));
StaticJsonDocument<JSON_MSG_BUFFER> jsonBuffer;
JsonObject ADCdata = jsonBuffer.to<JsonObject>();
ADCdata["adc"] = (int)val;
if (NumberOfReadingsADC > 1) {
ADCdata["adc_reads"] = NumberOfReadingsADC;
}
# if defined(ADC_DIVIDER)
float volt = 0;
# if defined(ESP32)
Expand All @@ -71,14 +83,19 @@ void MeasureADC() {
# endif
volt *= ADC_DIVIDER;
// let's give 2 decimal point
val = (volt * 100);
volt = (float)val / 100.0;
int v = (volt * 100);
volt = (float)v / 100.0;
ADCdata["volt"] = (float)volt;
# endif
# if defined(ADC_CALIBRATED_SCALE_FACTOR) // calibration: scale factor = ref-Vcal-mV / sum-of-cal-readings @see https://skillbank.co.uk/arduino/calibrate.htm
float voltage = sum_val * ADC_CALIBRATED_SCALE_FACTOR; // convert to voltage in millivolts
ADCdata["adc_sum"] = (int)sum_val;
ADCdata["mvolt_scaled"] = (int)voltage; // real scaled/calibrated value in mV
# endif
pub(ADCTOPIC, ADCdata);
persistedadc = val;
}
}
}
}
}
#endif
8 changes: 8 additions & 0 deletions main/config_ADC.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ extern void ADCtoMQTT();
# define ThresholdReadingADC 50 // following the comparison between the previous value and the current one +- the threshold the value will be published or not
#endif

#if !defined(NumberOfReadingsADC) || (NumberOfReadingsADC < 1)
# define NumberOfReadingsADC 1 // number of readings for better accuracy: avg adc = sum of adc / num readings
#endif

#ifndef MinTimeInSecBetweenPublishingADC
# define MinTimeInSecBetweenPublishingADC 0 // pub at least at defined interval - usefull to publish values in case they do not change so much ; 0 = disabled
#endif

/*-------------------PIN DEFINITIONS----------------------*/
#if defined(ESP8266) || !defined(ADC_GPIO)
# define ADC_GPIO A0 //on nodeMCU this is D3 pin
Expand Down