plcLib (Arduino): Working with Analogue Signals

The inAnalog() command reads an analogue input. This may then be used to control a continuously variable output value, such as the brightness of an LED, the speed of a motor, or the position of a servo. Scaling of inputs and outputs is performed automatically by the plcLib software.

The scanValue global variable holds the inputted value, which is in the range 0-1023 for an analogue input. This is automatically scaled in the range 0-255 or 0-179 when used to control a PWM or servo output, respectively.

Controlling LED Brightness using PWM

Linking the inAnalog() command to the outPWM() command, allows a PWM output to be controlled from an analogue input. As an example, the following sketch reads a potentiometer connected to input X0 and produces a repeating pulse waveform with a variable duty cycle on output Y0.

#include <plcLib.h>

/* Programmable Logic Controller Library for the Arduino and Compatibles

   PWM (Pulse Width Modulation) - Analogue control of a PWM output

   Connections:
   Input - potentiometer 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/

*/

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

void loop() {
  inAnalog(X0);    // Read Analogue Input 0
  outPWM(Y0);      // Send to Output 0 as PWM waveform
}
Source location: File > Examples > plcLib > InputOutput > PWM

Controlling the Speed and Direction of a Motor

The Arduino Motor Shield is based on a commercially available H-bridge driver IC, allowing the speed and direction of up to two DC motors to be controlled. A possible hardware connection is shown below.

Two motor channels are available – Channel A and Channel B – and the shield also supports a small number of Tinkerkit compatible input and output connectors (plus two TWI / I2C interfaces not considered here). The above image shows a linear potentiometer connected to pin X2 (A2), used to control the motor speed via PWM, and a tilt switch controlling the motor direction, linked to pin X3 (A3).

The following example controls the speed and direction of a motor connected to Channel A.

#include <plcLib.h>

/* Programmable Logic Controller Library for the Arduino and Compatibles

   Motor Channel A - Simple Motor Control on Arduino Motor Shield Channel A

   Connections:
   Input - Speed - potentiometer connected to input X2 (Arduino pin A2)
   Input - Direction - switch connected to input X3 (Arduino pin A3)
   Output - Channel A Direction (DIRA) - Arduino pin 12
   Output - Channel A PWM (PWMA) - Arduino pin 3
   Output - Channel A Brake (BRAKEA) - Arduino pin 9

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

*/

// Variables
unsigned int RUN = 0;  // Disable brake

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

                       // Turn off Channel A Brake
  in(RUN);             // Read RUN variable (0 = brake off)
  out(BRAKEA);         // Output to BRAKEA
}

void loop() {
                       // Control direction of motor 
  in(X3);              // Read switch connected to Input 3
  out(DIRA);           // Output to DIRA (motor direction)

                       // Read Analog Input 2 and send to Channel A PWM
  inAnalog(X2);        // Read from potentiometer connected to Analogue Input 2
  outPWM(PWMA);        // Output to PWMA (motor speed)
}
Source location: File > Examples > plcLib > Motor > MotorChannelA
See the Motor section of the example files for an equivalent sketch to control a motor connected to Channel B.

Notice that a number of predefined variables are available to simplify the coding of motor control software as far as possible, as summarised below:

  • BRAKEA and BRAKEB enable or disable the brake on each channel (0 = brake off).
  • DIRA and DIRB set the forward or reverse direction of each motor.
  • PWMA and PWMB control the motor speed of their related channels.

Position Control Using Servos

Controlling a servo is slightly more complex, due to the need to work with the Servo library which is supplied as a standard part of the Arduino environment. The outServo() replaces the outPWM() command used previously, and is defined locally, as shown below.

#include <Servo.h>
#include <plcLib.h>

/* Programmable Logic Controller Library for the Arduino and Compatibles

   Single Servo - Controlling the position of a servo using a potentiometer 

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

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

*/

Servo servo1;           // Create a servo object to control a single servo

extern int scanValue;	// Link to scanValue defined in PLC library file

void setup() {
  setupPLC();           // Setup inputs and outputs
  servo1.attach(Y0);    // Attaches Servo 1 to Output 0
}

void loop() {
  inAnalog(X0);         // Read potentiometer connected to input 0
  outServo(servo1);     // Output to Servo 1 (connected to Output 0)
}

// The outServo function is defined locally
int outServo(Servo &ServoObj) {
  ServoObj.write(map(scanValue, 0, 1023, 0, 179));
  return(scanValue);
}
Source location: File > Examples > plcLib > InputOutput > ServoSingle
See the InputOutput section of the example files for a dual servo control sketch.

Related Topics

top