Automation Using Nodered
Automation Using Nodered
com)
Node-Red redPlc
My goal was also to open a door to the automation world with redPlc for the Node-Red user.
Thinking in loops with Ladder Logic Diagrams is not easy at the beginning.
But over time you get the hang of it.
There is a lot of information and examples on the internet and on YouTube about Ladder Logic Diagram.
Wago PLC:
The PLC firmware reads all inputs and saves the data in memory.
The PLC firmware executes the user PLC program.
The PLC program reads the data, processes them and writes them into memory.
The PLC firmware writes the data from the memory to the outputs.
The PLC firmware performs a hardware check.
PLC programming languages
The PLC program is not written in C/C++ or Python, but has its own programming languages.
All these PLC programming languages are defined in IEC 61131-3.
LD I0
ORN I1
ST Q0
IF Start = 1 THEN
Motor := 1;
END_IF;
Connection to PLC:
Switches S0, S1, S2 are connected to the PLC inputs I0, I1, I2.
Lamp L1 is connected to PLC output Q0.
The logic when lamp L1 lights up depends on the PLC program.
Instuction List (IL) and Ladder-Logic-Diagram (LD)
You can write our electrical circuit above as Instuction List (IL):
LD I0
OR I1
AND I2
ST Q0
In LD, outputs are represented with this symbol and called Coil:
LD I0 LOAD IO in ACC0
OR I1 OR ACC0 with I1
AND( I2 LOAD I2 in ACC1 and remember AND operation
OR Q0 OR ACC1 with Q0
) AND ACC0 with ACC1
AND I3 AND ACC0 with (NOT I3)
ST Q0 STORE ACC0 in Q0
Normally Open (NO) Contact Node in Node-Red (right node with hidden label):
Normally Closed (NC) Contact Node in Node-Red (right node with hidden label):
Live and Neutral Rail Node in Node-Red (right node with hidden label):
Nodes that read or write data from the hardware are called module nodes in redPlc.
This module nodes also creates the I, Q, IA and QA variables.
I have changed the index numbers (pin numbers) according to my test circuit:
Select in module node rpi-gio-in GPIO16, GPIO20 and GPIO21 as pullup inputs.
Select in module node rpi-gio-out GPIO26 as output (R1 depends on used LED, ~220 Ohms).
The complete LD with sequential inject node and Raspberry PI module nodes:
Compact View of Nodes
The modules rpi-gio-in and rpi-gio-out are connected in series at first output of sequential inject node.
The LD nodes are connected to second output of sequential inject node.
If more rungs are required, the number of outputs in the sequential inject node can be increased.
This ensures the Sequence of Operations (SOO).
The LD nodes shows the signal flow with the status icons (grey for false, green for true, red for error).
The states of the variables are displayed in the status text or the cause of error.
The live and neutral rail nodes have no function and is for documentary purposes only.
But I recommend using these to indicate that it is an LD.
Displays icons without label, click in node Properties dialog Appearance icon
and deselect checkbox Label.
The next chapters show all redPlc nodes with their variants.
At the end are some redPlc examples.
If the ladder logic element has multiple inputs/outputs, the asterisk (*) indicates them in the node.
Memory Node
Creates memory variable.
Example:
This example stores the values of the dashboard elements to persistent storage.
How it works ?
1. The persistent nodes initialize the MA0 and M0 variables from file on start.
2. onstart node sends once True to all export nodes on start.
3. export nodes reads the variables and sends the data to the Dashbord elements.
4. The Dashbord elements initialize own data and view.
5. If the user changes the Dashbord element, data is send to import nodes.
6. The import nodes saves the data form Dashbord element to variables.
7. On update cycle the persistent nodes recognize a change and save the variable to file.
Contact Node
Normally Open (NO) Contact Node (also called Examine IF Closed XIC):
Input is passed through to output if variable is True. Else False is send to output.
Normally Closed (NC) Contact Node (also called Examine IF Open XIO):
Input is passed through to output if variable is False. Else False is send to output.
AND:
Logical AND inputs. Output payload is True if all inputs are True.
Needs min. 2 nodes wired on input for valid work.
XOR:
Logical XOR inputs. Output payload is True if inputs are different.
Needs min. 2 nodes wired on input for valid work.
P_TRIGGER, N_TRIGGER:
Logical OR inputs and sense for positive or negative transition.
Flip-Flop Node
This node is used for store digital states.
Each FlipFlop has its order of operation sequence priority.
1. Set: If input payload is True, output payload and flag Q is set to True.
2. Reset: If flag R is True, output payload and flag Q is set to False.
1. Reset: If input payload is True, output payload and flag Q is set to False.
2. Set: If flag S is True, output payload and flag Q is set to True.
L FlipFlop (latch):
T FlipFlop (toggle):
CTU Up-Counter:
Output payload and flag QU is set True if counter CV >= preset PV.
CTD Down-Counter:
CTUD Up-Down-Counter:
Output-1 payload and flag QU is set True if counter CV >= preset PV.
Output-2 payload and flag QD is set True if counter CV <= 0.
Timer Coil Node
Timers delays input signals or generate pulses (PT = Preset Time, ET = Elapsed Time).
Changes to PT efects on start/restart of timer.
If preset PT is changed to <= 0 then preset PT is set to entered default preset.
TON On Delay:
TP One Pulse:
If timer is started, input is ignored until preset time PT is reached.
Example 1:
Example 2:
Arithmetic Operations:
add-addition, sub-subrtact, mul-multiple, div-divide, mod-remainder, inc-increment, dec-decrement.
Trigonomic Operations:
pow-power, acos-arccosine, acosh-hyperbolic arccosine, asin-arcsine, asinh-hyperbolic arcsine,
atan-hyperbolic arctangent, cbrt-cube root, cos-cosine, cosh-hyperbolic cosine,
exp-exponent to euler's constant, log-natural logarithm, log10-base-10 logarithm, log2-base-2 logarithm,
sin-sine, sinh-hyperbolic sine, sqrt-square, tan-tangent, tanh-hyperbolic tangent,
torad-convert to radians, todeg-convert to degrees,
Statistical Operations:
abs-absolute, min-minimum, max-maximum,
round-rounded to the nearest integer, ceil-smallest integer greater than or equal,
floor-largest integer less than or equal, trunc-removing any fractional digits,
random1-random number one range (0..n), random2-random number two range (m..n).
Mathematical Constants:
PI - This is the ratio of the circumference of a circle to its diameter.
E - This is Euler's number, the base of natural logarithms.
LN2 - The natural logarithm of 2.
LN10 - The natural logarithm of 10.
LOG2E - The base-2 logarithm of e.
LOG10E - The base-10 logarithm of e.
SQRT2 - The square root of 2.
SQRT1_2 - The square root of 0.5, or, equivalently, one divided by the square root of 2.
Scale Analog Node
Scales analog values by given input/output limits and stores in variable.
Example:
This example reads voltage and current from the analog to digital converter,
scales the values to engineering values.
In Node-Red Dashboard, these are displayed with the export node in a gauge.
Compare Analog Contact Node
Compares analog variables and acts as a contact.
Input is passed through to output if compare is True. Else False is send to output.
Example:
This example switches on a consumer (e.g. lamp) with switches on selectable days of the week
and between hour 9 and 15.
The redPlc module sys-time reads the system time and saves it in the variable IA99.
The day of the week is stored in index 4 (0 = sunday, 1 = monday ..) and the hours in index 2.
The days of the week are selected with the switches on the Raspberry Pi input
and the days of the week are queried with the compare node.
After that, the start and end hours are queried with the compare nodes in series.
If all queries are fulfilled, the Raspberry Pi output is switched on.
Select in module node rpi-gio-in GPIO12 .. GPIO25 as pullup inputs.
Select in module node rpi-gio-out GPIO26 as output (R1 depends on used LED, ~220 Ohms).
Import/Export Node
Imports or exports data from other Node-Red nodes.
Example:
In this example voltage, current and power is imported via MQTT and saved in MA variable elements.
Rung 00 checks current if >= 2.1 and triggers timer T0. If limit more then 2 seconds exceeded, M0.0 is set.
Rung 01 checks voltage if >= 30.5 and triggers timer T1. If limit more then 3 seconds exceeded, M0.1 is set.
Rung 02 checks power if >= 15.5 and triggers timer T2. If limit more then 1 seconds exceeded, M0.2 is set.
The export nodes sends M0.0, M0.1, M0.2 to Dashboard slider for indicate alarms.
Array Structure of Variables
Digital Input (I): Array of Boolean, Index: 0..63, Access: R
Counter (C):
Timer (T):
Flip-Flop (FF):