Code: Select all
#define ADC_Ch 36
void setup() {
Serial.begin(115200);
}
void loop() {
Serial.println( ReadVoltage( ADC_Ch ), 3 ) ;
Serial.println( analogRead( ADC_Ch ) ) ;
delay( 1000 ) ;
}
double ReadVoltage(byte pin )
{
double reading = analogRead( pin ) ; // Reference voltage is 3v3 so maximum reading is 3v3 = 4095 in range 0 to 4095
if( reading < 1 || reading > 4095) {
return 0 ;
}
// return -0.000000000009824 * pow( reading,3 ) + 0.000000016557283 * pow( reading, 2 ) + 0.000854596860691 * reading + 0.065440348345433 ;
return -0.000000000000016 * pow( reading, 4 ) + 0.000000000118171 * pow( reading, 3 )- 0.000000301211691 * pow( reading,2 )+ 0.001109019271794 * reading + 0.034143524634089 ;
} // More complex, improved polynomial or simpler polynomial, use either
/* ADC readings v voltage
// Polynomial curve fit, based on this raw data:
* 464 0.5
* 1088 1.0
* 1707 1.5
* 2331 2.0
* 2951 2.5
* 3775 3.0
*/