# Relays sequencer

Hi Everyone

I’m Newby in Arduino trying to make this control:

I’ve three relays to sequence in pairs, transitions will occur when analog values raise or drops, want a output to 3 digits led display showing analog input scaled value.

Case Analog Relay 1 Relay 2 Relay 3
1 >0 and <300 1 1 1
2 >300 and <400 0 1 1
3 >400 0 0 1
4 >300 and <400 1 0 1
5 >400 1 0 0
6 >300 and <400 1 1 0
7 >400 0 1 0
8 >400 >5 Min 0 0 0

please check the attachment for more details.

Thanks for any help

OP's image

Curious... what are you controlling? If your values change quickly, you may not want to use a mechanical relay. Also, you’ll want to filter your input to keep the relays from chattering.

Pat

PM sent

!!! Another thing to note, 4-20mA is a very narrow and low input range. The 10-bit Arduino ADC only measures to a precision of 4.88mV (per bit), so your 4-20mV input will be converted to analogRead values of 1,2,3,4 (and will jitter around).

I’d rethink this. There are a lot of good alternative ADC options.

@Silente - what are your thoughts on this?

Pat.

I dont think about hardware. If someone tell “I must use it” means that he has only it, so for me hardware cant be changed. Aniways I didnt understand how he things to move the relays.
He has 3 groups of values: <300, 300/400, >400, but he is talking about 8 different possible comands (the numbers from 0 to 7 written in binary). How can it be done?? How can I understand when I must print 4 or 7, for example?

• There must be another variable, or some criterion, to tell it which case to select of 2, 4 or 6 since they are all the same sensor input (300-400) or of cases 3, 5 and 7 which are all the over 400 scenario. Perhaps it starts in 2 or 3 then moves to the others by time? Who knows?
• And what does the 4th behaviour “initialize system” actually mean?

But once that’s clarified, the coding for this is trivial.

Hi people, thanks for view my project.

This project isn’t to make any profit, just to show somebody the Arduino power, he argues about Arduino and PLC’s , I know PLC programing and can do this project but I know that Arduino can do it too.

Hi jubukraa, thanks for the interest, Values showed in table >300 and <400 are pressure units, it means the transition between cases, for example, we know that 4-20 ma sensor is raising it values is like a pressure switch is closed, it makes a pump run, and when values are dropping is like a pressure switch is open, pump off and of course pressure is dropping.

Values in clear green are decimal to send to a register in order to store there the case, means that I can take digital output from that register directly in binary, in PLC I can send 6 to N7:0 address, the use N7:0/0 to command pump 3, N7:0/1 to control pump 2 and N7:0/2 to control pump 1, I want make this same in Arduino, also indicate that always 2 pumps will work while pressure is going up, >300 and <400, if pressure gets over 400 only 1 pump will run and is pressure is between 0 and 300 all three pumps will run, last detail is if pressure is over 400 for more than 5 minutes then all three pumps go off.

Patduino, thanks for viewing my post, forgot mention that relay modules are SSR, but the frequency of work is very low, maybe one operation every 15 minutes, about 4-20 ma, don’t know how it works on Arduino, that’s why I’m here, looking for help, there is a 4-20ma to 0-5v converter module, need scale values in analog input to 0-450 scale and show it in 3 digital 7 segments displays, 4th behavior is Push button single ON/OFF to make system ON, it can be illuminated, also if stop system remember lasts pumps running and restart the same case or step, this last isn’t very important.

Any suggestions will be welcomed.

Thanks again.

It's still not clear, and there's no real point in explaining that you would send "this" to "that" PLC register. You need to explain it in umambiguous English.

We all understand that if we're on line 2, relays 2 and 3 are on. If we're on line 4, it's relays 1 and 3. Line 6 means relays 1 and 2. Coding that is trivial.

