plcLib (Arduino): Edge Triggered Pulses

Version 1.1 of the plcLib software introduces the ability to generate edge triggered pulses, the output of which are active for a single scan cycle only.

This can be useful in a range of scenarios, such as triggering an output once only, ensuring an output lasts for a shorter duration than an input, or preventing a latch-based system from being locked in one position due to a faulty external switch.

A simple edge triggered system is shown below.

This may be represented as shown in the following ladder diagram, with rising or falling edges on input X0 being used to momentarily set outputs Y0 and Y1, respectively.

An equivalent sketch is shown below.

#include <plcLib.h>

/* Programmable Logic Controller Library for the Arduino and Compatibles

   Generate rising-edge and falling-edge pulses using a digital input

   Connections:
   Clock input - switch connected to input X0 (Arduino pin A0)
   Rising-edge output - LED connected to output Y0 (Arduino pin 3)
   Falling-edge output - LED connected to output Y1 (Arduino pin 5)

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

*/

Pulse pulse1;              // Create a pulse object called 'pulse1'

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

void loop() {
  in(X0);                  // Read switch connected to Input 0 and
  pulse1.inClock();        // connect it to the clock input
  
  pulse1.rising();         // Read rising edge
  out(Y0);                 // Output rising edge for 1 scan cycle only
  
  pulse1.falling();        // Read falling edge
  out(Y1);                 // Output falling edge for 1 scan cycle only

  delay(50);               // Slow down scan cycle to enable viewing pulses
                           // (remove this delay in the final code)
}
Source location: File > Examples > plcLib > Pulse > PulseInput

Examining the above listing, a pulse object called 'pulse1' is first created. External input X0 is then connected to the clock input of the previously created pulse. Finally, rising and falling edge signals are used to drive outputs Y0 and Y1 respectively.

Output pulses last for a single scan cycle only, so a 50 millisecond time delay has been added to the above sketch to 'stretch' the output pulses long enough to be visible when connected to LEDs. This delay should be removed after testing to avoid slowing down the scan cycle.

Creating an Edge Triggered Bistable

A simple set reset latch may be created using several different methods, as demonstrated in the Latching Outputs section. Given the potential for faulty input switches mentioned earlier, it is useful to consider what would happen to a latch-based circuit if both the Set and Reset inputs were simultaneously activated, or if one of the external input switches became stuck in the 'On' position?

These potential issues may to a large extent be overcome by using edge triggered signals to drive the latch inputs, as shown in the following ladder diagram.

An equivalent sketch is shown below.

#include <plcLib.h>

/* Programmable Logic Controller Library for the Arduino and Compatibles

   Set or reset an output using edge-triggered inputs

   Connections:
   Set input - switch connected to input X0 (Arduino pin A0)
   Reset input - switch connected to input X1 (Arduino pin A1)
   Latched output - LED connected to output Y0 (Arduino pin 3)

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

*/

Pulse setPulse;              // Create a pulse object to set the latch
Pulse resetPulse;            // Create a pulse object to reset the latch

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

void loop() {
  in(X0);                  // Read switch connected to Input 0 and
  setPulse.inClock();      // connect it to the set pulse clock input

  in(X1);                  // Read switch connected to Input 1 and
  resetPulse.inClock();    // connect it to reset pulse clock input

  setPulse.rising();       // Read rising edge of X0
  set(Y0);                 // Set Y0 to 1 on using rising edge of X0
  
  resetPulse.rising();     // Read rising edge of X1
  reset(Y0);               // Reset Y0 to 0 on using rising edge of X1
}
Source location: File > Examples > plcLib > Pulse > SetResetEdge

Related Topics

top