Simulating a PLC ladder diagram on arduino

Hello and good day/night (depending on what region your in).. I have a question concerning the matter above,
i would like to create a simple program that the arduino can understand that is base on PLC ladder diagram.
i am more familiar with a ladder diagram that coding but i am trying to make the transition as easy as possible, though i have bought arduino uno for the long time, i truly think that i only able to scratch on the the surface of its capability. However in ladder diagram, i have an intermediate certificate in PLC programming and i really hope that i can use the same logic and principle to make sense of arduino coding.

i know that ( sorry for using ascii art to demonstrate due to not having any plc software at home)
:
on a ladder diagram
------|I1|------ or ------|/I1|----- is an input of I1 and

on arduino coding

int I1=2;
void setup()
{
  pinMode(I1,INPUT);
}
void loop(){}

on PLC
-----(Q1)------ meaning that Q1 is an output

on arduino

int Q1=3;
void setup()
{
  pinMode (Q1, OUTPUT)
}
void loop(){}

on plc

I1 Q1
----||------( ) meaning that when the input I1 high Q1 is high

on arduino

int I1=2;
int Q1=3;
void setup()
{
  pinMode (I1,INPUT);
  pinMode (Q1,OUTPUT);
}
void loop()
{
  boolean StateIn1=digitalRead(I1);
  digitalWrite(Q1, StateIn1);
// or simple yet just digitalWrite (Q1,digitalRead(I1));
}

but my real question is this how can i make the coding for arduino to an equivalent of PLC keep function

like :

I1 I2 Keep1
---| |--||-----ls l
I3 l l
---||---------lr l

according to this program
a high (I1) and a low (I2) with make the output high (keep1)
however the low (I3) will make it the output low

the keep function on a PLC is identical to rs flip flop.

how can i write this ladder diagram into arduino?
please help me, any idea would be great. And i thank anyone in advance to have spend your time read my question and to help me.

I'm not familiar with ladder diagrams so this is likely has a very obvious answer:

a high (I1) and a low (I2) with make the output high (keep1)
however the low (I3) will make it the output low

What happens if I1 is high, I2 is low and I3 is low. How do you resolve the apparent conflict? Latest condition wins?

A set or reset is easiest done with an if without else.

I1 = digitalRead(Input1);
I2 = digitalRead(Input2);
I3 = digitalRead(Input3);

if (I1 && !I2) Q1 = 1;
if (!I3) Q1 = 0;

digitalWrite(Q1, Output1);

If you want to simulate a plc, remember to first read all your inputs, then do your networks and in the end write your outputs...

1 Like

sorry for not clearing that up Karma...
the keep function that i am talking about is an digital equivalent RS flip flop.

meaning that every open and close contact operation in the upper rung ( rung in simple term line) if high with result in the S portion to be high.
if the lower rung is true, the R will set HIgh and the keep will be low. R will always be a priority

sorry wildbill

zatalian thanks a huge lot. i haven't try it yet.
however i think there is a slight typo

digitalWrite(Q1, Output1);

shouldn't it be
digitalWrite(Output1,Q1);

how ever how to make sure that the reset will a priority

When programming plc's, the order in which you program your networks will specify if your set or reset will have priority.
In your case, reset has priority.

In fact, by reading the inputs first, then doing the logic and then writing the outputs you prevent spikes on your output. In your case, the output will never become high.

edit: will never become high if you reset and set at the same time.

thanks a huge bunch, that really clarify everything....

btw is there any way i make a function or sub rutine that can simplify doing the keep funtion.

like

keep( (maybe a name for the memory slot),maybe S, maybe R, );

for instant 
keep(m1,I1,I2)
where as m1 is the memory address for the keep funtion, I1 is the set and I2 is the reset function

There are obvious similarities between the behaviour described by your ladder diagrams and the behaviour described by the Arduino imperative code and I suspect that your conceptual design skills will carry across well. However, I would urge you to write your sketches directly in C++ rather than try to 'write' them as a ladder diagram and then convert. You have clearly understood the basic concepts of variables holding values, and functions containing code that can be called. This is more than some people have when they start Arduino programming.

^^This. There are lots of examples in the IDE to look through that will help. Do you have a particular project in mind?

wildbill, thanks for the reply.
i basically have any particular project in mind but i really keen on using my knowledge in PLC programming into arduino,
i think i have get the keep function figured out.
could you verify this out?

int keep(boolean S, boolean R)
{
  int result;
  if (S== HIGH & R==LOW)
  {
    result = HIGH;
   }
   else 
   {
    result = LOW;
    }
   return result;
}

the only reason for me to use PLC way of programming is that it is something i fully understand, and i know that arduino is more then capable of handling this task due to most of PLC programming is done base on Boolean logic.

is Zatalian way of doing is much simpler?
ii really do like to have a keep function....

how about this?

int keep( int S, int R,)
int result;
if (S && !R) {result =1;}
else {result =0;}
return result;

