Pressure sensor calculations

Hi everybody,

I am using a piece of code from the Soft Robotics Toolkit webpage, where a pressure sensor is reading the pressure of a deformable silicone tube when pushed upon.

The code is as follows:

const int analogInPin = A8;   //Sets a variable for the pin that is connected to the sensor

int sensorValue = 0;    //Sets a variable for the raw sensor data
float midValue = 0;   //Sets a variable for a number used in calculating PSI
float psi = 0;    //Sets a variable for the final pressure reading

int loopTime = 100; //Sets a variable for how often the pressure is read, in milliseconds

void setup() 
{
  Serial.begin(9600);   //Initializes serial communications
}

void loop() 
{
  sensorValue = analogRead(analogInPin);    //Reads the current value of the sensor
  midValue = map(sensorValue, 320, 345, 0, 100);    //Performs calculations
  psi = map(midValue, 52, 100, 0, 15);    //Performs calculations

  /*
  Serial.print("sensorValue = " );    //Uncomment these lines to see raw sensor data              
  Serial.print(sensorValue); 
  Serial.print("\tmidValue = ");      
  Serial.print(midValue);
  Serial.print("/t");
  */   
  Serial.print("psi = ");   //Prints the results    
  Serial.println(psi);    //Prints the results
  delay(loopTime);     
}

It is working just fine, but I do not understand the calculations to get from voltage to psi in this bit:

  sensorValue = analogRead(analogInPin);    //Reads the current value of the sensor
  midValue = map(sensorValue, 320, 345, 0, 100);    //Performs calculations
  psi = map(midValue, 52, 100, 0, 15);    //Performs calculations

Does anybody have a clue as to what is going on here? I have tried turning to the datasheet for the sensor, but I don't see anything that could help me understand there. The link for the datasheet is here: https://softroboticstoolkit.com/files/sorotoolkit/files/mps20n0040d-s_datasheet.pdf

Thanks in advance for your help!

From the datasheet:
"Low cost"
"Full-scale output voltage 50 - 100 mV"

Which basically means that this sensor is cheap because it doesn't have a build-in instrumentation amp.
Only the more expensive sensors do.

You can't use this sensor without an instrumentation amplifier, like the HX711.
The bare sensor won't give you much more than 25 A/D steps.
Leo..

PS
const int analogInPin = A8; // Is this a Mega?
Might be able to use two Mega analogue inputs in differential mode, with gain (no experience).
Sensor +input(2) to 5volt, -input(5) to ground, +output(3) to one analogue input of the Mega, both -outputs(1,6) to a second analogue input of the Mega.
Leo..

I can give you an idea of what is going on. We will make a few assumptions first. The Analog to Digital converter is a 10 bit converter (2^10). The A/D reference voltage is 5.000 volts. What this means is at the analog input 0 to 5 volts is 0 to 1024 bits or we get 1024 quantization levels. That also tells us, in this case, that since 5 volts is full scale and we get 1024 quantization levels we can say 5 / 1024 = 0.00488 (4.88 mV) resolution. This is where the volts come from, they come from the bit count of the A/D.

Now we look at these few lines of code;

 sensorValue = analogRead(analogInPin);    //Reads the current value of the sensor
 midValue = map(sensorValue, 320, 345, 0, 100);    //Performs calculations
 psi = map(midValue, 52, 100, 0, 15);    //Performs calculations

So we map a bit count between 320 to 345 as 0 to 100. Now 0 - 100 is our sensor value. 320 bits is 1.561 volts and 345 bits is 1.687 volts. Remember 1 bit = 4.88 mV. This becomes the midvalue and again they map that as 52 to 100 = 0 to 15 PSIG.

Bottom line or end result is exactly as Wawa posted above.

Ron

Ron

Thank you so much for your response, it helped a lot!

My sensor circuit consists of the sensor and a dual op-amp, and I get plausible PSI outputs from the Arduino Mega, so I believe it has been set up correctly.
I simply did not understand the functioning of the code, but now I do. I really appreciate your help, thank you.

  • Marlene

Glad you have a handle on the code. Makes a big difference. :slight_smile:

Ron

Hi Ron_Blain. I just read this and got it too.
But why did they map the psi value twice? Why not just psi = map(sensorValue, 333, 345, 0, 15);
Would save a variable and a calculation step
or even psi = map((analogRead(analogInPin), 333, 345, 0, 15); or something like that?
Or is that sort of nesting not allowed?
Or is all that taken care of during compiling and optimised further than that anyway, so that the code just remains more easily readable?
Cheers,
Joe

Or is that sort of nesting not allowed?

It is allowed.

Who can guess what the author of that code was thinking? But a comment like "performs calculations" suggests: not much.

No, as jremington points out it's allowed. Pretty much at the discretion of the person writing the code. Often there are several ways to get the same result in code. Where one or two lines will get something done often some folks may use five or six lines.

Personally I was never a programmer and if I would have needed to rely on my programming skills to eat I likely would have starved to death. Fortunately I had the pleasure of working with some exceptional programmers and let them do their thing. :slight_smile:

Ron