Analog Sensor Setpoint

Hi all, so I have an analog air pressure sensor hooked up to my Arduino converting the read voltage to PSI.

Basically I want it to read the PSI then when I press a button I want it to use that PSI as a set point then have actions triggering two output dependant if the read value drops below or above the set PSI.

Is this possible and if so can anyone point me in the right direction?

-detect button press, save value to a variable
-consequently, compare input with variable and take appropriate action

Piece of cake.

You asked almost the same question an hour earlier here : Pressure Monitoring System - #4 by lloydowen - Project Guidance - Arduino Forum
That is okay, but it would be better if you would have told in the other topic that you started a new topic for that.

Do you have the pressure in a variable ? A 'float' variable ? Can you show your sketch between code tags ?

Create a variable and store the pressure in it when the button is pressed.
Compare that with the current pressure.

(while I was writing this, aarg wrote almost the same)

Yeah sorry (I was trying to keep the programming questions separate)!

This is my code so far (I've adopted it from a tutorial I found online...

void setup() {
    Serial.begin(9600);
}
 
void loop(){
    float pressure = readPressure(A1);
    float millibars = pressure/100;
 
    Serial.println();
    Serial.print("Pressure = ");
    Serial.print(pressure);
    Serial.println(" pascals");
    Serial.print("Pressure = ");
    Serial.print(millibars);
    Serial.println(" millibars");
    delay(1000);
}
 
/* Reads pressure from the given pin.
* Returns a value in Pascals
*/
float readPressure(int pin){
    int pressureValue = analogRead(pin);
    float pressure=((pressureValue/1024.0)+0.095)/0.000009;
    return pressure;
}

Create a global variable, for example: float setpoint;
A global variable is created above the setup() function, that variable will keep its value as long as the Arduino is running (and the reset button is not pressed).
When a button is pressed, set the current pressure into the variable.

Thanks! I'll give it a go and report back :slight_smile:

Hi again, so I've been playing about with this all week. I have the Arduino reading and printing the voltage both to the LCD and via Serial. I'm having some trouble converting the voltage into pressure though :frowning:

If anyone would be so kind to take a look I'd be so appreciative!!!!

So here's the code I've got so far, could someone show me how to then turn that voltage into a pressure (in PSI) and output it to the LCD? (Well I could do that part myself)

I've attached an excel file with the formula calculated which shows voltage against pressure etc just can't get it work in my code.

// the setup routine runs once when you press reset:

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  // initialize lcd
  lcd.begin(16, 2);
  lcd.setCursor(1, 0);
  lcd.print("Mattress Inflator");
}

// the loop routine runs over and over again forever:
void loop() {
  //Set cursor to second row pixel 1
  lcd.setCursor(0, 1);
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage = sensorValue * (5.0 / 1023.0);
  // print out the value you read:
  Serial.println(voltage);
  //Print the voltage to the LCD screen:
  lcd.print("V=");
  lcd.print(voltage);
  // Refresh Screen only every second
  delay(1000);
  // Turn voltage into pressure

  delay(500);
  lcd.setCursor(7, 1);
  lcd.print("PSI=");
  lcd.print(pressure);
}

PRESSURE SENSOR VDD.xlsx.zip (7.44 KB)

That is in the datasheet of the sensor. Do you use a AP2 sensor ? but I can find two different sensors that are called "AP2". I can't open a xlsx file.

Thank you for the reply - Yes I've found the formula in the datasheet. However the subject of the formula is not what I want... I've tried re-arranging it and it doesn't work out.... I've attached an image of the formula calculated for Vout= but obviously I want P(pressure)=

Nothing attached ... :confused:

Sorry! Try again..

Ok so I've rearranged the formula successfully!!! YAY! But now I need to enter that into my code... Can anyone help me?

Screen Shot 2015-06-04 at 18.32.52.png

I don't know what to do with that formula.
What is alpha ? What is beta ? what is Pe ? what is Te ?
Could you tell which sensor you have.

The Arduino can do floating point. I suppose Vdd is the power to the sensor. Is that 5V ?

int rawADC = analogRead(A0);   // 0...1023 for 0...5V
float Vout = (float) rawADC / 1024.0 * 5.0;  // 1024 steps for 0 up to 5V.
float alpha = 0.1
float beta = 23.8;
float Pe = 100.3;
float Te = -11.4;
float Vdd = 5.0;
float pressure = ( Vout / ( alpha * Vdd ) ) - ( beta * Pe * Te );

Alpha = 0.036
Beta= 0.04
Pe (Pressure Error)= 0.375
Te (temperature error)= 0.1

I Have the AP20 025KG pressure code

So now I've inserted this into my code... I can't try it until tomorrow but I'm hoping it'll work! Thank you so much!!!! :slight_smile:

 float Vout = (float) sensorValue / 1024.0 * 5.0; //1024 Steps from 0V to 5V.
  float alpha = 0.036; //Alpha Value
  float beta = 0.04; //Beta Value
  float Pe = 0.375; //Pressure Error Value
  float Te = -11.4; // Temprature Error Value
  float Vdd = 5.0; // Voltage in
  float pressure = ( Vout / (alpha * Vdd) ) - ( beta * Pe * Te ); // Calculating Pressure from given variables in KPA
  float psi = (pressure * 0.145037738); // Calculating PSI from the given pressure
  lcd.setCursor(7, 1);
  lcd.print("PSI=");
  lcd.print(psi);
  delay(500);

That is the Fujikura AP20 25K.
I have been reading about it, and it is a analog pressure sensor for 5V and 25kPa (which is very sensitive).
It is sold in the us by Servoflo.

Info : AP2 & AG2 Series Calibrated Low Cost Pressure Sensor - Pressure Sensors - Mass Flow Sensors - Humidity Sensors - Micropumps
Manufacturer introduction page : http://www.fujikura.co.jp/eng/f-news/2038611_4207.html

According to the info at Servoflo, the sensor is linear, has a span of 4.5V, and a offset of 0.2V.
That means that the 25kPa is linear equal to the 4.5V (with the offset of 0.2V).
With that info, I have a totally different calculation. You can try them both :stuck_out_tongue: Perhaps they have the same outcome.

int rawADC = analogRead(A0);   // 0...1023 for 0...5V
float Vout = (float) rawADC / 1024.0 * 5.0;  // 1024 steps for 0 up to 5V.
float pressure = ( Vout - 0.2 ) / 4.5 * 25.0;   // pressure in kPa, substract offset, 4.5V is 25kPa.

Are you serious about "psi", why not "kPa" ?

Thanks for all your help! I'll get back with some results tomorrow :slight_smile:

Ok so that code worked great! had to tweak some numbers to get it as accurate as I could but now its great! So I've wired up a push button and have the code reading out the value of the button press via serial. Now how do I make it so when that button is pressed it saves that value to a floating variable?

Here's my code for the button press:

  int buttonState = digitalRead(pushButton);
  Serial.println(buttonState);
  delay(1);

Make a global variable above the setup() function.
That variable will keep its value as long as the Arduino is running.

I don't know if the digitalRead() is HIGH or LOW when the button is pressed.
The name of the global variable is 'setpoint'.

// global variables
float setpoint = 0.0;

void setup() {
 ...
}

void loop() {
  
  if ( buttonstate == LOW )    // or HIGH, I don't know
    setpoint = pressure;
}

Wow didn't think it was that easy! Great!! I may have some more questions over the weekend lol :frowning: I'm learning so much these things are great! I'll want to do some logic next somehow...

Basically I'll want if pressure from the set point drops from its value then activate output # but if drops to a certain PSI range then do nothing... if that makes sense...