plcLib (Arduino): Performing Boolean Operations

Boolean logic functions such as AND and OR may be achieved using series / parallel arrangements of switch contacts. For example, two switches in series will give an AND function, since both switches must be closed to complete the circuit. Similarly an OR function may be achieved by two switches connected in parallel, as closing one or more switches will allow power to flow to the next stage.

The basic Boolean logic functions AND, OR, XOR and NOT may be represented in ladder diagram form by using series / parallel combinations of input switches and output contacts, as shown below.

This ladder diagram arrangement may be easily coded, as shown in the following sketch.

#include <plcLib.h>

/* Programmable Logic Controller Library for the Arduino and Compatibles

   AND, OR, XOR and Not - Basic Boolean Logic Functions

   Connections:
   Input - switch connected to input X0 (Arduino pin A0)
   Input - switch connected to input X1 (Arduino pin A1)
   Output - ANDed Output - LED connected to output Y0 (Arduino pin 3)
   Output - ORed Output - LED connected to output Y1 (Arduino pin 5)
   Output - XORed Output - LED connected to output Y2 (Arduino pin 6)
   Output - Inverted Output - LED connected to output Y3 (Arduino pin 9)

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

*/

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

void loop() {
  in(X0);      // Read Input 0
  andBit(X1);  // AND with Input 1
  out(Y0);     // Send result to Output 0

  in(X0);      // Read Input 0
  orBit(X1);   // OR with Input 1
  out(Y1);     // Send result to Output 1
  
  in(X0);      // Read Input 0
  xorBit(X1);  // XOR with Input 1
  out(Y2);     // Send result to Output 2
  
  in(X0);      // Read Input 0
  outNot(Y3);  // Send inverted result to Output 3
}
Source location: File > Examples > plcLib > Logic > AndOrXorNot

If active-low outputs are required then NAND, NOR and XNOR equivalent functions may be created in ladder logic.

Coding of these functions is achieved by replacing the out() instructions of the previous example with the negative logic outNot() equivalent, as shown below.

#include <plcLib.h>

/* Programmable Logic Controller Library for the Arduino and Compatibles

   NAND, NOR, and XNOR - Boolean Logic functions with inverted outputs

   Connections:
   Input - switch connected to input X0 (Arduino pin A0)
   Input - switch connected to input X1 (Arduino pin A1)
   Output - NAND Output - LED connected to output Y0 (Arduino pin 3)
   Output - NOR Output - LED connected to output Y1 (Arduino pin 5)
   Output - XNOR Output - LED connected to output Y2 (Arduino pin 6)

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

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

void loop() {
               // NAND
  in(X0);      // Read Input 0
  andBit(X1);  // AND with Input 1
  outNot(Y0);  // Send result to Output 0 (inverted)

               // NOR
  in(X0);      // Read Input 0
  orBit(X1);   // OR with Input 1
  outNot(Y1);  // Send result to Output 1 (inverted)

               // XNOR
  in(X0);      // Read Input 0
  xorBit(X1);  // XOR with Input 1
  outNot(Y2);  // Send result to Output 2 (inverted)
}
Source location: File > Examples > plcLib > Logic > NandNorXnor

Logical operations may also be performed with one or more of the inputs inverted, as seen here.

An equivalent sketch is shown below.

#include <plcLib.h>

/* Programmable Logic Controller Library for the Arduino and Compatibles

   Inverted Input Logic - Boolean logic operations using inverted inputs
                          (equivalent to normally closed input switches)

   Connections:
   Input - switch connected to input X0 (Arduino pin A0)
   Input - switch connected to input X1 (Arduino pin A1)
   Output - ANDed output - LED connected to output Y0 (Arduino pin 3)
   Output - ORed output - 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 Input 0
  andNotBit(X1);   // AND with Input 1 (inverted)
  out(Y0);         // Send result to Output 0

  in(X0);          // Read Input 0
  orNotBit(X1);    // OR with Input 1 (inverted)
  out(Y1);         // Send result to Output 1
}
Source location: File > Examples > plcLib > Logic > InvertedInputLogic

It is also possible to perform logical operations involving the state of output contacts, which in effect applies feedback from outputs to inputs. This of course is the basis of sequential logic, the simplest of which is the Set-Reset latch – to be considered next.

Related Topics

top