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 pressure in a tank.  In order to do that we need to know how to connect it up and how 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 would be the weight of a £5 note (or a dollar bill) on a table is roughly equivalent to 1 pa. Popcorn kernels popping exudes roughly 10 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 would be very low.  What needs to be done is to amplify the output of the pressure sensor in order to record the output 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:

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("\toutput = ");      

  // wait 500 milliseconds before the next loop
  // for the analog-to-digital converter to settle
  // after the last reading:

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 £5.00 which is roughly $7.10 - 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!