Tech Tip — ESP8266 ADC

Jan 2018.  Obtain consistent ESP8266 ADC readings
Getting consistent ESP8266 ADC readings can be tricky because, as observed and discussed in the community, there is an unknown internal factor affecting expected straight-line readings. Use this code snippet to work around this shortcoming if you can tolerate a 25ms reading.

The code simply takes an average of the lowest sample reading over five windows with five readings each.

        		
int readAnalog() { 
   int sum = 0;
   for( byte ix = 0; ix < 5; ix++ ) {
      int lowest = 1000;
      for( byte iy = 0; iy < 5; iy++ ) {
         int sample = analogRead( A0 );
         if( sample < lowest )
            lowest = sample;
         delay(1);
      }
      sum += lowest;
   }  
   return sum / 5;
}
					

© 2017 PixelTwenty LLC
info@pixeltwenty.com