Tuesday, 18 November 2014

How to use a pressure sensor with a Microcontroller (MPS20N0040D-D)

A colleague of mine bought a pressure sensor from Amazon - they are all over the internet can be bought from Amazon, Hobby Components and Dealxtreme!  He wants to use it to measure water pressure in a tank.  In order to do that we need to know how to connect it up and how to obtain the electrical signal from the sensor and how to read this electrical signal with a micro controller so that we can display the result and act upon it...The sensor can be bought from the sites below:

MPS20N0040D-D-Pressure-Sensor (Amazon)

MPS20N0040D-d Pressure sensor (DealXtreme)

MPS20N0040D-D Pressure sensor (hobby Components)

The Sensor looks like this:

MPS20N0040D-D Pressure sensor
Here is a link to the datasheet for the sensor:

Pressure Sensor Datasheet

The datasheet isn't the best I have read but it does provide most of the information required.  The pressure sensor has a measurement range of 0-5 8 psi (40kpa).  The unit psi is an imperial measurement which stands for pounds per square inch.  The scale was designed for use in measuring the weight of goods (solids) however the scale can be applied to any pressure - gas or liquid.

Wikipedia Entry on the psi unit

The unit psi can be converted to an SI unit Pascals and the datasheet for the pressure sensor refers to 40 kpa as being the measurement range converted from psi to Pascals.

Wikpedia Entry on Pascals

1 Pascal (pa) = 1 kg / (metre * second)

Using mathematical formulae to define scales is technically correct but doesn't really give a real world example of what 1 pa actually feels like.  So a real world example of the pressure exuded by one pa is the weight of a £5 note (or a dollar bill) on a table is roughly equivalent to 1 pa. Popcorn kernels popping exudes roughly 2000 pa.  There are some more real world examples provided in the link below:

magnitudes of pressure

Converting psi to pascals is easy:

1 pound per square inch =
6 894.75729 pascals

So we have a sensor that is capable of measuring a range of pressures, can be driven by 5 Vdc and puts out a 0-25 mV signal.

That's enough theory for now...lets get on with using the sensor...The datasheet shows the device using a bridge connection that outputs a 0-25 mV signal.  That is a very small signal, if we were to connect the output directly to a micro-controller we wouldn't measure much unless the pressure was full scale and that output would be very low.  What needs to be done is to amplify the output of the pressure sensor in order to record the sensor signal properly.  That way we get more sensitivity and resolution - in short a better measurement device.

There are plenty of ways of amplifying electronic signals but in the case of instrumentation it is often necessary to amplify signals quite a lot of times in order to get a usable signal.  To that end we are going to design a difference amplifier. This is an application of operational amplifiers set to provide gain but also only measure the difference between the signals applied to the inputs.

Hyper-physics difference amplifier page

All about circuits - differential amplifiers

The datasheet for the Pressure sensor shows how to connect the sensor although not particularly clearly as a Wheatstone bridge.  If more information about Wheatstone bridges is required check out the link below.  It was conceived by a British Scientist and engineer - Samuel Hunter Christie in 1833 and improved by Sir Charles Wheatstone who made it popular.

Wheatstone Bridge

These measurement circuits are one of the foundations of analogue electronics and instrumentation. You can make almost any kind of sensor measurement using a Wheatstone bridge.  Here is the internal circuit diagram for the pressure sensor:

So which pins connect to what?

  • - Output (1) connects to - In on the Operational Amplifier
  • + Input (2) connects to +5 Vdc
  • + Output (3) connects to + In on the Operational Amplifier
  •    Output (4) does not connect to anything!
  • - Input (5) connects to 0 Vdc
  • - Output (6) connects to - In on the Operational Amplifier

Next we need to calculate the gain required.  We need to change 0 V - 25 mV into something larger and we also need to account for the voltage offset present (around 2 Vdc).  So first of all lets design a differential amplifier.

How to design a difference amplifier

Rather than reinvent the wheel and go through all of the theory again I used an online calculator to generate values for me.  It's a lot quicker and easier than pages of mathematical calculations.

Online Difference Amplifier Calculator

I have made several assumptions about the circuit....that the output from the sensor will be somewhere between 0.0 Vdc and 0.0025 Vdc.   I set the supply voltage to the op-amp as +5 Vdc and 0 Vdc (single supply mode).  The amplifier then gives out between 57 mV and 970 mV.  Those values are quite small so we will need to amplify that further in order to give a reasonable output into the micro-controller ADC input.  We are looking for something between 0 Vdc and 5 Vdc.

