Monday, 30 May 2016

Calibrating a MPS20N0040D-D Pressure Sensor

It has recently become apparent that readers of the pressure sensor posts on my blog need a way of calibrating the MPS20N0040D-D pressure sensors.

Here is the original post on pressure sensors:

http://langster1980.blogspot.co.uk/2014/11/how-to-use-pressure-sensor-with.html

This is not too difficult but you will need a few items:
  • An air pump - a bicycle pump will do.
  • An air pressure gauge - something with a human readable scale, preferably calibrated...
  • An MPS20N0040D-D pressure sensor and the difference amplifier set to the gain required for the circuit in mind - Hint I sell breakout boards here - Buy a pressure sensor breakout board
  • An arduino uno or other suitable microcontroller development board.
Once you have everything ready connect up the pressure sensor, analogue electronics to the arduino - I used the analogue zero pin, use whichever analogue pin required but make of note of it as the code needs to match the circuit.

Connection Diagram



Once you have everything connected as above upload the code below into the arduino:

/*
Pressure Sensor test Code
 */

const int analogInPin = A0;  // Sensor connected to A0

int sensorValue = 0;        // value read from the pressure sensor via the amplifier stage
float outputValue = 0;      // value output to the Serial port

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600); 
}

void loop() {
  // read the analog in value:
  sensorValue = analogRead(analogInPin);            
  
  // print the results to the serial monitor:
  Serial.print("sensor = " );                       
  Serial.print(sensorValue);  
  
  Serial.println();
       
  delay(1000);                     
}

Now open the serial monitor in the arduino IDE.  Observe the bit value received when no pressure is applied to the sensor.  This is the zero value.  Now introduce air pressure to the sensor using the pump.  When the external gauge reads 1 psi record the value of bits received.  Repeat this process until you have reached 32 psi or when 1023 bits has been recorded.  Create a table of results, mine looked like this:


Input pressure
ADC
ADC voltage
(PSI)
(Bits)
(volts)
0
61
0.283295132
1
82
0.4001820137
2
96
0.4740954967
3
116
0.5620723363
4
141
0.6742689746
5
163
0.7817888563
6
191
0.9386412512
7
242
1.183020528
8
269
1.329648094
9
329
1.632903226
10
420
2.052785924
11
450
2.19941349
12
470
2.2971652
13
510
2.492668622
14
540
2.639296188
15
570
2.785923754
16
605
2.956989247
17
630
3.079178886
18
660
3.225806452
19
690
3.372434018
20
720
3.519061584
21
740
3.616813294
22
765
3.739002933
23
800
3.910068426
24
850
4.154447703
25
900
4.398826979
26
920
4.49657869
27
950
4.643206256
28
969
4.736070381
29
980
4.789833822
30
995
4.863147605
31
1000
4.887585533
32
1006
4.916911046

Now that the results have been obtained we can plot a graph which shows how the number of bits received relate to the amount of pressure present.  We could work this out from the table but it's much easier to visualise this using a graph:

We can use the information from the graph and the table to display the pressure measured in psi. There are several methods to achieve this such as a lookup table which will interpolate the results.  I have decided to use a map function as the results obtained a quite linear (a straight line was achieved). 

Arduino Map Function

Enter the highest value obtained and the lowest value obtained and apply it to the scale needed (in this case PSI) and the function will do the rest for us:

outputValue = map(sensorValue, 60, 1006, 0, 32);  

Here is the full code in case people need it:

/*
Pressure Sensor test Code
 */

const int analogInPin = A0;  // Sensor connected to A0

int sensorValue = 0;        // value read from the pressure sensor via the amplifier stage
float outputValue = 0;      // value output to the Serial port

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600); 
}

void loop() {
  // read the analog in value:
  sensorValue = analogRead(analogInPin);            
  
  // print the results to the serial monitor:
  Serial.print("sensor = " );                       
  Serial.print(sensorValue); 
  
  outputValue = map(sensorValue, 60, 1006, 0, 32);  // convert the raw bit value 

  Serial.print("sensor = " );                       
  Serial.print(outputValue); 
  
  Serial.println();
       
  delay(1000);                     
}

Hope this helps people make more use of the MPS20N0040D-D pressure sensor.
 

That is all for now - take care people!