Assistance with pressure transducer switching relay under certain conditions.

Hi all,

First of all its taken me weeks of researching before deciding to turn to others for help on this forum.
I’m not new to the Arduino platform but I’am by no means an expert I understand basic concepts but most of my projects I have had to find an existing sketch written by someone else and modify it to my needs.

in this case my sketch is based around this sketch found here so full credit goes to Reinier van der Lee for providing the original sketch freely on the internet

Basically I want to use a Pressure transducer to switch a relay which in turn is connected to a high pressure diaphragm pump at a certain pressure so I can pressurise the system to say 6 bar and the pump will turn off, then when the pressure drops to say 5 bar or below the pump will turn back on and re pressurise the system to 6 bar. I also want to include excessive high and low pressure warnings to the switch statements.

I’m trying to use switch case statements to determine when the pump will turn on however I did have my sensor voltage converted to psi (as in the original sketch)

void loop() {
  float sensorVoltage = analogRead(A0);   // read the sensor voltage
  int pressure = ((sensorVoltage-95)/204)*50;

however I understand bar more than psi so added this line to convert psi to bar

void loop() {
  float sensorVoltage = analogRead(A0);   // read the sensor voltage
  int pressure = ((sensorVoltage-95)/204)*50; 
  pressure/14.5038; // 14.5038 is the conversion unit from Psi to Bar comment out for psi

I wanted to have the int pressure as float pressure to give me a more accurate reading of the system pressure but I can’t use the float datatype with the switch case statements.

I did have it set up as if else statements but that wasn’t working either, I only changed to switch case statements today (as an aside I have never used them in a sketch before so I might not fully understand how they work)

also when the screen refreshes when I apply pressure to the transducer it does change from “Normal” to “Charging” but leaves certian characters on the LCD when it changes back, I assume I need an lcd.clear command but im not sure where to put it.

Here is the sketch I’m currently working on there are issues with it. I’m asking for help as I just can’t find the answers I’m looking for anywhere else.

// "Vinduino" portable PSI sensor code V1.00
// Date March 7, 2015
// Reinier van der Lee
// www.vanderleevineyard.com

// include the library code only for LCD display version
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int pumpRelay = 7;

void setup() 
{
  pinMode, (pumpRelay = OUTPUT); 
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);

}

void loop() {
  float sensorVoltage = analogRead(A0);   // read the sensor voltage
  int pressure = ((sensorVoltage-95)/204)*50; 
  pressure/14.5038; // 14.5038 is the conversion unit from Psi to Bar comment out for psi
  lcd.setCursor (0,0);
  lcd.print ("System P ");
  lcd.print (pressure);
  lcd.print (" bar"); //comment out for psi
  //lcd.print (" PSI"); // comment out for bar
   delay (1000);

 switch (pressure) {
    case 1: // pressure <= 5 bar ie low system pressure
    digitalWrite (pumpRelay, HIGH);
    lcd.setCursor (4,1);
    lcd.print ("charging");
      break;
    case 2: // pressure == 6 bar normal opperating pressure
    digitalWrite (pumpRelay,LOW);
    lcd.setCursor (5,1);
    lcd.print ("Normal");
    break;
    case 3:// pressure > 7 bar WARNING HIGH SYSTEM PRESSURE implement some warning 
    lcd.setCursor (1,1);
    lcd.print ("WARNING HIGH P");
      break;
    case 4: // pressure < 4 bar WARNING LOW SYSTEM PRESSURE implement some warning 
    lcd.setCursor (1,1);
    lcd.print ("WARNING LOW P");
    break;
    default: //no system pressure
    digitalWrite (pumpRelay, LOW);
    lcd.setCursor (1,1);
    lcd.print ("NO SYSTEM P");
    break;
  }
}

As an end note I’am a lvl 3 NVQ plumber with my vented and unvented system qualification so I understand pressurised systems and my project will have other mechanical safety devices in place.

many thanks and I highly appreciate any time you put into an answer.

Harry

pressure/14.5038;

This is called an expression in C. It calculates a number. But unfortunately it doesn't do anything with that number. The result is thrown away because you didn't say what to do with the result. Are you trying to store the result back into the variable called pressure?

I think you meant:pressure = pressure/14.5038;

You also have the problem that pressure was declared as an integer so it can only hold the values 0, 1, 2, 3, 4, 5, 6... You can't store 5.5BAR in that variable.

Having one variable storing something with different units at different times is going to cause problems. When you use that variable elsewhere in the program, how do you know if that point in time contains BAR or PSI? Create a new variable with a new name to store pressure in BAR. Then use that elsewhere in your program:

float pressureBAR = pressure/14.5038;

Thank you

As I said I'm no expert with coding in arduino so sometime I get confused with all the different terms.

I wanted to have the int pressure as float pressure to give me a more accurate reading of the system pressure but I can't use the float datatype with the switch case statements.

as it comes up with error message

Pressure_Sensor.ino: In function 'void loop()': Pressure_Sensor:33: error: switch quantity not an integer

does this mean i cant use the switch case statements or should I change it back to and if else?

I have changed the code on your recommendations:

void loop() {
  float sensorVoltage = analogRead(A0);   // read the sensor voltage
  int pressure = ((sensorVoltage-95)/204)*50; 
  pressure = pressure/14.5038; // 14.5038 is the conversion unit from Psi to Bar comment out for psi
  float pressureBar = pressure/14.5038;//
  lcd.setCursor (0,0);
  lcd.print ("System P ");
  lcd.print (pressure);
  lcd.print (" bar"); //comment out for psi
  //lcd.print (" PSI"); // comment out for bar
   delay (1000);

pressure = pressure/14.5038; // 14.5038 is the conversion unit from Psi to Bar comment out for psi float pressureBar = pressure/14.5038;//

Whoops! You've converted twice. This is exactly the sort of error that is avoided by using different names.

Switch-case is probably not very useful since it only works on integers. You don't want to have to specify different cases for 0BAR, 1BAR, 2BAR; where all of those would likely do the same thing. Just use regular if() statements to define the ranges.

const float PressurePumpOn = 5.5;//BAR - pressure when pump comes on
const float PressurePumpOff = 6.5;//BAR - pressure when pump comes off
const float PressureAlarmHigh = 6.6;//BAR - high-pressure alarm (pump failed to switch off when commanded)
...
void loop() {
  //read pressure, convert to pressureBar

  if(pressureBar > PressureAlarmHigh) {
    soundAlarm();
  } else if(pressureBar > PressurePumpOff) {
    pumpOff();
  } else if(pressureBar > PressurePumpOn) {
    //do nothing, either we are pumping or we are nor pumping but we don't change
  } else { //pressure is below the pump-on pressure
    pumpOn();
  }
}

Thanks for your help I should have stuck with the if else statements.

I will continue to tinker

again thanks for your time

Harry

I hope I understood correctly but I fear I have not as the relay won’t switch on when the pressure increases

here is the new code

// "Vinduino" portable PSI sensor code V1.00
// Date March 7, 2015
// Reinier van der Lee
// www.vanderleevineyard.com

// include the library code only for LCD display version
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const float PressurePumpOn = 5.50;//BAR - pressure when pump comes on
const float PressurePumpOff = 6.50;//BAR - pressure when pump comes off
const float PressureAlarmHigh = 7.00;//BAR - high-pressure alarm (pump failed to switch off when commanded)
const float PressureAlarmLow = 4.00; // BAR - low pressure alarm (pump failed to switch on when commanded)
int pumpRelay = 6;

void setup() 
{
  pinMode, (pumpRelay = OUTPUT); 
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);

}

void loop() {
  float sensorVoltage = analogRead(A0);   // read the sensor voltage
  float pressure = ((sensorVoltage-95)/204)*50; 
  float pressureBar = pressure/14.5038; // 14.5038 is the conversion unit from Psi to Bar
  lcd.setCursor (0,0);
  lcd.print ("Sys P ");
  lcd.print (pressureBar);
  lcd.print (" bar");
   delay (1000);

  if(pressureBar > PressureAlarmHigh) { // pressure alarm high is 7.0bar
    digitalWrite (pumpRelay, LOW);
    lcd.setCursor (0,1);
    lcd.print ("!HIGH PRESSURE!"); // system pressure is too high pump failed to switch off  
  } else if(pressureBar > PressurePumpOff) { // pressurePumpOff is 6.5bar
    digitalWrite (pumpRelay, LOW);
    lcd.setCursor (0,1);
    lcd.print ("Pressure OK");   // current system pressure ok
  } else if(pressureBar < PressurePumpOn) { //PressurePumpOn is 5.5bar
    digitalWrite (pumpRelay, HIGH);
    lcd.setCursor (0,1);
    lcd.print ("Charging Sys P"); // system is charging PressurePumpOff (6.5bar)
  } else(pressureBar < PressureAlarmLow); {
    lcd.setCursor (0,1); 
    lcd.print ("!LOW PRESSURE!");  //pressure is below the pump-on pressure
  }
}

Here's one clue:

    lcd.print ("Pressure OK    ");   // current system pressure ok

Can you see what I did there?

You code will never reach the low pressure alarm. That pressure is below the pump-on pressure, so it will try to turn the pump on and never gets to the else part of that if().

When you expect the relay to come on (but it doesn't) what is the LCD showing at that moment?

And don't do this in the middle:   delay (1000); You are just throwing away millions of cycles of useful computation when you do that. If you want to stop the LCD flickering, then don't clear it every time. Use my clue.

OK so having a low pressure alarm serves no purpose as the code wont be able to get to the else statement.

its now currently just displaying after I updated the sketch (see below)

Sys P 0.05 bar
Charging Sys P!

but it will say low pressure regardless of the pressure I am applying

I expect the relay to switch on when the pressure reads 5.50bar+
when it goes above 7.00 bar it looks as though the text is written underneath the “Charging Sys P” text but the relay never switches.

the
lcd.print ("Pressure OK "); // current system pressure ok

probably should say something different I’m not too sure (sorry if I’m being a bit dense)

I have now removed the delay, that was left over from the original sketch

// original code from www.vanderleevineyard.com

// include the library code only for LCD display version
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int pumpRelay = 6;
const float PressurePumpOn = 5.50;//BAR - pressure when pump comes on
const float PressurePumpOff = 6.50;//BAR - pressure when pump comes off
const float PressureAlarmHigh = 7.00;//BAR - high-pressure alarm (pump failed to switch off when commanded)


void setup() 
{
  pinMode, (pumpRelay = OUTPUT); 
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  
  
}

void loop() {
  float sensorVoltage = analogRead(A0);   // read the sensor voltage
  float pressure = ((sensorVoltage-95)/204)*50; 
  float pressureBar = pressure/14.5038; // 14.5038 is the conversion unit from Psi to Bar
  lcd.setCursor (0,0);
  lcd.print ("Sys P ");
  lcd.print (pressureBar);
  lcd.print (" bar");

  if(pressureBar > PressureAlarmHigh) { // pressure alarm high is at 7.0bar
    digitalWrite (pumpRelay, LOW);
    lcd.setCursor (0,1);
    lcd.print ("!HIGH PRESSURE!"); // system pressure is too high pump failed to switch off 
  }
  else if(pressureBar > PressurePumpOff) { // pressurePumpOff is 6.5bar
    digitalWrite (pumpRelay, LOW);
    lcd.setCursor (0,1);
    lcd.print ("Pressure OK ");   // current system pressure ok
  } 
  else (pressureBar < PressurePumpOn); { //PressurePumpOn is 5.5bar
    digitalWrite (pumpRelay, HIGH);
    lcd.setCursor (0,1);
    lcd.print ("Charging Sys P"); // system is charging PressurePumpOff (6.5bar)
  } 
}
float pressure = ((sensorVoltage-95)/204)*50;

This won't work as you expect, because of integer division. Read up on the topic when you get a chance. Replace it with a floating point operation.

float pressure = (sensorVoltage-95)*0.245;

ah I see that makes sense, in the original sketch the pressure value was an integer in psi when I changed the code to bar I wanted to be able to show variable in bar. I didn't change the math correctly

I will do more reading around the subject

thanks

Here's one clue: lcd.print ("Pressure OK "); // current system pressure ok

Can you see what I did there?

sorry yes I see what you did you made the Pressure OK fill up the bottom line of the screen makes sense thanks

Hi all,

thank you for your help already I feel I’m getting somewhere now however I have 2 issues

  1. the relay isn’t switching on regardless of the pressure
  2. the LCD display ghosts ( that’s the only way I can describe it) and I have attached 2 pictures of the LCD and will attach the code in this post

if I should be posting this in a new thread let me know, I’am a bit green when it comes to using forums

I have attached a couple of pics of the ghosting issue

I have tried to use a lcd.clear(); but where ever I put it it seems to mess the screen up more

sorry to keep bothering you guys I do appreciate the help

// original code from www.vanderleevineyard.com

// include the library code only for LCD display version
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const float PressurePumpOn = 2.50;//BAR - pressure when pump comes on
const float PressurePumpOff = 4.50;//BAR - pressure when pump comes off
const float PressureAlarmHigh = 5.00;//BAR - high-pressure alarm (pump failed to switch off when commanded)
int pumpRelay = 13;

void setup() 
{
  pinMode, (pumpRelay = OUTPUT); 
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  lcd.clear();
  lcd.setCursor (0,0);
  lcd.print ("Sys P ");
  
  
}

void loop() {
  float sensorVoltage = analogRead(A0);   // read the sensor voltage
  float pressure = (sensorVoltage-95)*0.245; 
  float pressureBar = pressure/14.5038; // 14.5038 is the conversion unit from Psi to Bar
  lcd.setCursor (6,0);
  lcd.print (pressureBar);
  lcd.print (" bar ");

  if(pressureBar > PressureAlarmHigh) { 
    digitalWrite (pumpRelay, LOW);
    lcd.setCursor (0,1);
    lcd.print (" !HIGH PRESSURE!"); 
  }
  else if(pressureBar > PressurePumpOff){ 
    digitalWrite (pumpRelay, LOW);
    lcd.setCursor (0,1);
    lcd.print ("  Pressure OK   ");    
  }
   else(pressureBar < PressurePumpOn); { 
    digitalWrite (pumpRelay, HIGH);
    lcd.setCursor (0,1);
    lcd.print (" Charging Sys P "); 
  
   }
  
   
   
}

I don't use the LCD routines, but LCD displays are slow. You may have to insert short delay() statements (e.g. 10 milliseconds) to make sure the display has time to update.

The loop() function is executed hundreds of thousands of times per second.

In fact it is better not to write to the display more often than necessary. In my programs, I keep the current display information somewhere, a separate function to update the display, and maintain a flag or state variable that keeps track of when "something changed, time to rewrite the display".

The logic of your if, else if and else is confusing. I don't know what that should do and would instead use switch() case constructs.

I did attempt to use the switch case statement but as it only works on integers I couldn’t use it

I will try to add a delay and if that doesn’t work I will have a trawl through the interwebs and do some more studying on the subject

Thanks

Harry

You can certainly make the logic clearer by using mutually exclusive, simple if constructs like this:

  if (pressure < P1) do_underpressure_things();
  if (pressure >= P1 && pressure < P2) do_OK_things();
  if (pressure >= P2) do_overpressure_things();

Hi all
I thought I would update you all on my progress to see if what I have done is ok to do and It may also help someone else who has a similar project.

here is my sketch I re wrote the whole thing as adding to and taking bits away from my previous attempt was becoming very confusing. I don’t know if its a good idea to chain together all those if statements but it seems to work well I probably should use an else in there somewhere.
Its a bit messy and could do with some tidying up but I will tinker with it another day because I’m pretty tired now :sleeping: .

let me know what you think and any suggestions or modifications you may have.

Night

Harry

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int pump = 7;
const int pressureTrans = A0; 


void setup()
{
  pinMode(pump, OUTPUT);
  lcd.begin(16,2);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print ("Pressure ");  
}

void loop()
{
  int sensorVoltage= analogRead(pressureTrans);
  int pressure= (sensorVoltage-95)*0.245;
  int pPsi = pressure;
  float pBar= pressure/14.5038;  
  lcd.setCursor (10,0);
  lcd.print (pBar);
  lcd.print (" b   ");
  
  if (pPsi <1){                   // the Relay board im using is switched low (for some reason) hence why high and low are switched around in this sketch
   digitalWrite(pump,LOW);        // if pressure is less than 1 attempt to turn on pump and display LOW  PRESSURE warning 
   lcd.setCursor(0,1);
   lcd.print("!!LOW PRESSURE!!");
  }
  if (pPsi>= 2 && pPsi<= 79){     // if pressure is between 2 and 79 psi turn pump on and display pressurising 
   digitalWrite(pump, LOW);
   lcd.setCursor(0,1);
   lcd.print("  Pressurising  ");
  } 
  if (pPsi>= 80 && pPsi<= 84){   // if pressure is between 80 and 84 display pressure ok
   lcd.setCursor(0,1);
   lcd.print("  Pressure OK  ");
  }
  if (pPsi == 85){                 // if pressure is 85 psi turn pump off and display pressure ok         
   digitalWrite(pump,HIGH);
   lcd.setCursor(0,1);
   lcd.print("  Pressure OK   ");
  }
  if (pPsi >= 90){                 // if pressure is greater than 90 psi attempt to turn pump off and display HIGH PRESSURE warning
   digitalWrite (pump, HIGH);         
   lcd.setCursor(0,1);
   lcd.print("!HIGH  PRESSURE!");   

  }
}
  if (pPsi == 85){                 // if pressure is 85 psi turn pump off and display pressure ok

Better to use

  if (pPsi >= 85){                 // if pressure is 85 psi or higher turn pump off and display pressure ok

It WILL happen that the pressure will increase past 85 before you measure it. The next statement will still catch values that are too high (>= 90).

I probably should use an else in there somewhere.

You could always just throw one in and see what happens.