Go Down

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

#### PeterH

#15
##### Nov 18, 2012, 06:20 pmLast 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 lowboolean 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:

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.

#### justone

#16
##### Nov 18, 2012, 06:44 pm
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.

http://electronics4dogs.blogspot.com/2011/07/ldmicro-and-arduino.html

#### retrolefty

#17
##### Nov 18, 2012, 06:58 pmLast 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++.

Lefty

#### ash901226

#18
##### Nov 19, 2012, 11:02 am
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?

#### ash901226

#19
##### Nov 19, 2012, 11:53 am
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;} `

#### PeterH

#20
##### Nov 19, 2012, 02:18 pm
If you want this result value to persist after your calculation then it needs to be a static variable, or declared outside the function. All the variables declared inside the function are discarded once the function returns.

Again, though, this is a very contrived way to evaluate expressions in an Arduino and I do not think you will be doing your co-workers any favors if you encourage them to design their Arduino sketches as if they were PLC programs. Using 'C' and C++ code you have far more flexibility and can produce far simpler solutions than you would achieve with the PLC approach you're using. Better IMO to describe the behaviour you're trying to achieve and then write it directly in 'C'/C++.

There are hundreds of different programming languages and types of computer and controller and the value comes from knowing what they're capable of and how to use them most effectively - not how to make them all look and behave like the first thing you learned.

#### ash901226

#21
##### Nov 19, 2012, 03:31 pm
sorry for my last code. i have try the code, i can compile well but cant be execute according to the principle that talk before.
this is the one that work after i have spend time making it and testing it to be working well.

this is just an example:
Code: [Select]
`/* trying to set arduino latch funtion connect pin 2 to a momentary push botton and the other leg to groundpin2 -----./.----gnd    ./. is a momentary switch    repeat connection on pin3    since most arduino have a LED connected to pin 13, i will use this to varify the code*/boolean result = LOW;void setup(){  pinMode(2,INPUT); //setting pin 2 as input  digitalWrite(2,HIGH);// activating pin 2 internal pull up  pinMode(3,INPUT);// setting pin 3 as input   digitalWrite(3,HIGH);//activating pin 3 internal pull up  pinMode(13, HIGH);// setting pin 13 as output}void loop(){  int Set = !digitalRead(2);  int Reset =!digitalRead(3);  int K=latch(Set,Reset);  digitalWrite(13,K);}int latch(boolean s, boolean r){  if ( (s== HIGH || result == HIGH) && r==LOW)  {     result = HIGH;   }  if (r == HIGH)  {    result =LOW;  } return result;} `

#### zatalian

#22
##### Nov 19, 2012, 03:47 pm
A keep function (or SetReset, or ResetSet, ...) is a function with a memory... You can't program it with local variables. You either need a global variable or a parameter.
You probably need something like this, but i haven't tested it...

Code: [Select]
`#define I1 1#define I2 2#define Q3 3boolean Input1, Input2, Output1, M1;void setup(){  pinMode(I1, INPUT);  pinMode(I2, INPUT);  pinMode(Q3, OUTPUT);    M1 = 0;}void loop(){  Input1 = digitalRead(I1);  Input2 = digitalRead(I2);    Output1 = keep(Input1, Input2, &M1);    digitalWrite(Output1, Q3);}boolean keep(boolean Set, boolean Reset, boolean *Memory){  if (Reset == HIGH) *Memory = LOW;  else if (Set == HIGH) *Memory = HIGH;    return *Memory;}`

#23
##### Nov 19, 2012, 03:48 pm
Quote
the only reason for me to use PLC way of programming is that it is something i fully understand

Limiting what you do to what you already know is a bad idea, learn new things.

If you insist on programming the Arduino in Ladder logic then what you should look for is something that pre-processses ladder logic into C code.

Somebody seems to have done this here;
http://electronics4dogs.blogspot.ie/2011/07/ldmicro-and-arduino.html

However even if you were going to use something like this I would suggest you need to learn how to program the Arduino fully in C first, otherwise what is happening will just be "magic" and you will not really know what is going on.

#### ash901226

#24
##### Nov 19, 2012, 05:19 pm
Zatalian thanks for the code. it works but theres a slight Typo in the code
int the part
Code: [Select]
`digitalWrite(Output1, Q3);`

it should have been
Code: [Select]
`digitalWrite (Q3, Output1);`

there's only one problem with this function of the one i post before,
if let say that i would like to call keep function a couple of time,
any term that use the same term will exhibit the same result.

#### kf2qd

#25
##### Nov 19, 2012, 07:06 pm
To those who have never used ladder logic and been involved in troubleshooting a machine where it has been used it would seem to be a rather archaic and feeble system. But for those who have used it it provides a great deal of ease for troubleshooting and an easy to decode code for learning about how the controlled machine works.

Ladder logic is a graphical language and can produce very complex control over very complex machines. The basic elements are the examine true ( ---|/|---) and examine false (---| |---) and the OR which is a branch. Various PLCs allow various complexities of the branching and some allow more complex coding than others.

Ladder is used for programming PLCs in the US while the STL languge is used in Europe and Japan. Some PLC programming packages will convert between the 2, there are others where code can be written in STL that can't be converted to Ladder. STL has a look and feel that is very close to Assembly language. Well written code in Ladder or STL if fairly easy to decypher and work with, while poorly written STL code is just as much a nightmare as poorly written code in any other system. Ladder could be considerred a structured language as there are only certain things you can do in certain places - outputs can only be on the right rail, inputs go from the left rail to the right rail. Unfinished rungs are an error.

Imagine writing a statementin C with over 50 AND, OR and NOT. Piece of cake with ladder.

Being graphical it is very easy to animate what is happening in a rung, with those elements that are true being highlighted and the path from left to right is easily seenand requies a lot less analysis than getting multiple dumps of several variable. I have written ladder programs running to over 500 rungs, 48 inputs, 32 outputs (along with a couple axis drives) 4 analog inputs and 4 analog outputs. To have coded it in C and then debugged it would have taken much longer and would have been much harder to modify when the specs changed for a specific machine.

Ladder still rocks and is a powerful tool for process control and machine control.

#### zatalian

#26
##### Nov 20, 2012, 08:55 am

there's only one problem with this function of the one i post before,
if let say that i would like to call keep function a couple of time,
any term that use the same term will exhibit the same result.

If you want to use more than 1 keep function, you will need more than 1 "memory bit" variable...

Code: [Select]
`Output1 = keep(Input1, Input2, &M1);Output2 = keep(Input3, Input4, &M2);`

I have to agree with the others here that trying to rewrite ladder to c is not the best way to go... You are making things more difficult than it should be. Everything you want can be solved with some simple bool & integer logic and some if statements. Even the counters you want...

But i understand that you try to stick to ladder though... people tend to forget that programming is only a means to reach a goal and that we're not all programmers in the first place.

#27
##### Nov 20, 2012, 11:15 pm
Quote
Ladder still rocks and is a powerful tool for process control and machine control.

Absolutely, ladder logic is a very easy to use yet powerful tool for solving many problems.
However it is not a good idea to devise a solution to your problem in ladder logic and then implement it in C/C++, which is what ash901226 seems to be trying to do.

Likewise if your solution is devised in C/C++ implement it in C/C++, don't tie yourself in knots trying to convert to ladder.

#### ash901226

#28
##### Nov 21, 2012, 10:35 am
well i got to say to all that commented on this post that i do think and i do agree that i do need to learn to write in C/ C++ but for now i just want to make the transition a bit more simple. that's why i want to emulate the knowledge i have in ladder diagram into arduino. I do how ever see the importance of learning and properly programming arduino with C/C++ due to its base language as C/C++. Its like a Japanese guy trying to speak German to German guy. Sure maybe the Japanese know a little bit of German and maybe they could carry on a full conversation but the fact is they truly cant understand each other. i mean they full intention and all well at lease until the Japanese guy learn to speak proper German. As you can see i am that i am trying to do now be that Japanese guy that know a little bit of the German language. But over time i hope that i will be able to learn and master the language well enough until i can seriously use C/C++ as my main language

#### uncle

#29
##### Feb 22, 2013, 05:32 pm
Hi friends,

I was trying to do something like latching in ladder logic
and here is my result. Code is very simple because i am two months playing with Arduino. I like it.
2                   3                    8

---||-----|--------||---------------( )
8      |
---||-----|

8                                       9
---||-------------------------------( )

Code: [Select]
`int pin2 = 2;int pin3 = 3;int pin4 = 4;int pin5 = 5;int led8 = 8;int led9 = 9;int led10 = 10;int button2 = 0;int button3 = 0;int button4 = 0;int button5 = 0;int light8 = 0;int light9 = 0;int light10 = 0;void setup(){  pinMode(pin2, INPUT);  pinMode(pin3, INPUT);  pinMode(pin4, INPUT);  pinMode(pin5, INPUT);  pinMode(led8, OUTPUT);  pinMode(led9, OUTPUT);  pinMode(led10, OUTPUT);}void loop(){  button2 = digitalRead(pin2);  button3 = digitalRead(pin3);  button4 = digitalRead(pin4);  button5 = digitalRead(pin5);  light8=digitalRead(led8);  light9=digitalRead(led9);  light10=digitalRead(led10);  if(button2 == HIGH || light8 == HIGH && button3 == LOW)  {    digitalWrite(led8, HIGH);  }  else if (button3 == HIGH)  {    digitalWrite(led8, LOW);  }  if(light8 == HIGH && button4 == HIGH)  {    digitalWrite(led9, HIGH);  }  else if(light8 == LOW)  {    digitalWrite(led9, LOW);  }}`

Go Up