Monday, 17 April 2017

Using the MPX7002DP Differential Pressure Sensor with an Arduino Uno

Recently I started to design an open source medical device for use by doctors and medical professionals to treat various respiratory disorders.  The post is here:

Multi-functional Medical Device

As part of that I bought a differential pressure sensor breakout board which uses the MPX7002DP differential pressure sensor made by NXP.  The datasheet for the sensor is here:

MPX7002DP Datasheet

The sensor itself can be bought from various online electronics vendors including:

Farnell Electronic Components - Part Number 2080499

RS Components - Part Number 719-1197

The pressure sensor breakout board itself looks like this:


Connecting this to a microcontroller development board like the arduino uno is very simple.  The underside of the PCB contains silk screen which gives the information:


The only thing I don't particularly like about this breakout board is that the wires are not colour coded.  It makes it awkward to wire the breakout board up!  If there are different signals on the wires then use different colours!

Here is the Connection Diagram for those who prefer pictures:
For people who prefer text:

GND connects to GND on the Arduino Uno
+5V connects to +5V on the Arduino Uno
Analog connects to A0 on the Arduino (Or any analogue pin as required)

Here is a picture of my setup - Ignore the Liquid Crystal Display:


Once the connections have been made it's time to write the code which will read the pressure sensor into the arduino.  Reading the datasheet for the sensor gives the designer the information needed to use this sensor:

Lets discuss what the operating characteristics tell us:

  • The pressure range is from -2 kPa (Vacuum) to +2 kPa (Positive Pressure).  So we need to convert the Raw ADC output to account for that.  1023 bits divided by 4000 Pa is the conversion factor assuming the sensor is accurate and no other noise creeps in.
  • The sensitivity is 1 Volt / pA - so we can check our measurement data if needed.
  • The response time is 20 ms - that means there needs to be a 20 ms delay between each sensor read.

The code has to do several things and to make things easier lets draw a quick flow diagram:
From the diagram it should be pretty simple to write up some code to get this working - The code I wrote is below:

 // MPX7002DP Test Code  
 // A.Lang - 2017  
 // This code exercises the MPX7002DP  
 // Pressure sensor connected to A0  
 int sensorPin = A0;  // select the input pin for the Pressure Sensor  
 int sensorValue = 0;  // variable to store the Raw Data value coming from the sensor  
 float outputValue = 0; // variable to store converted kPa value   
 void setup() {  
  // start serial port at 9600 bps and wait for port to open:  
  Serial.begin(9600);  
  while (!Serial) {  
   ; // wait for serial port to connect. Needed for native USB port only  
  }  
  pinMode(sensorPin, INPUT);  // Pressure sensor is on Analogue pin 0  
 }  
 void loop() {  
  // read the value from the sensor:  
  sensorValue = analogRead(sensorPin);  
  // map the Raw data to kPa  
  outputValue = map(sensorValue, 0, 1023, -2000, 2000);  
  // print the results to the serial monitor:  
  Serial.print("sensor = " );  
  Serial.print(sensorValue);  
  Serial.print("\toutput = ");  
  Serial.println(outputValue);  
  // wait 100 milliseconds before the next loop  
  // for the analog-to-digital converter and  
  // pressure sensor to settle after the last reading:  
  delay(100);  
 }  

It should be fairly self-explanatory.

  • The sensor output will be connected to analogue pin A0.  The actual data will be stored as an integer value in a variable called sensorPin.
  • The raw converted analogue data will be stored in an integer variable called sensorValue.
  • The converted output data in kPa will be stored in a float variable called outputData.
  • In the Setup Function the serial communications are initialised and the sensorPin variable is declared as an input.
  • In loop function the sensor data is read in from the analogue pin and mapped to a kPa value.
  • The data is then sent to the serial terminal so it can be reviewed.
  • A delay of one hundred milli-seconds is introduced to allow the system to settle.
  • Then the whole process repeats for ever!

Here is a video of the circuit in action.


The output of the sensor was actually very sensitive, more so than I anticipated and will work well for the application.  The next thing to do is to output the serial data directly to an external program so that it can be graphed which provides a much more visual and useful display.  After that the serial communications can be converted to bluetooth and then the data can be sent to a mobile phone or again to a PC but the system can be made battery powered and therefore truly wire free!

Take care people - Langster!

