Go Down

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

#### ash901226

##### Nov 18, 2012, 01:34 pm
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)
:
------|I1|------   or  ------|/I1|-----   is an input of I1 and

on arduino coding
Code: [Select]
`int I1=2;void setup(){  pinMode(I1,INPUT);}void loop(){}`

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

on arduino
Code: [Select]
`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: [Select]
`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?

#### wildbill

#1
##### Nov 18, 2012, 01:43 pm
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?

#### zatalian

#2
##### Nov 18, 2012, 01:47 pm
A set or reset is easiest done with an if without else.

Code: [Select]
`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...

#### ash901226

#3
##### Nov 18, 2012, 02:01 pm
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

#4
sorry wildbill

#### ash901226

#5
##### Nov 18, 2012, 02:06 pm
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);

#### ash901226

#6
##### Nov 18, 2012, 02:11 pm
how ever how to make sure that the reset will a priority

#### zatalian

#7
##### Nov 18, 2012, 02:15 pmLast Edit: Nov 18, 2012, 02:20 pm by zatalian Reason: 1
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.

#### ash901226

#8
##### Nov 18, 2012, 02:36 pm
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: [Select]
`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`

#### PeterH

#9
##### Nov 18, 2012, 03:29 pm
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.
I only provide help via the forum - please do not contact me for private consultancy.

#### wildbill

#10
##### Nov 18, 2012, 03:34 pm
^^This. There are lots of examples in the IDE to look through that will help. Do you have a particular project in mind?

#### ash901226

#11
##### Nov 18, 2012, 03:52 pm
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: [Select]
`int keep(boolean S, boolean R){  int result;  if (S== HIGH & R==LOW)  {    result = HIGH;   }   else    {    result = LOW;    }   return result;}  `

#### ash901226

#12
##### Nov 18, 2012, 03:55 pm
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.

#### ash901226

#13
##### Nov 18, 2012, 04:03 pm
is Zatalian way of doing is much simpler?
ii really do like to have a keep function....

Code: [Select]
`int keep( int S, int R,)int result;if (S && !R) {result =1;}else {result =0;}return result;`

#### wildbill

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

Go Up

Please enter a valid email to subscribe