Go Down

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


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.


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...

Either use a ladder to c translator as suggested or forget about the ladder structure.

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.


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.

If your solution is in ladder then implement it in ladder, not C or C++.
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. 


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


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);

  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