plcLib (Arduino): Latching Outputs

You can latch a momentary input, causing it to remain active (or set) until it needs to be cancelled (or reset). Three different approaches are available, as described in the following sections.

Latch with Discrete Components

The Set-Reset latch is one of the mainstays of electronics, and is the simplest of sequential logic circuits – most often seen as a pair of cross connected NAND or NOR gates. It is also quite easy to create a self latching circuit using just a relay (an electromagnetically operated switch) and a few other components, as seen in the following relay logic circuit.

Examining the circuit at the left, firstly notice that the two switches are wired in parallel, which is of course a logical OR arrangement. However, to begin with, current does not flow through either path! The upper branch is off, as the push-to-make switch is not pressed. The lower switch is a push-to-break type, but this path is also off, being blocked by the relay contacts which are in the off position (down).

A momentary press of the Set input switch allows power to reach the relay coil which activates, moving the relay contacts to their On position (up). Current now flows through the lower branch, so the relay remains enabled, even when the Set input is released. The relay remains active until the Reset input switch is pressed, hence disconnecting the relay coil and returning the relay contacts to their Off position.

Set Reset latches often have normal and inverted outputs. This function may easily be added, if required, by wiring a second lamp via the unused set of switch contacts, but with opposite polarity to the main output.

An equivalent Arduino sketch is shown below.

#include <plcLib.h>

/* Programmable Logic Controller Library for the Arduino and Compatibles

   Latch using Discrete Components - Self latching circuit with Q and Not Q outputs

   Connections:
   Input - Set - switch connected to input X0 (Arduino pin A0)
   Input - Reset - switch connected to input X1 (Arduino pin A1)
   Output - Q - LED connected to output Y0 (Arduino pin 3)
   Output - NotQ - LED connected to output Y1 (Arduino pin 5)

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

*/

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

void loop() {
  in(X0);           // Read switch connected to Input 0 (Set)
  orBit(Y0);        // Self latch using Output 0 (Q)
  andNotBit(X1);    // Reset latch using Input 1 (Reset)
  out(Y0);          // Output to Output 0 (Q)

  in(Y0);           // Read Q output 
  outNot(Y1);       // Produce inverted output on Output 1 (Not Q)
}
Source location: File > Examples > plcLib > Latch > LatchDiscreteComponents

The above self-latching circuit works well, but is rather verbose. A simplified version is shown in the next section.

Using the Latch Command

The latch() command is functionally identical to the self latching arrangement seen above, but requires a minimum of two lines of code. An equivalent function block diagram representation is shown below, the first variant having normal and inverted outputs, and the second with only a single normal output.

The following sketch shows a latch with both normal and inverted outputs, although the latter two lines used to generate the inverted output are optional.

#include <plcLib.h>

/* Programmable Logic Controller Library for the Arduino and Compatibles

   Latch Command - Set Reset latch with Q and NotQ outputs, based on the 'latch' command

   Connections:
   Input - Set - switch connected to input X0 (Arduino pin A0)
   Input - Reset - switch connected to input X1 (Arduino pin A1)
   Output - Q - LED connected to output Y0 (Arduino pin 3)
   Output - NotQ - LED connected to output Y1 (Arduino pin 5)

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

*/

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

void loop() {
  in(X0);              // Read switch connected to Input 0 (Set input)
  latch(Y0, X1);       // Latch, Q = Output 0, Reset = Input 1
  
  in(Y0);              // Read Q output and generate NotQ on Output 1
  outNot(Y1);          // (These two lines are optional)
}
Source location: File > Examples > plcLib > Latch > LatchCommand

Notice that the Set input to the latch is taken from the preceding value from the same 'rung' of the ladder diagram (reading input X0 in this case). The command takes two arguments which are the Q output and the Reset input respectively.

Using the Set and Reset Commands

An alternative method of creating a latched output is to use separate set() and reset() commands, as shown in the following sketch.

#include <plcLib.h>

/* Programmable Logic Controller Library for the Arduino and Compatibles

   Using Set and Reset commands to create a Set-Reset Latch

   Connections:
   Input - Set - switch connected to input X0 (Arduino pin A0)
   Input - Reset - switch connected to input X1 (Arduino pin A1)
   Output - Q - LED connected to output Y0 (Arduino pin 3)
   Output - NotQ - LED connected to output Y1 (Arduino pin 5)

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

*/

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

void loop() {
  in(X0);              // Read switch connected to Input 0 (Set input)
  set(Y0);             // Set Y0 to 1 if X0 = 1, leave Y0 unaltered otherwise

  in(X1);              // Read switch connected to X1
  reset(Y0);           // Clear Y0 to 0 if X1 = 1, leave Y0 unaltered otherwise
}
Source location: File > Examples > plcLib > Latch > SetResetCommands
The set() and reset() commands are available in Version 0.7 or later of the plcLib library.

A letter is added to the standard output symbol indicating whether the output is a 'set' or 'reset' type, as shown in the equivalent ladder diagram.

This method allows separate logic to control the enabling and disabling of a latched output, which is often convenient. A sequential function chart is a typical application, in which these commands may be used to control the transitions between steps in a sequence.

Related Topics

top