Raspberry Pi Model B+ Audio/Video out

The Raspberry Pi A, and Raspberry Pi B both had a composite video out, on an composite video plug (RCA), the new model B+ uses a 4-pole socket which carries both audio and video signals. This style of connector is sometimes referred to as “TRRS”, which stands for “Tip-Ring-Ring-Sleeve”. The pin out for the Raspberry […]

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 obtain the signal and read this 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 blood pressure in humans 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 40kpa 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 1pa 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 1pa. Popcorn kernels popping exudes roughly 10pa.  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 5V and puts out a 0-25mV 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-25mV 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 amplifier 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 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?

+ Input connects to +5V
- Input connects to 0V
+ Output connects to + In on the Operational Amplifier
- Output connects to – In on the Operational Amplifier

Next we need to calculate the gain required.  We need to change 0v – 25mV into something larger and we also need to account for the voltage offset present (around 2V).  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 2V and 2.2V.   I set the supply voltage to the op-amp as +5V and 0V (single supply mode).  The amplifier then gives out between 57mV and 970mV.  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 0V and 5V.

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 5k 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 1k – I’m hoping it works this well!!

The green square is a simple filter to prevent external 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 200mV to 3.5V 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 design 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 communciations 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 16×2 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:

/*
Alex’s MPS20N0040D-D test Code

A simple piece of code to check the output from an
analogue pressure sensor connected via a difference
amplifier to the A0 input of the arduino

Based on the analogueInOut Serial Example

 */

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

int sensorValue = 0;        // value read from the Sensor
int outputValue = 0;        // Mapped Value for the serial output

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

void loop() {
  // read the analog in value:
  sensorValue = analogRead(analogInPin);            
  // map it to the range of the analog out:
  outputValue = map(sensorValue, 640, 1023, 0, 255); //The zero value of the sensor is around 640 
  // change the analog out value:
  analogWrite(analogOutPin, outputValue);           

  // print the results to the serial monitor:
  Serial.print(“sensor = ” );                       
  Serial.print(sensorValue);      
  Serial.print(“t mapped output = “);      
  Serial.println(outputValue);   

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

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….And then I had a moment of clarity and sucked on the tube connected to the sensor instead of blowing down it – SUCCESS!!!  The reading from the serial monitor varied with the amount of suction provided….One slight hitch….We wanted to measure positive pressure and not vacuum….

So whilst this circuit does work for this sensor and does do everything required to get it working it isn’t what was originally required.  After the time spent designing and getting it working I’m writing it up as a learning exercise…It was a good piece of analogue electronics to get the amplifier designed….pity the sensor doesn’t work as intended….*wry grin*

I’m not going to etch the PCB for this project as there is no real point.  Everything is available to get it working if people are interested.

Cheers

Langster!