Here is the first part of the circuit.  I've drawn the sensor as resistors in the 'Wheatstone bridge' configuration.  To check the sensor was working I measured the resistance between each sensor pin with an ohm meter and found there to be 5 k-Ohms present in each part of the sensor circuit.

Lets explain the circuit....
The blue square is a rough guess at how the sensor works...It may not be entirely accurate but I don't have any more information to work from.  The amount the Wheatstone bridge varies is again a guess at 1 k-Ohms - I'm hoping it works this well!!

The green square is a simple filter to prevent external electrical noise (interference) from affecting the measurement.  We only want to measure signals from pressure sensor and nothing else.

The red square is the section designed with the calculator.  It's a standard difference amplifier with a feedback capacitor and some supply de-coupling capacitors again to prevent external interference affecting the circuit.  The gain of the amplifier is 5.6.

The output of the amplifier is still a little low to drive the ADC so lets add a non inverting amplifier to the output section so that we then get a times 3 gain and therefore a 200 mV to 3.5 Vdc swing.

Here is the full analogue input stage:

I simulated the circuit just to make sure it worked.  It appears to and here is the video of the circuit for those that are interested.


We can now design the full circuit and create an arduino shield.  I have added RS485 communications as that was one of the requirements of the circuit.  I haven't discussed RS485 before but it is a fairly common serial communications protocol.  Here is the full schematic diagram:




I have also designed an Eagle Shield for it but I have actually etched this yet....


Here is the top player - note that the Instrumentation amplifier is an SOIC surface mount package which is mounted on the underside of the board.


The rest of the circuit shows the connections to the arduino and I also added a 16x2 LCD display and the communications section.

So before I do anything I always prototype a circuit and this time is no different.  I got all of the required components and attached them with wires to my breadboard and arduino.  I didn't bother with the RS485 Communications section.  That can come later, Here is how it looks:


I then wrote some very quick code to check it works:

/*
Pressure Sensor test Code
*/

// These constants won't change.  They're used to give names
// to the pins used:
const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to

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

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

void loop() 
{
  // read the analog in value:
  sensorValue = analogRead(analogInPin);            
  outputValue = map(sensorValue, 10, 1023, 0, 100); //The zero value of sensor is around 10
  
  // print the results to the serial monitor:
  Serial.print("sensor = " );                       
  Serial.print(sensorValue);      
  Serial.print("\toutput = ");      
  Serial.println(outputValue);   

  // wait 500 milliseconds before the next loop
  // for the analog-to-digital converter to settle
  // after the last reading:
  delay(100);                     
}


Once I had uploaded this to the arduino and opened a serial monitor I expected there to be a steady stream of values being output to the serial monitor - there was!  Excellent.  I then attached a small piece of tubing to the pressure sensor and blew down the tube (provided some pressure)....Nothing happened....I then checked all of my connections and swapped the LM358 OP-Amp for another one....just in case and nothing happened.  I then removed all of the connections and rebuilt the entire circuit and reconnected it to the arduino and repeated the test and nothing happened.  At this point I was beginning to think the sensor was faulty....I'll be honest - I then gave up and moved on to other things.