I'd strongly recommend that you let the ladder diagram stuff go - it'll hamper your understanding of existing code and it'll baffle anyone you go to for help (such as here). For better or worse, the two languages are very different and in my opinion at least, trying to force one to be the other is not going to go well. It could be done that way, but I can see no reason to do so and lots of reasons not to.

I'm not familiar with the thing you call a 'keep' but the behaviour you're describing could be coded like this:

// a high (I1) and a low (I2) with make the output high (keep1)
// however the low (I3) will make it the output low
// in the code below, true corresponds to logic high and false corresponds to logic low

boolean I1, I2, I3, result;
if(I3)
{
    if(I1 && !I2)
    {
        result = true;
    }
    else
    {
        result = false;
    }
}
else
{
    result = false;
}

There are more concise ways to code that calculation, but they are harder to get your head around. For example:

result = I3 && (I2 && !I3);

This 'keep' logic may make sense as a way to build logical behaviour in a PLC, but unless your goal is to make the microcontroller emulate a PLC I wouldn't expect to use this type of thing very often - usually, logical expressions can be expressed much more simply than this. I wouldn't use I1, I2, I3 as variable names either - I'd use names that indicate the purpose of the variables.

Just thinking that this may have already been done in a way so a little googling came up with a few.

Here is one that looked OK.

The original purpose of the PLC ladder logic programming method was to allow the then existing power electrical engineers and technicians to program a PLC application with just the knowledge and experience they used in designing and building power and control relay cabinets. These first came out in the mid to late 1970s. It made heavy use of a primitive CRT based GUI format where they defined 'coils' output and 'contacts' inputs coorosponding to real digital inputs and outputs, and could if required 'wire' them to internal function blocks like counters, shift registers, timers, etc.

Today there is little reason to limit ones self to just creating such ladder logic applications, and most modern PLC offer an alternate 'higher level' programming language to support user created programs. The biggest advantage to using PLCs are there very rugged and reliable construction rather then their ability of being able to be programmed in 'ladder logic'. If one was going to emulate a PLC system with an arduino board it would be pretty dumb in my opinion to not just program the application directly in the arduino C/C++ sketch language, rather then trying to implement a ladder logic interrupter 'programmer'/runtime emulator. Lots of work to do that with little payback benefits, just learn to program in C/C++. :wink:

Lefty

yes retrolefty. i am pretty much sure that if i try programming arduino with the skill i use to program a plc will be an overkill and well defeat the purpose of using arduino right.... urm well for me maybe the thought that i could use the same kind of logic i use for plc can be implement into arduino will be a tremendous help... when i first learn programming a plc, the teacher tell me went progrmming a PLC you only need to know 6 basic things and the rest would be extra for you to create a good enough program for your Machine to run. those 6 basic things are
--| |-- a normally open contact
--||-- a normally close contact
--( )-- a coil or output
a keep function
a timer function
and last but not least is the counter function.

i work as an asistance engineer for the past 2 years and only recently resign so i could continue my study.
anyway while i was working, i have lots of friends that are great PLC programmer dont know a thing about C or any other kind of programming. so what i'm trying to do is to emulate a PLC ladder diagram inside arduino so that most (if not all my programmer friend ) to start using Arduino. this is just a warm up so they know the capability of Arduino and maybe make them more intrigue to learn more about arduino?

i been thinking for the past few night and it seems that i may have found a way to emulate the keep function.
A keep function is just an equivalent to an Rs flip flop or to many electrician or people that know how to keep a relay on with hard wiring is called latch.

the basic for any latch in hard wiring is:
I1 I2 M1
---| |---||------( )
M1i |
---| |-|

let say I1 and I2 are momentary push button, the I1 connection is the normally open while I2 is the normally close part of the switch that we are care about. M1 coil is the coil of a relay and M1i is one of the relay contact.
the operation goes that if someone push button I1, the circuit will be completed and current will flow . ( consider that the left side of the diagram is maybe 12V/5V/24/240V(live) for that matter and the right side is the ground/neutral). this will cause the relay coil(M1) to be energize and one of the relay contact will be close (M1i).Now the current will flow trough the (M1i) and down to the coil thus keeping the relay in the energize state (latch).....
to turn of this latch is as easy as pressing the I2 button, cause it cut the flow of current.
what happen if both the I1 and I2 button being press at the same time?
well because I2 is the one controlling the current flow, the coil will not be energies.in most way this make the circuit have some form of safety features.

how to emulate keep or latch inside arduino in term of programming?
i personally like to use the term keep but due to maybe making the wrong message across so i would use the second most use term to describe this behavior that is Latch.

 int latch(boolean s, boolean r)
{
  boolean result = LOW;
  if ( (s== HIGH || result == HIGH) && r==LOW)
  { 
    result = HIGH;
   }
  if (r == HIGH)
  {
    result =LOW;
  }
 return result;
}