All About PLC Analog Input and Output Signals and Programming
Do you know how to scale your analog input? Or wire your analog output?
Many people struggle with analog signals in PLC programming. The wiring and how to use an analog input or output in your PLC program can be tricky. Most often you will see digital example PLC programs and in my Structured Text Tutorial most of the examples are digital programs.
I’m writing this article because many of you wanted to learn how to use analog signals in PLC programming. So how do you connect that 4-20mA analog transmitter to your PLC and use it in your PLC program? How do you scale an analog signal in Function Block Diagram? This article will give you answers to this and much more.
In this article you will learn about:
But first, let me start out with some basics about analog signals and how they work in a PLC.
Analog Signals in the PLC
First of all, we are going to look at some numbers. If you’ve read my article about combinatorial logic you will know that a PLC works with boolean values. A PLC can only work with the values 0 and 1.
This is great for digital signals. They are either 0 or 1 and thereby relatively easy to work with. But what about analog signals? As Wikipedia writes, analog signals are continuous signals that can vary over time.
For example you can have a 0-10 volt analog signal. This signal can vary from 0 to 10 volts and have any voltage level in between. And since analog signals are continuous, this signal will always at any time represent a voltage level. If you look at the diagram below, you will see that the analog signal can have any value between 0 and 10 volts.
The question is now: how do a PLC deal with all these different values? Let’s say you have an analog signal at 5 volts going into the PLC. We cannot represent it with boolean values, because they can only have the values 0 and 1.
The answer is binary numbers!
Representing Analog Signals with Binary Numbers
As mentioned before a PLC can only work with the two values 0 and 1. But that does stop us from working with analog signals. Because what is really going on, is that the PLC works with binary numbers. This is due to the fact that a PLC or a microcontroller is really just advanced electrical circuits made out of transistors. Since a transistor can only be either on or off, these two states will then represent the values 0 and 1.
But this only gives us two states. Very useful for digital signals, but not for analog. To understand how analog inputs work in a PLC you have to understand binary numbers.
Binary numbers is the numbering system used by a PLC or any other computer. The system only has two numbers, compared to our 10-base numbering system where we have 10 numbers from 0 to 9. Binary is just another way of writing numbers. I would recommend that you take a look at this tutorial on binary numbers, if you don’t know how they work.
Bits and Bytes
A binary number with one digit is called a bit. One bit can hold either a 0 or a 1. As mentioned, this is how digital inputs work. But if you combine these bits and thereby create multiple digit numbers, things start to get interesting.
In many PLC’s an analog signal is represented by a word. A word in binary is 8 zeros’ in a row or two bytes (4 zero’s). Just like this:
If you remember a little bit about binary numbers you will know, that a binary number with 16 digits can represent values from 0 to 65.535. This is only half the truth for PLC’s though. Because the first bit is used for signing the number, giving it either a positive or negative value.
So with 1 bit for signing we have 15 bits left for representing the analog value. The binary number can therefore represent values from -32.768 to 32.767.
When the analog input signal enters the PLC it goes through an A/D converter or analog to digital converter. This is the component in the PLC analog input card that transforms the analog signal to digital signals. It is these digital signals that will eventually give our binary value representation in the PLC.
Before getting to know about the A/D converter it’s important to understand what type of analog signal you’re dealing with. In this article I will focus on these three types of analog signals in PLC programming:
The reason we need to know about the type of signal is because we need to know the range of the signal.
One very used type of analog signal is the 4-20mA.
We have to know our type of signal. Because in this example we now know that the analog signal has a range of 16mA. An analog value is often put in a 16-bit double word in a PLC. This is because the A/D converter converts the analog signal to a digital value of 16 bits long.
You can buy analog input cards with different resolutions. That depends on how many bits the A/D converter has to work with. More bits gives us more numbers to represent the analog signal.
Resolution of Analog Signals
The number of bits you have to save the analog value is in fact called the resolution. Think of it like your TV. It also only has a certain amount of pixels to represent a picture. The same goes with analog to digital conversion.
Just like we call it resolution when talking about the number of pixels on out television, we also call it resolution when talking about analog values represented with a number.
The resolution is very important when dealing with analog signals in PLC programming. When entering the analog input card the analog signal will be split into a value between 0 and 32.767. Dividing the analog value into 32.767 gives us a certain resolution.
Every time our value increases by 1, it means that the analog signal has increased with x mA.
We can’t quite yet calculate the mA. Because most PLC’s has something called over- and under range, when talking about analog signals. Let’s have a closer look at the ranges for analog signals in PLC programming.
Analog Signal Range
Now things start to become vendor specific. In this article I will take Siemens as example. But don’t worry. This principle applies to most PLC vendors.
Analog signals tends to be very sensitive. Although it is our intention that a signal range should be from 4-20mA, the signal can sometimes peak or drop. When that happens we want to be able to see that in the PLC. Although it is not always the case these peaks and drops can mean that there is something wrong. To be able to detect these in out PLC program we need what is called over- and under range.
Our normal range or rated range is 4-20mA. But on top and below that Siemens had added some extra mA in the range. They divide the over range in these two categories:
- Overshoot range (overrange)
And the under range in these two:
- Undershoot range (underrange)
What this means is that instead of a 4-20 mA range we now have a 1.185 – 22.96 mA range. The same applies to the other types of analog signals. E.g. the 0-10V range is 0 – 11.852 V range.
All in all this means that our analog signal ranges can be illustrated like this:
|PLC Value||Measuring Range|
|Dec||Hex||0-10 V||4-20 mA|
|32767||7FFF||11.852 V||22.96 mA||Overflow|
|32511||7EFF||11.759 V||22.81 mA||Overshoot Range|
|27648||6C00||10 V||20 mA||Rated Range|
|20736||5100||7.5 V||16 mA|
|1||0001||361.7 μV||4 mA + 578.7 nA|
|0||0000||0 V||4 mA|
|-1||FFFF||No Negative Values||Undershoot Range|
With the over- and underflow in mind, we can now start to calculate the real resolution of our analog signal. But instead of just using the 22.96-1.185 mA or the 11.852 V ranges there is one number you should notice here:
As you can see in the table above this is where our rated range ends. Our resolution for a 0-10 V signal should therefore be calculated like this:
10 V / 27658 = 361.7 μV
Or for our 4-20 mA signal:
16 mA / 27648 = 578.7 nA
These two numbers are the minimum values we can represent in out PLC with the digital value. In most cases these steps are small and precise enough. Keep in mind also that the higher resolution you want, the more expensive PLC analog input and output modules.
With a little bit of theory in mind let’s dig into it by looking at the analog inputs in a PLC.
Analog inputs can come from a variety of sensors and transmitters. You can measure a whole bunch of different things. The job of the sensor or transmitter is to transform that into an electrical signal. Here are a few of the things you can measure with analog sensors:
Of course there are many other things you can measure. The main point here is that we (the sensor or transmitter) will transform those physical values into an analog signal. It is that signal we can use in out PLC as an analog input.
An example here could be a temperature transmitter with a 4-20 mA output. Connected to the transmitter is a temperature sensor. The transmitter is then calibrated to a range of e.g. 0-100 degrees. What this means is that when the temperature is 0 degrees the output of the transmitter will be 4 mA, and 20 mA by 100 degrees.
A transmitter is often needed because the sensor in itself cannot provide us with an analog signal. Or at least not one that fits a PLC analog input. It is possible to buy analog input modules where you can connect a temperature sensor for example directly. But most often you will have a voltage or current input module where you connect a transmitter.
Calibration is very important when talking about transmitters. You have to know how much those mA or volts represents in the physical value.
Wiring of Analog Inputs
Before you start wiring any PLC analog module I highly recommend that you not only read the manual, but also know what type of signal you’re dealing with. Wiring a PLC analog input is a bit different depending on the type of signal.
In this tutorial I will cover wiring of the two most basic analog input signals:
The reasons I divide the analog input signals in these two categories are not only because they are the most used signals. It is also because the wiring of them are different. Since these two types of analog signals works in very different ways, you will also have to wire them differently on the analog input module. Mistakes in the wiring can potentially destroy the input module, so be careful!
Both types analog signals have one thing in common.
Resistance is what divides the voltage or limits the current. As a matter of fact, a resistor is even used to measure the current. But more on that later. Let’s start by looking at the first type of analog signal – voltage.
Voltage Analog Input Wiring
Using voltage for analog signals is quite common. They are also fairly easy to wire, since you often only need two wires. But that doesn’t mean you shouldn’t be careful when wiring this type of analog signal. If not wired correctly you can get a faulty analog signal or even worse, a broken analog input module.
So, basically all analog voltage inputs has two terminals:
- AGND: The ground or reference for analog inputs
- AIN: Analog input
Voltage is always measured between two points. You can’t take one point and then say: at this point I can measure 10 volt. For that you need a reference point. Just like a battery. A 9 volt battery is only 9 volt between the positive and negative side.
That’s what we use the AGND or analog ground for. It is between AGND and AIN that the PLC measures how many volt there are on the analog input. This also gives us the answer to what AIN is.
When you’re connecting the analog voltage source, the AIN is where you connect the positive (+) side of it. The negative side (-) should be connected to AGND. Those are the two main wires for the analog signal.
But if you only connect those two you will end up with a very vulnerable signal. Electromagnetic compatibility (EMC) can easily alter your analog signal.
As you can see below, this is solved by shielding the wires and connecting the shield to ground. Be aware though that this is NOT the same ground as the AGND!
The thing is that not all grounds are the same. Where the AGND is used as a reference for our analog signal (0 volt), the shield should be connected to an earth ground. Because noise is really just induction of current in wires because of magnetic fields. By using a shield the current will be induced in the shield instead of the wires of our analog signal. This current needs to be led away and that is why we connect the shield to earth ground.
Voltage Drop in Wires
Since we’re dealing with voltage here there is something about the wiring itself that can cause problems. All electrical wires (conductors) have resistance and hence creating a voltage drop. This could potentially mean that the voltage at the analog input is different than the voltage at the transmitter. This of course only has a significant consequence if you have long distances of wiring or too small wires.
Current Analog Input Wiring
Instead of voltage you can also use current as analog signal. They might be a little more tricky to wire, but in general they are more stable. Especially one of them has a very smart feature that makes it probably the most used type of analog signal.
The current in analog signals is usually measured in milliamperes (mA). A typical range here is between 0 and 20 mA. Less than that would be hard to measure and more than that would soon become dangerous. Let’s take a look at what it really means to use current in analog signals.
First of all, you need a closed loop for current to flow.
Even though the wiring of the first type of current transmitter is the same as with voltage, something different goes on here. Current is the flow of electrons from one pole to another. That is why you will always need a closed loop for measuring current.
In fact, the PLC can’t even measure current. So what happens is that inside the analog input module, a resistor is put between positive (AI) and negative (AGND). This will not only make up the closed loop but also converts our current signal to a voltage signal.
The resistor is called a shunt resistor and has a specific resistance. E.g. in some Siemens analog input modules it is 250 Ohm. And because of Ohm’s Law, the analog current signal can now be converted to a voltage signal by simple calculations.
Voltage (V) = Resistance (R) x Current (I)
Since we have a well known resistance of 250 Ohm, it is also possible to calculate how many milliamperes our measured voltage corresponds to.
With that background in mind, let’s take a look at the wiring of current analog inputs. They can generally be divided in three types:
When talking about 2, 3 and 4 wires what we’re talking about here is really the transmitters or transducers. Current transmitters can be wired in different ways and therefore with a different number of wires. The difference is really the power supply for the signal. Sometimes the transmitter will supply the circuit, and sometimes you have to use an external supply.
2-wire Analog Input
The first and most simple way to connect a 2-wire transmitter is connecting it to be loop-powered. Basically this means that the PLC will supply the current loop. Only 2 wires are needed here. In order for the PLC to supply the current we need to use another terminal on the analog input module:
- A+: Analog input supply
One wire that goes from A+ through the transmitter and back in another wire to AIN. The supply comes from A+, the transmitter controls the current flow and the analog current signal goes into AIN.
You can also use an external power supply for the 2-wire transmitter. Connect the 0 V from the supply to AGND and 24 V through the transmitter back to AIN. Although you would practically need 3 wires for this, it is still considered a 2-wire analog input connection. Because the transmitter only has two wires.
One disadvantage of the 2-wire current loops is that you only have one loop for both the supply and the signal. This actually means that the transmitter has to consume less than 4 mA for it to work. Some sensors and transmitters consume more than that, which brings us to the 3- and 4-wire loops.
3-wire Analog Input
In a 3 wire current loop only the ground is shared between the supply and the signal. The ground is connected to both AGND and on the power supply (-). But the transmitter has 2 positive (+) wires. One that goes to the power supply (+), and one that goes to AIN.
Although they share the ground a 3-wire transmitter creates 2 loops. One for the signal and one for the supply. Now the sensor or transmitter can consume as much current as it needs without interfering with out analog signal.
4-wire Analog Input
The 4-wire transmitter is also widely used since it separates the supply from the signal. By giving the transmitter 4 wires, you can have 2 for the supply and 2 for the signal. Naturally, the 4-wire transmitter requires an external power supply.
Clearly the biggest advantage of using a 4-wire transmitter is the complete separation between supply and signal. They can be fully isolated or opto-isolated so that disturbance in the supply shouldn’t affect the signal.
Read the Manuals
The most important thing here is that you know what transmitter or sensor you have. Read the manual and decide how you want to connect it. Don’t forget to also read the manual for the analog input module. Different vendors give different names to the terminals on the module. Sometimes they even put different guidelines for grounding and noise protection.
All in all. To avoid problems with wiring… Always read and follow the instructions given by the manufacturer.
Analog Input Scaling
When the analog input signal enters the PLC you will often have to scale it in your PLC program. What scaling means is that you convert the raw value from the analog input to some sort of engineering value. An engineering value is a number that represents the physical value e.g. flow (m3/s), weight (kg) or temperature (degrees).
Converting or scaling is done simply with some math. Some IDE’s like Siemens Step 7 or Tia Portal even has a function block specifically for doing the scaling. Here I will show you both ways of scaling an analog input.
Scaling with Math
Previously I explained how the analog signal becomes a raw value in the PLC. But this number really represent nothing but e.g. on the Siemens platform a number between 0 and 27648. What the analog signal represents is a physical measurement like 0 to 300 degrees. It is convenient to work with the engineering value in our PLC program or to show the measurement on an HMI.
Somehow we have to convert the range 0-27648 to 0-300.
Mathematically we can describe it with this relation:
Raw Value / 27648 = Engineering Value / 300
By isolating the engineering value we now get an equation we can directly use in our PLC program. This equation can be used to scale the analog input to the engineering value:
Engineering Value = (Raw Value / 27648) * 300
Of course the easiest option here is to use structured text. In that way we can scale the analog input with just one line of code. Here’s how that is done in CODESYS:
But even though you can do the scaling with math only, you are sometimes provided with function blocks to do the scaling.
Scaling with the SCALE Function Block
Siemens for example has build-in function blocks specifically for scaling. The block is simply called SCALE and has 5 inputs and 3 outputs. But I’ll only focus on 3 of the inputs and 1 of the outputs.
The HI_LIM and LOW_LIM are the limits for your engineering value. For example if you want to scale your analog signal to 0-300 degrees, the LOW_LIM should be 0 and HI_LIM 300. At the input called IN is where the raw value from the analog input goes.
Finally the result of the scaling will be sent to the output of the block (OUT).
All that is going on here is the same math as shown previously. Some people prefer one method over the other.
Analog outputs has a lot of similarities with analog inputs. But there are still some differences in both the way you wire them, and in the way you use them in your PLC program. Just like the analog input, the analog outputs can be divided in two types:
And the principles are the same. Either we use voltage or current as analog signal.
Let’s dig right into it by looking at the wiring of analog outputs.
Wiring of Analog Outputs
No matter which type of analog output you are using there’s one thing you always have to keep in mind. The load. Because essentially you will connect a load to the output. This could be a flow controller valve, a frequency converter or even an analog input on another PLC.
The device you connect to your analog output is what decides which type of analog signal you should be using. If you want to control a valve with a 4-20 mA signal, then your analog output should also be 4-20 mA.
Voltage Analog Output Wiring
Again, the voltage analog output is the easiest one to wire. For this you will only need 2 wires. A positive and a negative side of the voltage source. Which in this case is our analog voltage output. Here’s how a simple 2-wire analog voltage output is wired:
But sometimes 2 wires are just not enough. Even for voltage outputs. Some Siemens modules have two extra terminals at their analog outputs. These are used to compensate for something called line impedance or line resistance. Without getting into too much detail impedance is the opposition that a circuit presents to changes in current or voltage. Again, we’re dealing with voltage here, so any resistance will create a voltage drop.
If you have an analog voltage signal that changes the resistance is even bigger. Those two additional wires are there to compensate, so that the voltage level at the analog output is the same as the voltage level at the destination.
Talking about resistance there is also another thing you have to be aware of when dealing with analog voltage outputs: load impedance. To avoid short-circuiting the analog output, the load connected to the output has to have a minimum load impedance. This is typically between 500 and 1k Ohm. You should always consult the manual both for the analog output module and for the load you’re connecting to it.
Current Analog Output Wiring
Most analog outputs on PLC’s works very much like a 3-wire transmitter. This might seem odd, since we only had 2 wires so far. But the PLC often provides an internal power supply giving us the third wire. The supply for the signal.
This means that when you’re working with current outputs, you will most times only have to wire the signal current loop. Of course you will eventually also have to wire the supply loop. But that is often needed to supply the module itself so the PLC can see it.
One big difference between the current and voltage outputs is the load impedance. Because where voltage outputs would require a minimum load impedance, the current outputs actually has a maximum load impedance. Remember, we’re talking flow of current here and impedance (resistance) limits the current. If the load impedance is too big, the loop will simply not be able to drive the current flow needed.
A typical PLC analog current output has a maximum load impedance of 300-500 Ohm.
Analog Output Scaling / Unscaling
If you know how to scale an analog value, you also know how to unscale it. Because that is something you often have to do. By unscaling I mean transforming the engineering value to a value that the module can convert to an output.
Let’s say we have a 0-10 V analog output signal to control the speed of a motor. In order to run the motor at 50% speed we have to generate a 5 V analog signal at the output. To do that we need to know which value to send to the D/A converter. To put in other words, we need to know our range.
If we take Siemens PLC’s, as I used as example in the beginning of this article, they have a range of 0-27648.
Because we now know our range we can calculate the 50% of that: 13824.
The problem here is that the operator doesn’t enter 13824 on the HMI as setpoint. What he typically will set is the engineering value, in this case percentage (%). That’s why we now have an engineering value that we have to convert to a value withing the analog output range.
You can do unscaling in two different ways. With math or with function blocks. Using math is a good way to make it easy to understand what’s behind. Just use the same relation as with the scaling:
Raw Value / 27648 = Engineering Value / 100
By isolating Raw Value you now have a mathematical expression for converting an engineering value to a raw value. You can implement this in structured text with just one line of code:
RawValue := (EngineeringValue / 100) * 27648;
Unscaling can of course also be done with function blocks. Some vendors provide specific function blocks for unscaling. Take Siemens for example. They have a block called UNSCALE. It works in the opposite way of the SCALE block. By unscaling a value within a certain range to a value from 0-27648. The HI_LIM and LOW_LIM are now the limits of your engineering value.