Pages: [1] 2 3   Go Down
Author Topic: Simulating a PLC ladder diagram on arduino  (Read 6558 times)
0 Members and 1 Guest are viewing this topic.
Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Code:
int I1=2;
void setup()
{
  pinMode(I1,INPUT);
}
void loop(){}


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


on arduino
Code:
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
Code:
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.

Logged

New Jersey
Online Online
Faraday Member
**
Karma: 67
Posts: 3677
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Quote
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?
Logged

Offline Offline
Newbie
*
Karma: 1
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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...
Logged

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

sorry wildbill
Logged

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

zatalian thanks a huge lot. i haven't try it yet.
however i think there is a slight typo
Quote
digitalWrite(Q1, Output1);

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

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 1
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: November 18, 2012, 08:20:25 am by zatalian » Logged

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Code:
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
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12631
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

I only provide help via the forum - please do not contact me for private consultancy.

New Jersey
Online Online
Faraday Member
**
Karma: 67
Posts: 3677
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Code:
int keep(boolean S, boolean R)
{
  int result;
  if (S== HIGH & R==LOW)
  {
    result = HIGH;
   }
   else
   {
    result = LOW;
    }
   return result;
}
 

Logged

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

how about this?
Code:
int keep( int S, int R,)
int result;
if (S && !R) {result =1;}
else {result =0;}
return result;
Logged

New Jersey
Online Online
Faraday Member
**
Karma: 67
Posts: 3677
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1] 2 3   Go Up
Jump to: