Twincat 3 – Back up pumps control

Hello everyone!

Today I’m going to explain a thing I had to do some time ago, it was slightly different but the overall functionality was kind of the same.

Imagine that we have a pump fed through a VFD controlled by a PID with its sensor and reference. For today’s example, we’re going to read that PID reference and if it’s higher than a set point for a period of time that we can configure, then we will turn on a pump fed through a soft starter.

Let’s see our overall system. We’re going to focus on the area highlited in green:


First of all, I’ve created a data type (STRUCT) named as PUMP, whose variables are the following:

MODE : INT; // 1 AUTOMATIC MODE (An algorithm will take charge of it), 2 MANUAL
FAILED_TO_START : BOOL; // As receiving a start command the pump will have to start within a period of time. If not, this alarm will be raised.
FAILED_TO_STOP : BOOL; // As receiving a stop command the pump will have to stop within a period of time. If not, this alarm will be raised.
CURRENT_RT : REAL; // Accumulated running time of the pump.
HEALTHY : BOOL; // 0 Unhealthy ; 1 Healthy

For the modelling of the pump there are several modes to control the pump. Locally, we have the LOCAL_SELECTOR which will be true if the pump is controlled from the electrical panel, the algorithm will copy that state so that if it turns to REMOTE it will keep the very same status. The running feedback will be on if locally the pump is turned on or remotely we turn it on.

The pump has two protections, a thermal relay and a bearing relay, it should have far more, I just put two for the sake of simplicity. If the pump fails to start or to stop, it will be unhealthy and will remain like that until someone restart the pump. This has been made because by doing so we make sure someone has seen the alarm and somehow has acknowledge it. It could be added a log to register such alarms, but that is something out of the aim of today’s project.

As the algorithm will pay attention to the running hours of the pump, in case of replacing one of the pumps you’ll have to modify the running time of the pump (To set it to 0 or another pump if you are using that one from another facility with an amount of running hours accumulated).

For the commands I’ve decided to set all of them to 0 once we’ve gone once through this block, because of experience I do really recommend doing this, because if some command by any reason is latched from local to remote operation may result in undesired consequences.

I’ve created the following function block to control the pumps with the following I/O variables:

pump fb

About the algorithm, it will determine, within the healthy pumps which to start (the one in auto mode with the least current time accumulated) if the PID Reference is higher than the set point to start a pump. Likewise the algorithm wil decide the pumps to stop if the PID Reference is lower than the set point to stop a pump (the one with the most current time accumulated) The I/O variables of the function block are the following:

Algorithm fb

Here is a demo of the program simulated on TwinCAT:




Twincat 3 – Transfer circuit breaker


Hello everyone!,

In my last post I modeled a circuit breaker. The aim of the modeling of that circuit breaker was to use it later on for a “bigger purpose” let’s say.

Today that day has come and I’ve programmed an algorithm to automatically control the circuit breakers.

First let’s define the behavior of the automatic control:

  • If our two feeders are energized then the algorithm will give preference to the circuit breaker chosen by Preference (1 for circuit breaker 1 and 2 for circuit breaker 2).
    • In the case of one of the circuit breakers being closed without being the preference and the two feeders being energized, the algorithm will open first the circuit breaker that is closed so that we avoid the coupling of the two supply sources.
  • This is very important and vital, a circuit breaker will not close unless the other has been opened successfully as you’ll be able to watch in the video, if there’s a fail to open alarm in any of the circuit breakers, the other circuit breaker will not close. The reason you may wonder? We may be feeding a short-circuit in the other source.
  • If there’s only a power supply available (We only have voltage in one of the sources) then the corresponding circuit breaker should be closed if the other is already opened.
  • In case of any power supply losing its voltage the circuit breaker will be tripped.

Without any further delay, let’s see how it works in practice:

And from here you’ll be able to download the program.

Twincat 3 – Modeling a circuit breaker.

Hello everyone!

As it has been long since last post of a program using Twincat 3 I wanted to program an algorithm to transfer the power supply from one feeder to another but the very first step is to model a circuit breaker and then program the algorithm, today I’m publishing the Circuit breaker and the testing carried out through the HMI, I still am unable to modify values from the HMI, I have to write the variables through the IDE itself.

By the way, just as a reminder, Timers should be used in this manner: INSTANCE_TMR(IN:=CONDITION_TO_INITIALIZE, PT:=TIME_DELAY, Q=>OUTPUT); because if you try to do this:



It won’t work 😉

The circuit breaker is in charge of interrupting the energy for the Loads 1 to N connected to the Busbar. To represent the circuit breaker, the filled squared means that the circuit breaker is closed, in case of the square being empty, the circuit breaker is open and the cross means that the status of the circuit breaker is unknown.

