Go Down

Topic: Simulating a PLC ladder diagram on arduino (Read 7 times) previous topic - next topic


Nov 18, 2012, 06:20 pm Last Edit: Nov 18, 2012, 06:23 pm by PeterH Reason: 1
I'm not familiar with the thing you call a 'keep' but the behaviour you're describing could be coded like this:

Code: [Select]

// 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(I1 && !I2)
       result = true;
       result = false;
   result = false;

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

Code: [Select]

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.
I only provide help via the forum - please do not contact me for private consultancy.


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.



Nov 18, 2012, 06:58 pm Last Edit: Nov 18, 2012, 07:02 pm by retrolefty Reason: 1
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++.  ;)



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.

Code: [Select]

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;

Go Up