I later revisited the circuit and re-tested it once I had it connected up on the breadboard.  It now does work and displays both positive and negative pressure.  The sensor's resolution isn't great but it does work.  I don't have a calibrated pressure source so I can't prove the output is correct.  The connections are as drawn above.  Here is the diagram of the connections (It's probably easier to follow...)


Parts List:

1x MPS20N0040D-D Pressure Sensor

2x 10k Resistors (Brown, Black, Orange, Gold)
2x 56k Resistors (Green, Blue, Orange, Gold)
1x 1k Resistor (Brown, Black, Red, Gold)
1x 2.7k Resistor (Red, Violet, Red, Gold)

1x LM358 Op-Amp

2x 100nF Capacitors - (Optional)
2x 10uF Electrolytic Capacitors - (Optional)

1x Arduino R3

Connections wires...

The hardest part is working out the connections of the sensor...It's a 6 pin package with no discernible markings although there is a bite on one edge.  I'm not sure how I got this working but I just kept fiddling the connections until I was happy it worked.  The middle two pins are connected to 0 Vdc and 5 Vdc and the two outer pins to the right are connected to the 10k resistors and then the op-amp. I'm not sure why it works but it does...

Best of luck people...and my apologies for not getting this working correctly first time...

Update - as this post appears to be so popular I designed a breakout board - check it out!!

MPS20N0040D-D Pressure Sensor Breakout Board

If people are interested I am selling these breakout boards for £10.00 which is roughly $12.93 - Contact me if you are interested!

Here is the webstore where you can purchase a breakout board -

Lang Electronics Design - Web Store

I have also written up a post on how to calibrate the pressure sensor:

Calibrating the pressure sensor

Enjoy, Langster!

55 comments:

  1. That is very interesting and helpful.
    Thank you very much for sharing this kind effort.

    ReplyDelete
    Replies
    1. You are most welcome! Thanks for reading my blog!

      Delete
  2. This comment has been removed by a blog administrator.

    ReplyDelete
  3. This comment has been removed by a blog administrator.

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
  4. SIR I MAKE SAME CONNECTION WITH RASPBERRY PI
    BT I STILL GET SATURATED OUTPUT

    ReplyDelete
    Replies
    1. It will not work with a raspberry pi. A pi does not have an analogue to digital converter.

      Delete
  5. Hello, Thank you for the circuit
    Please what is the cutoff frequency for the filter circuit and what type of filter is it, I can see that it looks like a low pass but what configuration is it, for example the Sallen key filter
    Also, if i want to apply this to a blood pressure system what modifications can I make to this?

    ReplyDelete
    Replies
    1. It is a low pass passive filter. I never bothered to measure or calculate the cut off frequency. The sensor could be used to measure blood pressure, you would need a blood pressure cuff and an electronic switching valve in order to achieve this along with a sensor and microcontroller.

      Delete
    2. I'm doing a project on this( Electronic Sphygmomanometer).Can I proceed to buy this pressure sensor?

      Delete
  6. Hi my friend, thank you for your work. I have a question about the design of the sensor, I do not have it on hand and I intend to buy it: Is it possible to connect a hose to this sensor and measure the water pressure from a small pump "fish tank"? What do you think?

    Thank you again.

    ReplyDelete
    Replies
    1. The sensor would have to be modified to measure water pressure. The would need to be a waterproof barrier between the sensing element and the water. It can be done and would be similar to a blood pressure cuff but I have never tried to achieve this.

      Delete
  7. This comment has been removed by a blog administrator.

    ReplyDelete
  8. Can this be used to quantify negative pressures?

    ReplyDelete
  9. I have noticed that small levels of negative pressure (vacuum) can be detected with this sensor. I have never tested it formally so I wouldn't rely on it's accuracy without testing it. The datasheet for the sensor makes no mention of the sensor's ability to measure vacuum. I'd be more inclined to choose a sensor that was rated for measuring vacuum such as a pirani or penning gauge.

    ReplyDelete
  10. Hi. I want to use this sensor to measure blood pressure using arduino. how can I get value with mmHg unit??

    ReplyDelete
    Replies
    1. Hi,

      You will need to calibrate the sensor in order to display pressure measurements in millimetres of mecury (mmHg). The correct way to do this would be to get a pump with a gauge on it and introduce air pressure to both the gauge and the pressure sensor using the pump. I go through this process in my blob post here:

      https://langster1980.blogspot.com/2016/05/calibrating-mps20n0040d-d-pressure.html

      You would then need to convert PSI to mmHg:

      https://www.google.com/search?client=firefox-b-d&q=convert+PSI+to+mmHg

      You can do this in the microcontroller code easily enough...

      If you are using the same circuit as I did you could use my calibration from the blog post and straight convert

      roughly:

      0 Vdc : 0 PSI which is 0 mmHG
      0.4 Vdc : 1 PSI which is 51.7 mmHG
      2 Vdc : 10 PSI which is 517 mmHG
      5 Vdc : 32 PSI which is 1654.88 mmHG

      Good luck!

      Delete
    2. Hi sir again. I want to understand the diferrence between sensor input 2 and 5 connection and the connection that u have described with the amplifier. In sensor discription Input 2 to 5v and Input 5 to 0V but in figure shows the connection with amplifier it's oposed

      Delete
    3. Hi Attaoui. I think you need to do a little research on Operational amplifiers and how they work - in particular look at difference amplifiers. I have drawn the schematic with the signal being presented to both the inverting input and the and the non-inverting input. I am measuring the difference between two signals which is how the sensor functions. It is a differential pressure sensor.

      Delete
    4. Okay sir. for my case im using your circuit for BLOOD pressure measurement, Im asking if a should add filtering step that you have mentiioned optional ? sorry for asking a lot but really I have spent 2 week with conditionnig step because the sensor gives ironic values.

      Delete
    5. The filter is only necessary if the power supply used is not creating high frequency noise. It sounds as if your power supply and setup is causing noise. Are you using a switch-mode power supply? If so it would be better to use a linear one or a battery and a linear regulator. Keep connection wires to a minimum and as short as possible. If you still have issues include the filter circuitry. If you can send me photos of your setup I may be able to help you further.

      Delete
    6. Sorry I made a typing mistake...that should read only necessary if the power supply is creating noise...

      Delete
    7. Mrs Alexander dirt of all thank you for you help and repleay. actually I'm using just the 5V of arduino to supplay all the others componenets. Well i'll send you pictures of my circuit te be more understanable

      Delete
    8. Hi Attoui,

      How are you supplying power to the arduino? It is that power supply that will be causing the noise I expect. USB power supplies are notorious for being electrically noisy. I look forward to seeing pictures of your circuit.

      Good luck!

      Delete
    9. I have not thinking about supplying power of arduino can generat noise, actually I'm unsing 5v and 3.3V with all my projects. About this one Im using the same circuit that you have discribed with filtering step just I havn't found 10nF for the output 2 I used 100nf that you have mentioned in the part list. Im traying to contaction you to send picture of circuit diagram but I don't fond uor contact yet.

      Delete
  11. Hi, interesting demo! but a question still in my mind, that is:
    I just not to use a single op-amp circuit with a gain larger than 5.6, for example with gain of about 15?
    Thanks!

    ReplyDelete
    Replies
    1. Sorry there wasn't any feature to edit my post above, So second line first word is WHY instead of I.

      Delete
    2. IF you increase the gain of the amplifier then you will cause the analogue to digital converter in the microcontroller to saturate. A low signal on the output of the sensor will be read as full scale because too much gain has been applied. Too much gain affects resolution and sensitvity. A pressure measurement from the sensor will be detected but the amount of pressure detected willbe unresovable - it will be seen as full scale. Hope this makes sense.

      Delete
    3. I think the question here, which I also have, is why it is necessary to use both the op amps with gains of 5.6 and 3 to give a total gain (as I see it) of about 16.8. Sure the LM358 has two opamps on board, but why could we not have a single op-amp with a gain close to 16.8 - I cannot see why that single opamp solution would cause saturation of itself since the net gain would be the same.

      It is the case that the ADC would most likely have an input immpedance requirement e.g. that the source being measured should have an impedance fairly low in order to source enough current to the ADC sample and hold. But as I recall an opamp does have a decently low output impedance. It's been 30 years or more since I studied this so I may be rusty!

      Delete
  12. But in my understandings, the circuit that you used contains two amplifier that the first one amplifies the signal by the magnitude of 5.6 and the second one by 3.
    So my question is why just not to use a Single op-amp with gain of about 15, the two op-amp total gain in your circuit is almost 15, too? So is there any other reason behind using 2 op-amps instead of just using one with larger gain ratio?
    And i think in this case, there is no risk of saturation too.

    ReplyDelete
  13. Hi! What program did you use to simulate the circuit?

    ReplyDelete
    Replies
    1. Hi,

      I used and still use National Instruments Multisim. There is an online version available here if you register:

      https://www.multisim.com/

      I find it to be one of the best spice simulators around. There are others but using spice simulators to check circuit function is an invaluable tool when designing circuits. The only caveat is that you must enter the correct information into the spice simulator otherwise your results will be incorrect - Good Luck!

      Delete
  14. can i use this in blood pressure measurment

    ReplyDelete
  15. Hi. I'm trying to do the same circuit but i do have lm741cn opamp instead of lm358 opamp. Does it work with lc741cn? I also have lm324n opamp if makes a use.

    ReplyDelete
  16. This comment has been removed by a blog administrator.

    ReplyDelete
  17. Great project.
    I would like to use this configuration to measure nasal breathing.
    Do you think this sensor is capable of taking those pressure values?
    It goes up to 40k Pascal.

    ReplyDelete
  18. Thank you very much for this fantastic and detailed blog. I am building an arduino project based on this sensor. My question is that can i use lm393 instead of lm358? Cause i have only that kind at the moment.

    ReplyDelete
    Replies
    1. LM 393 is a comparator not an operational amplifier. The devices are for different functions. To create the difference amplifier you will need an operational amplifier but it doesn't have to be the LM358....just about any operational amplifier would work.

      Delete
  19. Do u have a video of this circuit while it is working?

    ReplyDelete
  20. Hi! Thanks for your post! I am facing an issue though: the lm358 only seems to read the pressure if the difference between the pins +/- is larger than ~0.7V (it varies according to the gain), any thoughts?

    ReplyDelete
  21. Hi Yuri, sorry for the delay in response. I haven't played around with this circuit in a very long time and I didn't notice the effect you are describing. Have you used the exact circuit above? If so what and where are you measuring this voltage difference?

    ReplyDelete
  22. Hi, I'm trying to build your circuit to use the pressure sensor. I connect everything as you say but at the output of pin 7 of the operational I have no signal. I would be interested to know what was your failure at first that did not work for you, I use the lm358. Thank you

    ReplyDelete
    Replies
    1. Hi,

      My issues when I first built the circuit relating to the sensor connections. It is hard to tell which pin is which on the sensor. If you are not getting the output you think you should be I would be looking to measure the voltages you are getting a each pin of the op-amp. Using a digital volt-meter I would measure the voltage with respect to ground on each pin.

      Pin 1 = 1st op-amp output, should be roughly 2 Volts
      Pin 2 = 1st op-amp negative input should be roughly 2 Volts
      pin 3 = 1st op-amp positive input should be roughly 2 volts
      pin 4 = GND should be zero volts
      pin 5 = 2nd op-amp input should be roughly 3 volts
      pin 6 = 2nd op-amp input should be the same as pin 1 roughly 2 volts
      pin 7 = 2nd op-amp output should be roughly 3 volts
      pin 8 = Vcc should be 5 Vdc.

      Good luck!

      Delete
    2. I will try thank you for this post

      Delete
  23. good topic!!thnx
    however, can i use two separate pressure sensors ( MPS20N0040D )could be used to get differential pressure? & calculate the airflow

    ReplyDelete
  24. Hi Alexander, great project!
    I would like to use the board for low measurement values to measure mouth blow in a distance of 15-20 cm to the lips. A friend can not move and I thougt about a blowing bell to help her. I tried a BME280 but it does not sense much and is also quite slow (in fact it measures humidity better than blow ;-). Thx for a short response!

    ReplyDelete
  25. Hi - I built the circuit exactly as outlined in the last picture in the article. While the sensor seems to be working, the output isn't as precise as I need it to be. The direct output I'm getting from A0 is around 560, and I need to add about ~150 mmHg of pressure in order to get it to move to the next increment of 561. I want to be able to measure at a resolution of 1 mmHg. Would I be able to do that by running the whole thing through another op-amp? Also separately, where would the optional capacitors go in that last diagram?

    ReplyDelete
  26. Muy buen trabajo. Gracias, muchas gracias.

    ReplyDelete
  27. An observation for posterity. The data sheet for the MPS20N0040D-D has a peculiar pin numbering. The usual convention is for pin 1 to be top left of the chip seen from above with the notch at the top, but it seems to me that the actual pin arrangement in the data sheet shows the pins viewed from below. This may be the explanation for some of the confusion in how it is to be wired up. When I have a moment I really should put an ohm-meter on the pins to determine the "at rest" resistances.

    ReplyDelete
  28. Boa noite. queria tirar uma dúvida: estou interessado nesse sensor de pressão. Você poderia me dizer se eu colocar em um ambiente fechado em que a pressão fique variando durante o dia ele pode fazer a leitura? e outra questão: podendo ser usado. você vende o equipamento pronto? Pergunto porque não é muito minha área, mas estou precisando de um equipamento desse para fazer um teste.

    ReplyDelete
  29. Hi, can we use the same circuit for the Omron PP02 pressure sensor (datasheet https://omronfs.omron.com/en_US/ecb/products/pdf/en-2smpp-02.pdf)? This sensor also has six pins.

    ReplyDelete
  30. very useful information sharing about pressure sensor with a Microcontroller keep it up and making more blogs like this. check it out wyze promo code

    ReplyDelete
  31. Reading your blog was a journey through a landscape of profound ideas. Your ability to weave facts with narrative is exceptional. It's not just a read; it's an experience to savor.
    electronic components shop near me
    electronic parts
    electrical components
    electronics component suppliers
    electronic component suppliers

    ReplyDelete
  32. This comment has been removed by the author.

    ReplyDelete
  33. This comment has been removed by the author.

    ReplyDelete