int mapsen = 0; // Set MAP sensor input on Analog port 0
int NUM_SAMPLES=300; // number of samples
float sum = 0; // sum of samples taken
int sample_count = 0; // current sample number
float input_voltage = 4.90; //measured with multimeter
void setup() {
Serial.begin(9600);
}
void loop() {
sum = 0;
sample_count = 0;
while (sample_count < NUM_SAMPLES) {
sum += float(analogRead(mapsen));
sample_count++;
delay(0.5);
}
float out_voltage = (sum / NUM_SAMPLES * input_voltage) / 1024;
float Pabs = out_voltage*2705.8824/input_voltage-16.4706; //absolute pressure in mbar
//Bosch 20-250 kPa MAP sensor min pressure measured: 200mbar with 0.4V (typical)
//Bosch 20-250 kPa MAP sensor max pressure measured: 2500mbar with 4.65V (typical)
//Bosch 20-250 kPa MAP sensor linear model formula is:
//Ua=Uv*(Pabs*0.85/230 + 0.0061); Uv=supply voltage; Ua=output voltage; Pabs is absolute pressure
//Ua is proportional to the supplied voltage, so Ua is always divided by Uv
//from this formula the linear model for absolute pressure in mbar:
//slope = dy/dx = (2500mbar-200mbar) / (Ua_max-Ua_min)/Uv_input = 2300mbar / (4.65V-0.4V)/5V = 2300/0.85 ~2705.882353
//generalised linear model y=ax+b; then the y-intercept (b) is calculated as follows with Uv=5V:
//200=(0.4/5)*(2300/0.85)+b; b=200-(0.4/5)*(2300/0.85)=200-0.08*2705.8824=200-216.4706=16.4706
//slope = dy/dx ~ 2705.8824; y-intercept ~ -16.4706, so y=ax+b or y(abs pressure in mbar)=2705.8824*(Ua/Uv)-16.4706
Serial.println(Pabs);
//Serial.print("\t");
//Serial.println(out_voltage,4);
}