21 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. I read another one of your blog posts recently about a different pressure sensor for which you designed a breakout board. I need to be able to measure pressures in a soft vacuum starting approximately 10 psi below atmospheric. Does this sensor already have an operational amplifier built in? According to the available information, I don't think in its current state it will have the range I need for my application.

    ReplyDelete
    Replies
    1. Apologies for the late response. I don't always get notice about comments on my blog post. The sensor does not have any in built amplification. It it doesn't have the range for your application adding gain won't help. Adding gain will improve the sensitivity but won't affect the range. I would suggest using a different sensor.

      Delete
  3. Do you help us to read flow of gases using this sensor

    ReplyDelete
    Replies
    1. I could try and help you use this sensor but I need to understand a little more about what you are trying to achieve.

      Delete
    2. I need the same thing. I'd like to measure the air flow coming in and out of a pacient lungs. I'm trying to develop a Ventilator to help people infected with COVID-19 virus. A have the MPXV5050DP and the D6F-PH. Both are diferential pressure sensors. I know that is possible to calculate the air flow using a diferential pressure sensor installed in a Venturi Tube

      Delete
  4. Forgot to present myself. My name is Hillermann and I'm from Brazil. Sorry about my english.

    ReplyDelete
    Replies
    1. Hi Hillerman, thanks for the introduction and for reading my blog. In my later posts I discuss making a venturi tube and how to make measurements. If you have questions please get in contact. I'm happy to try and assist where I can.

      Delete
    2. Hello sir pls mail me on kayodesamuel523@gmail.com so as to chat you sir , pls im also developing a ventilator, thanks sir , I'm most greatfull

      Delete
  5. Hi Alex, this post is exactly what I have been looking for! I'm a beginner in Arduino but have helped design electro-mechanical pneumatic systems before - I just had no idea about the software side of things.
    How do you know to divide 1023 bits by 4000 kPa? Unsure if you got that from the datasheet or if that is a rule of thumb?
    Thanks

    ReplyDelete
    Replies
    1. Hi, thanks for commenting. The top line of the electrical characteristics for the pressure sensor gives the minimum and maximum range in the data sheet. The Analogue to Digital converter on the atmel 328p (the microcontroller used on an arduino R3) has a 10 bit range. Ten bits in binary has a decimal value of 1023...so it's a simple conversion to get from one to the other.

      If you have any other questions or need a hand please get in contact.

      Delete
    2. Hi Alex, Thank you very much for getting back and for the clear and succinct answer. Much clearer!

      I will do, I'm hoping to start this project soon.

      Ben

      Delete
    3. You are most welcome Ben! There must be a lot of people looking to do something with this sensor, I have had a lot of interest / comments on this post and similar ones recently. I suspect it is to make home brew ventilators. I should probably do something similar...

      Delete
  6. Hi Alex i am Omar can you plz give me more details about ur setup ( the connections and the way to connect with LCD
    and the components that shows in Ur video..)
    It is not clear
    Really appriacte it buddy

    ReplyDelete
    Replies
    1. Hi Omar...

      I clearly say in the post that the connections are +5V, 0V, and signal to A0. The LCD display is not in use. I'm not sure how I can be more clear about the connections to be honest - It's three wires...Send me a message if you are still struggling

      Delete
  7. Hi Alex,
    You have a great post, thank you for that. I am working on developing a tank level sensor and am curious of your thoughts if a differential pressure sensor could work for this. I envision one port connected to a tube that is vertically aligned inside the tank, as the tube fills with fluid there will be a small pressure differential between the ports. Or I suspect there will be anyways....curious of your thoughts.
    Regards,
    Kent

    ReplyDelete
  8. HELLO SIR! BY THIS CIRCUIT CONCLUSION CAN WE MEASURE THE FLOW FOR A GIVEN PRESSURE?.WAITING FOR YOUR ANSWER SIR. THANK YOU!!.

    ReplyDelete
  9. Hi, I run your example code. Yet the result value is 250+ why is that?

    ReplyDelete
  10. Hello. I want to measure the speed of the air inside the air handling unit air duct on the arduino with the MPXV7002DP sensor. The air flow in the duct is in the range of 0-2300m3/h. Channel cross-sectional area 0.31*0.66m2. I could not make a stable measurement with the codes I found on the internet. Can you help with this?

    ReplyDelete
    Replies
    1. Hi, I'm sorry I'm not sure I can help. It is very difficult to assist with specific projects and problems. Good luck though!

      Delete
  11. Hi Alex,
    I recently ordered the same breakout board that you show in this article. The sensor has two ports but only one analog wire going into the Arduino. So, can you only make use of one of the ports and does it matter which one?

    Thanks,
    Paul

    ReplyDelete