plcLib (Arduino): Advanced Concepts

This section explains the internal operation of the plcLib software. An understanding of these concepts is not necessary for basic programming, but may be useful when developing more advanced applications, or for understanding why things aren't quite working as you expected.

How the Software Works

A PLC operates by repeatedly reading inputs, performing calculations, and then sending the results to the outputs. This process is known as the scan cycle. A typical ladder diagram based application is 'scanned' one rung at a time, from left to right, starting at the top rung and working progressively downwards. This process repeats continuously.

Each rung of the ladder may be thought of as a parallel process, which receives its own share of the processor time as the scan cycle repeatedly executes. Hence PLC-based applications demonstrate simple parallel processing capabilities, but without the need to resort to advanced programming techniques.

For basic operation, the PLC software uses a single variable called scanValue to hold its running calculation result as each branch is solved. Consider the following code snippet to see how this works for single bit values:

void loop() {
  in(X0);      // Read Input 0
  out(Y0);     // Send to Output 0
}

The single bit input command in(X0) reads digital input pin X0 and stores its result in the scanValue variable as 1 or 0. A subsequent bit output command out(Y0) simply reads the scanValue variable and sends this value to digital output pin Y0. This process repeats as each rung of the ladder diagram is calculated, with scanValue repeatedly initialised, updated and then discarded as the ladder logic program executes.

The process is similar for an analogue input, which is read from an analogue to digital converter as a 10-bit value in the range 0-1023 using the inAnalog() command – as illustrated by the following code snippet.

void loop() {
  inAnalog(X0);    // Read Analogue Input 0
  outPWM(Y0);      // Send to Output 0 as PWM waveform
}

The same scanValue variable is used to hold this analogue value, which is not a problem as the variable is actually an unsigned integer variable type. The plcLib software automatically handles the scaling of any 'analogue' outputs, so an outPWM() command is scaled to be in the range 0-255 (8-bit binary), while an outServo() command would use a value scaled in the range 0-179 (representing 180° of rotation).

The next section explains how the above mentioned scanValue variable may be used, and discusses possible applications of user-created variables in sketches.

Related Topics

top