As an exercise I propose you to add each event such as fail to open, fail to close and any change of status of the circuit breaker to an array of strings with N registers using a FIFO structure click here for more information about this structure.

Here’s the video of the testing:


There are two external lockings which are not wired, this is because circuit breakers vary and so does criteria. Therefore, the lockings that affect to the operation of the CB is whether there has been a fail to change its status or the status is already the one that has been commanded to be.

From here you can download the project.



STL – Tia Portal 14 – Start On Delay Timers

Hello everyone!

Today I’m presenting you to an example of how to use Start on delay timers in STL.

These two pdf sheets with the instructions of the STL may be of use for you:

Sheet 1– Sorted by category.

Sheet 2 – Sorted alphabetically.

However I would strongly recommend using F1 on your IDE to find out more about the particular function you want to use.

For my particular case and following the thread of the last post I’ve decided to model a tank with its respective High alarm, high warning, low warning and low alarm.

So, in STL how do I initialize a timer comparing values?

Let’s start by writing the High level alarm

We have must compare values, to do so we’ll use the function Load, L.

L #CV // We load the current value read from an analog input to the Accumulator 1.

L #HAlarmSP //We load the Set point when we’ll see an alarm to the accumulator 1, deplacing #CV to accumulator 2

>=R // We compare whether The accumulator 2 (#CV) is greater than the accumulator 1( #HAlarmSP) if this is true then the program continues its execution downwards.
L #AlarmDelay // We load the time to delay the alarm.
SD “TimerHAlarm” // We put the loaded time in the timer initializing it in case the condition mentioned above is true. (This has been declared in a TagList within the TIA Portal IDE.
A “TimerHAlarm” // This is the output of the timer (TMR.Q for those who use . notation)
= #HAlarm // This will be the bool that will be true once the time has elapsed.

So far, so good? I hope so, if not I’ll let the example I’ve programmed here.

And as an exercise I encourage you to program the rest of the code to get a High warning , Low warning  and Low alarm.

Here is the video of what I’ve programmed:

Plant Modeling (I) – Tanks

Hello everyone!

Somehow I miss being around tanks, pumps, sterilizers, filling machines, VSDs and so on. Because of the price of PLCs being that expensive I’m toying with arduino to simulate somehow a little plant and today we’re going to start by the tanks by using OOP in arduino and exchange data with the application I’ve developed for that very purpose in Visual C#.
Through this interface we’ll be able to:

  • Monitor the level of the tanks.
  • Receive the alarms and warnings of the tanks.
  • Establish the setpoints for giving either the alarms or the warnings.

The level of the tanks is controlled by two knobs which are the variable resistors that are wired to the analog inputs of the arduino board. It is needed yet some filtering of the signal, but I still have things that I don’t see clearly. Experimentally, supposedly saved values in an array changed, which may be because I had to pass values with their reference and so on but I was incapable of getting the result I wanted even using references and pointers the values still changed, I have to look into a bit else about this particular matter.

The code source of both, the arduino and the interface can be downloaded in the following links:

Arduino and interface source code:

Reading and writing in Arduino’s EEPROM with Visual C#

Hello everyone!

Today I have ended the interface with Visual C# to read and write the EEPROM of the Arduino. It has been funny, as using the methods of the Serialport in Visual C# seemed as though it added some characters to the stream of characters I was sending. I thought that maybe those ones were obviously constant and I decided to set a beginning ” : ” and an end “/” as sending the stream of characters, taking it into account in the arduino program.

I realized about this as sending the read command which I wanted it to be a simple 20. It would have been far easier sending a string or whatever but well, I’m stubborn sometimes and I wanted it to work in that way, just for the shake of the challenge and after some changes here it goes:

Visual C# code source:

Arduino code source:

Persistent variables and Arduino

Hi everyone!

Have you ever thought of storing some paraters in your arduino board? Maybe you have not even wonder about this, but after tuning a PID for example or after having set some time parameters, storing data sometimes is of the essence, not just for adjusting parameters but to store alarm signals that could precent a pump to start because of having suffered from high temperatura, the data also could be retrieved to a log text for maintenance or tracking some faults.

This is part of a series of posts where my aim is to interface with arduino through Visual C#, for now, if we are thinking of storing variables, a way to do it is through the EEPROM of Arduino.

Since you stop providing with energy to your arduino board I’m sure you’ve already noticed that your variables get their initial values. So what to do then?

You should include this library in your project:

#include <EEPROM.h>

And now, by simply using:

EEPROM.write(addr,dR); // you can write in your EEPROM to the address addr the data dR; // you can read from your arduino EEPROM from the address addr

For more information about the methods .write() or .read()

In my example I’ve written only in an address because my purpose is to write down parameters to the EEPROM to particular addresses from Visual C#. However, this could be changed from Visual C# itself.


The program can be downloaded here.