plcLib (Arduino): Using Time Delays

The plcLib software has commands to produce time delays based on the activation or deactivation of an input signal (the timerOn() and timerOff() commands), and can also produce an output pulse of fixed duration (the timerPulse() command). All commands accept two parameters which are firstly the name of the elapsed timer variable and secondly the required time delay in milliseconds.

Avoid using the Arduino's own delay() command wherever possible, as this halts the PLC scan cycle for the duration of the delay, and only supports a single delay being active at any time. See the Advanced Concepts section for more details.

Producing a Turn-on Delay

The timerOn() command delays activating an output until the input has been continuously active for the specified period of time in milliseconds.

A timer-based system may be be conveniently represented using a block diagram or function block diagram, as shown in the following example.

The two on-delay timers provide independent activation time delays of 2 seconds and 4 seconds on Outputs 0 and 1, respectively. Output 0 becomes active after a signal connected to Input 0 has been active for 2 seconds, while Output 1 requires Input 1 to have been active for a period of 4 seconds. The equivalent sketch is shown below.

#include <plcLib.h>

/* Programmable Logic Controller Library for the Arduino and Compatibles

   Turn-on Delay - Delays turning an output on after an input is applied

   Connections:
   Input - switch connected to input X0 (Arduino pin A0)
   Input - switch connected to input X1 (Arduino pin A1)
   Output with 2 s delay - LED connected to output Y0 (Arduino pin 3)
   Output with 4 s delay - LED connected to output Y1 (Arduino pin 5)

   Software and Documentation:
   http://www.electronics-micros.com/software-hardware/plclib-arduino/

*/

unsigned long TIMER0 = 0;  // Variable to hold elapsed time for Timer 0
unsigned long TIMER1 = 0;  // Variable to hold elapsed time for Timer 1

void setup() {
  setupPLC();              // Setup inputs and outputs
}

void loop() {
  in(X0);                  // Read Input 0
  timerOn(TIMER0, 2000);   // 2 second delay
  out(Y0);                 // Output to Output 0

  in(X1);                  // Read Input 1
  timerOn(TIMER1, 4000);   // 4 second delay
  out(Y1);                 // Output to Output 1
}
Source location: File > Examples > plcLib > TimeDelays > DelayOn
Notice that each timer makes internal use of an elapsed timer variable of type 'unsigned long', which must be declared at the start of the program.

Switch Debouncing

A common problem with switch- or keypad-based systems is contact bounce in which a single key press causes the contacts to physically bounce several times – over a period of several milliseconds – before settling. This mechanical effect can be eliminated using an on-delay timer having a suitable time delay, as shown in the following example.

#include <plcLib.h>

/* Programmable Logic Controller Library for the Arduino and Compatibles

   Switch Debounce - Delays a switch input by 10 ms to remove contact bounce

   Connections:
   Input - switch connected to input X0 (Arduino pin A0)
   Output - debounced output - LED connected to output Y0 (Arduino pin 3)

   Software and Documentation:
   http://www.electronics-micros.com/software-hardware/plclib-arduino/

*/

unsigned long TIMER0 = 0;  // Define variable used to hold timer 0 elapsed time

void setup() {
  setupPLC();              // Setup inputs and outputs
}

void loop() {
  in(X0);                  // Read Input 0
  timerOn(TIMER0, 10);     // 10 ms delay
  out(Y0);                 // Output to Output 0
}
Source location: File > Examples > plcLib > TimeDelays > SwitchDebounce

Creating a Turn-off Delay

A turn-off delay timer becomes active immediately and then delays turning-off for a given period after the controlling input is removed.

An application of the turn-off delay is 'pulse stretching' in which a brief input signal is expanded to have a minimum pulse width. The following function block diagram shows an off-delay timer with a 2 second turn-off delay.

The equivalent sketch is given below.

#include <plcLib.h>

/* Programmable Logic Controller Library for the Arduino and Compatibles

   Turn-off Delay - Delays turning an output off after an input is removed

   Connections:
   Input - switch connected to input X0 (Arduino pin A0)
   Output - LED connected to output Y0 (Arduino pin 3)

   Software and Documentation:
   http://www.electronics-micros.com/software-hardware/plclib-arduino/

*/

unsigned long TIMER0 = 0;  // Variable to hold elapsed time for Timer 0

void setup() {
  setupPLC();              // Setup inputs and outputs
}

void loop() {
  in(X0);                  // Read Input 0
  timerOff(TIMER0, 2000);  // 2 second turn-off delay
  out(Y0);                 // Output to Output 0
}
Source location: File > Examples > plcLib > TimeDelays > DelayOff

Creating a Fixed Duration Pulse

The timerPulse() command (available with Version 1.0 or later) creates a fixed width output pulse when triggered by a brief input pulse. This is equivalent to an electronic monostable circuit.

An example sketch is shown below.

#include <plcLib.h>

/* Programmable Logic Controller Library for the Arduino and Compatibles

   Fixed Pulse - Activates an output for a fixed period after a momentary input is applied

   Connections:
   Input - switch connected to input X0 (Arduino pin A0)
   Output with 2 s pulse - LED connected to output Y0 (Arduino pin 3)

   Software and Documentation:
   http://www.electronics-micros.com/software-hardware/plclib-arduino/

*/

unsigned long TIMER0 = 0;     // Variable to hold elapsed time for Timer 0

void setup() {
  setupPLC();                 // Setup inputs and outputs
}

void loop() {
  in(X0);                     // Read Input 0
  timerPulse(TIMER0, 2000);   // 2 second pulse
  out(Y0);                    // Output to Output 0

}
Source location: File > Examples > plcLib > TimeDelays > FixedPulse
In Version 1.1 or later, operation of the timerPulse command is modified to be edge triggered rather than level triggered. Hence the timer is re-triggered by the first low to high transition of the trigger input after the previous pulse has ended. (This now agrees with the standard behaviour in IEC standard 61131-3.)

The next section extends the time delay concepts introduced here to create continuously repeating waveforms.

Related Topics

top