plcLib (Arduino): Comparing Analogue Values

Analogue comparison commands provide equivalent functionality to an electronic comparator circuit, giving a true / false result based on which of the two tested analogue signals is the larger.

The circuit symbol for an electronic comparator is triangular, with two analogue inputs at the left and a single digital output at the left. A high output is produced if the voltage applied to the upper V+, input terminal is greater than that connected to the lower, V input (called the non-inverting input and the inverting input, respectively). Otherwise, a low output is produced.

Software-based Comparison of Analogue Values

Comparing analogue values in software is essentially a three step process:

  1. Input the first analogue value.
  2. Compare this with a second value.
  3. Output the comparison result to a digital output.

The two available variations of the comparison operation are compareGT() and compareLT() which test whether an input is either greater than or less than a reference value, respectively.

The following example tests whether the analogue voltage on input X0 is greater than that on input X1, setting output Y0 if this is true.

#include <plcLib.h>

/* Programmable Logic Controller Library for the Arduino and Compatibles

   Comparator - Greater than test between two input pins

   Connections:
   Analogue Input - potentiometer connected to input X0 (Arduino pin A0)
   Analogue Input - potentiometer connected to input X1 (Arduino pin A1)
   Digital Output - LED connected to output Y0 (Arduino pin 3)

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

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

void loop() {
  inAnalog(X0);      // Read Analogue Input 0
  compareGT(X1);     // X0 > X1 ?
  out(Y0);           // Y0 = 1 if X0 > X1, Y0 = 0 otherwise

}
Source location: File > Examples > plcLib > AnalogCompare > GreaterThan

It is also possible to compare an analogue input against a fixed reference, as shown in the following example.

#include <plcLib.h>

/* Programmable Logic Controller Library for the Arduino and Compatibles

   Comparator - Less than test between an input and a fixed threshold

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

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

*/

unsigned int threshold = 500;   // Analogue threshold = 500
   
void setup() {
  setupPLC();                   // Setup inputs and outputs
}

void loop() {
  inAnalog(X0);                 // Read Analogue Input 0
  compareLT(threshold);         // X0 < 500?
  out(Y0);                      // Y0 = 1 if X0 < 500, Y0 = 0 otherwise

}
Source location: File > Examples > plcLib > AnalogCompare > LessThanThreshold

A Simple Application

In this example, a pair of comparators are used to test whether an analogue input is either above an upper threshold (greater than 3.5 V), or below a lower threshold (less than 1.5 V), as shown below.

The upper comparator causes its associated LED to light if the input voltage, V1 is greater than a fixed threshold of 3.5 V. Conversely, the lower comparator gives an output if its input voltage is less than its threshold voltage of 1.5 V. (Carefully note the polarity of each comparator input connection to understand how 'greater than' or 'less than' tests are achieved.)

An equivalent sketch is given below, which makes use of both forms of comparator command, together with calculated threshold values based on a 5 V power supply.

#include <plcLib.h>

/* Programmable Logic Controller Library for the Arduino and Compatibles

   Comparator - Maximum / minimum test using fixed threshold values

   Connections:
   Analogue Input - potentiometer connected to input X0 (Arduino pin A0)
   Digital Output - 'High' LED connected to output Y0 (Arduino pin 3)
   Digital Output - 'Low' LED connected to output Y1 (Arduino pin 5)

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

*/

unsigned int lowLimit = 307;    // Analogue lower threshold = 307
                                // (30% of 1024 = 1024 * 0.3 = 307)
                                // Lower threshold voltage = Vsupply * 0.3 
                                // (1.5 V if Vsupply = 5 V)

unsigned int highLimit = 717;   // Analogue lower threshold = 717 
                                // (70% of 1024 = 1024 * 0.7 = 717)
                                // Upper threshold voltage = Vsupply * 0.7
                                // (3.5 V if Vsupply = 5 V)

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

void loop() {
  inAnalog(X0);                 // Read Analogue Input 0
  compareGT(highLimit);         // X0 > upper threshold?
  out(Y0);                      // Y0 = 1 if X0 > 717, Y0 = 0 otherwise

  inAnalog(X0);                 // Read Analogue Input 0
  compareLT(lowLimit);          // X0 < lower threshold?
  out(Y1);                      // Y1 = 1 if X0 < 307, Y1 = 0 otherwise
}
Source location: File > Examples > plcLib > AnalogCompare > MaxMin

Following sections introduce the range of commonly used PLC programming techniques, starting with Instruction List programming.

Related Topics

top