What we (or maybe it's just I?) don't understand is how the system will choose between lines 2, 4 or 6 when all you're telling us is that the pressure is between 300 and 400. Your table shows only one input column, and 3 of the lines are identical in that column. (And there are 3 more lines identical for the over 400 case.)

There has to be some other criterion to distinguish line 2 from 4 from 6 (and 3 from 5 from 7.)

If I as a human need to turn the pumps on with switches, and you tell me that if the pressure is between 3 and 400 I must go to the line that says 300-400 and turn on the relays it tells me, I will say "But there are 3 such lines, which must I choose". What will you say to that?

Forget PLC-speak and Arduino code: tell me how I as a human will know which line to choose if the pressure is between 3 and 400. (Your table needs another column, I reckon, or the column you have needs breaking down further.)

Ok, hand on a sec, I'm going to take a wild guess here, and say that I think this works through the table in order, perhaps to share the load among the pumps?

• So system starts up, pressure 0, all pumps on.
• Pressure builds, pump 1 goes off.
• Bit more pressure, pump 2 goes off.
• So the pressure drops... pump 1 comes in (3 still on)
• Pressure builds, 3 goes off
• .... etc

Is that correct? (We shouldn't be guessing, and if that's right then I don't think you actually said that's how it works, perhaps assuming there is some prior knowledge of PLCs (or that style of table) here.)

Btw, I wonder if this is the right sub-forum for this? Do you actually want someone to team up with you "officially" for this (maybe with \$\$\$ changing hands)? If not, perhaps ask a moderator (click Report to mod) to move it to Project Guidance or Programming Questions?

Hello people

i'm so sorry for my english, some times forget that isn't my mother languages and pretend to know much but sometimes remember that.

Jubukraa is correct, is a sequencer, case 1 is when initiate cycle, case 2 to 7 will loop while pressure up and drop, case 8 will occur when air load stops and pumps raise 400 PSI and if this pressure stay for 5 minutes case 8 will pop and all pumps goes off.

Thanks again everyone.

Ok well I coded it on the assumption that's how it was meant to work. I was almost finished when domestic duty got in the way (washing dishes etc etc ) so will post code later when I'm done.

I also added a new case 0, where it will go from Arduino power up, then I have a push button to start the sequence, and another button to stop any time.

What's supposed to happen if the pressure ever drops below 300? Should it shut off or go back to state 1 with all the pumps on?

(I don't have a 3 digit led display, so it's all in the serial monitor. I also don't have a 4-20mA pressure sensor so I just used a pot on an analog pin.)

Meantime, since you are a...

....Newby in Arduino

... if you haven't already, you should be working through the examples here, and for this project especially have a look at switch/case.

Washing the dishes was too exciting and it messed up my brain so I forgot to come back and post the code, sorry about that.

Have a look at this.

I don’t have your sensor, so I just used a pot on an analog pin. It’s not “scaled” as you asked, since I’m not sure what you meant, and anyway to have it scaled to 400 (whatever you meant) doesn’t make sense when you also expect it to go over 400. So I just used the actual analog reading of 0 to 1023. But the low and high thresholds are in the code as variables. I tested with 300 and 600 in fact; dividing the range into 3rds more or less; obviously you can set those to whatever you want.

I also don’t have your display; run it with the serial monitor open.

The start and stop buttons are wired from their pins to ground.

I have the pressure reading taken every 1/2 second (readingInterval) and also just use that to blink a led so you can see the code hasn’t locked up.

What’s supposed to happen if the pressure ever drops below 300? Should it shut off or go back to state 1 with all the pumps on?

… so I don’t check for that ever.

For testing, it goes to case 8 after 5 seconds (highTimeout).

If your relays go on with a low pin, just change the high and low in these lines:

``````#define ON HIGH
#define OFF LOW
``````
``````// https://forum.arduino.cc/index.php?topic=680662
// 29 apr 2020

// 3 pumps with pressure sensor

//relay logic
// tested with leds from i/o pin to ground
#define ON HIGH
#define OFF LOW

const byte relay1Pin = 2;
const byte relay2Pin = 3;
const byte relay3Pin = 4;
const byte startButton = 8;
const byte stopButton = 9;
const byte pressureSensor = A0;
byte currentState;

unsigned long wentHighAtMillis;
byte timedOutInState;
long highTimeout = 5000; //short time for testing, set to 5mins in real life
int lowPressure = 300;
int highPressure = 600; // easier for my test with a pot, gives a bigger movement range
// value not scaled at all, just using the actual analog value 0 to 1023
int pressure;

//timer for taking pressure and blinking the pulse led
bool ledState;

void setup()
{
Serial.begin(9600);
Serial.println("setup() ... ");
Serial.println("3 pumps with pressure sensor");

pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, ledState);
pinMode(relay1Pin, OUTPUT);
pinMode(relay2Pin, OUTPUT);
pinMode(relay3Pin, OUTPUT);
pinMode(startButton, INPUT_PULLUP);
pinMode(stopButton, INPUT_PULLUP);

Serial.println("setup() done");
Serial.println(" ");
currentState = 0;
Serial.println("Entering state 0, press Start");
}

void loop()
{
{
Serial.println("Stop pressed, returning to state 0");
currentState = 0;
}
doRelays();
} //loop

void doRelays()
{
switch (currentState) //0 to 8: 000,111,011,001,101,100,110,010,000
{
case 0: //000
digitalWrite(relay1Pin, OFF);
digitalWrite(relay2Pin, OFF);
digitalWrite(relay3Pin, OFF);

{
currentState = 1;
Serial.println("Entering state 1");
}
break;

case 1://111
digitalWrite(relay1Pin, ON);
digitalWrite(relay2Pin, ON);
digitalWrite(relay3Pin, ON);

if (pressure > lowPressure)
{
currentState = 2;
Serial.println("Entering state 2");
}
break;

case 2://011
digitalWrite(relay1Pin, OFF);
digitalWrite(relay2Pin, ON);
digitalWrite(relay3Pin, ON);

if (pressure > highPressure)
{
currentState = 3;
Serial.print("Entering state 3 at "); Serial.println(millis());
wentHighAtMillis = millis();
}
break;

case 3://001
digitalWrite(relay1Pin, OFF);
digitalWrite(relay2Pin, OFF);
digitalWrite(relay3Pin, ON);

if (pressure < highPressure)
{
currentState = 4;
Serial.println("Entering state 4");
}

if (millis() - wentHighAtMillis >= (unsigned long) highTimeout)
{
timedOutInState = 3;
currentState = 8;
Serial.print("Entering state 8 at "); Serial.println(millis());
}
break;

case 4://101
digitalWrite(relay1Pin, ON);
digitalWrite(relay2Pin, OFF);
digitalWrite(relay3Pin, ON);

if (pressure > highPressure)
{
currentState = 5;
Serial.print("Entering state 5 at "); Serial.println(millis());
wentHighAtMillis = millis();
}
break;

case 5://100
digitalWrite(relay1Pin, ON);
digitalWrite(relay2Pin, OFF);
digitalWrite(relay3Pin, OFF);

if (pressure < highPressure)
{
currentState = 6;
Serial.println("Entering state 6");
}

if (millis() - wentHighAtMillis >= (unsigned long) highTimeout)
{
timedOutInState = 5;
currentState = 8;
Serial.print("Entering state 8 at "); Serial.println(millis());
}
break;

case 6://110
digitalWrite(relay1Pin, ON);
digitalWrite(relay2Pin, ON);
digitalWrite(relay3Pin, OFF);

if (pressure > highPressure)
{
currentState = 7;
Serial.print("Entering state 7 at "); Serial.println(millis());
wentHighAtMillis = millis();
}
break;

case 7://010
digitalWrite(relay1Pin, OFF);
digitalWrite(relay2Pin, ON);
digitalWrite(relay3Pin, OFF);

if (pressure < highPressure)
{
currentState = 2;
Serial.println("Entering state 2");
}

if (millis() - wentHighAtMillis >= (unsigned long) highTimeout)
{
timedOutInState = 7;
currentState = 8;
Serial.print("Entering state 8 at "); Serial.println(millis());
}
break;

case 8://000
digitalWrite(relay1Pin, OFF);
digitalWrite(relay2Pin, OFF);
digitalWrite(relay3Pin, OFF);

if (pressure < highPressure)
{
if (timedOutInState == 3)
{
currentState = 4;
Serial.println("Entering state 4");
}

if (timedOutInState == 5)
{
currentState = 6;
Serial.println("Entering state 6");
}

if (timedOutInState == 7)
{
currentState = 2;
Serial.println("Entering state 2");
}
}
break;

}//switch
}//doRelays

{
{
Serial.println(pressure);