NeuronicWorks is a product design, prototyping and manufacturing organization based in Toronto, Ontario catering to various industries across North America. We can help you implement a solution that best meets your needs using either a PLC or a microcontroller for the implementation, or even designing a custom PLC for you using a microcontroller custom solution. Contact us today to see how we can help.
PLC versus Microcontroller Programming
A microcontroller can be commonly considered the heart of an embedded design. Alternatively, however, a design could be based on a Programmable Logic Controller (PLC). The NeuronicWorks team has expertise with both approaches, but when would one choose a microcontroller rather than a PLC?
This article discusses how a PLC compares with a microcontroller in embedded applications. It first introduces what a PLC is and presents the concepts of Ladder Logic. The discussion should give the reader a sense of how PLC-based development relates to firmware code implementation that uses the C language on a microcontroller. The article then points out some limitations of PLCs which make a microcontroller the more suitable option in many circumstances.
What Are PLC and Ladder Logic?
Programmable logic controllers are designed to control industrial manufacturing processes, with the ability to withstand harsh environments, and can be easily programmed even by someone without much programming experience. The IEC 61131-3 standard specifies the languages used for programming PLCs, namely Instruction List, Structured Text, Ladder Logic, Sequential Function Chart and Function Block Diagram. Amongst these, Ladder Logic is the most widely used programming language.
Similarities between PLC and Microcontroller Code Execution
Figure 1: Microcontroller and PLC Code Execution
As depicted in Figure 1, Ladder Logic consists of “rungs”, which are similar to statements in C programming. Rungs are executed one at a time from top to bottom, in the same way as C language statements are. Furthermore, both Ladder Logic and C language firmware have the concept of an infinite loop, which enables rungs or statements to execute repeatedly. Ladder Logic additionally has a way to configure the number of times that a rung is to be executed within an infinite loop. “Init” rungs as shown in the above PLC program are configured to execute only once, causing them to operate in the same way as the statements inside the Initialization block which precedes the infinite loop in the above microcontroller program.
Next we will see how rungs are made up of instructions that act on signals Input to the PLC, and drive signals Output from the PLC. We will use switches and a light bulb to give simple examples of an application interfacing to those PLC Inputs and Outputs. And we will introduce symbols that denote common examples of those PLC instructions.
Understanding a Basic Switch, a Light Bulb and Digital I/O
Let’s first consider connecting a switch to an Input of a PLC. There are two different types of switches or pushbuttons -- Normally Open (NO) and Normally Closed (NC). Here “Normally” refers to the “at rest” condition of the switch. In other words, a force must be applied in order to transition the pushbutton to the opposite condition. Figure 2 depicts the NO and NC pushbuttons.
Figure 2: NO and NC pushbuttons
If we were to connect such pushbuttons to a microcontroller, we would access their state through I/O control registers in which a single I/O is represented by a binary bit. Likewise in a Digital I/O module of a PLC, every digital I/O is represented by a binary bit. I/O modules are components which can be installed as needed according to an application’s requirements, to serve different purposes – Digital Input, Digital Output, Analog Input, Analog Output, Communication Bus, etc. In Digital Input and Output modules, Digital I/Os are addressed by unique names, and can also have nicknames as we will see later.
If an Input in a Digital Input module is connected to a power source with the NO pushbutton, as shown in Figure 3 below with the Input named X0, it’s at rest bit status is 0 to represent Open, and when the button is pressed, it changes to 1 to represent Closed, and vice versa for the NC pushbutton.
Now let’s consider connecting a light bulb to an Output of a PLC. In the same way as with a Digital Input, a Digital Output is represented by a binary bit. If an Output in a Digital Output module is connected to a light bulb as shown in Figure 3 with the Output named Y0, the light bulb is off when the Digital Output’s bit status is 0, and turns on when its bit status changes to 1.
When programming a PLC, it is necessary to know what types of devices are connected to Digital Inputs and Outputs and how their behaviour relates to the status of the internal bits representing those Inputs and Outputs.
Figure 3: PLC Connected to Pushbutton and Light Bulb
As you examine the above diagram, you may wonder why the components are wired using the arrangement shown. Since we said that pressing the NO pushbutton causes Input X0’s bit status to be 1, does this mean that applying 24V to a Digital Input necessarily sets its bit to 1? And since we said that setting Output bit Y0’s status to 1 causes the light bulb to turn on, does this necessarily mean that setting a Digital Output bit to 1 causes the Output to be driven with 24V?
The answer is that it depends on the types of the Digital Input and Output modules used. The most commonly available types are Sinking Digital Input, Sourcing Digital Input, Sinking Digital Output and Sourcing Digital Output. Figures 4 and 5 show how the wiring of our simple example would be different for each type of module. So in Figure 3, we are using a Sinking Digital Input module and a Sourcing Digital Output module, and have wired our pushbutton and light bulb in the necessary way for each module respectively. Sinking Input and Sourcing Output modules are the most commonly used in North America.
Figure 4: Sinking and Sourcing Digital Input Modules
Figure 5: Sinking and Sourcing Digital Output Modules
If we were to use the Sourcing Input module instead – wired in the necessary way as shown in Figure 4 – then pressing the pushbutton would apply 0V to the Input instead of 24V. If we were to use the Sinking Output module, then driving the Output with 0V instead of 24V would turn on the light bulb. However, it is important to point out that in spite of the voltages being opposite, the Input and Output bit statuses would nevertheless be 1 in these cases! In other words, pressing the NO pushbutton would cause the Input bit status to become 1 and setting the Output bit status to 1 would cause the light bulb to turn on in all cases regardless of module type.
An easy way to remember this is by realizing that in Digital I/O modules, a bit status of 1 is always associated with a completed circuit. Or put differently, a bit status of 1 is associated with current flowing and not with a voltage being applied.
In the case of Figure 4 with the Sinking Input module, pressing the NO pushbutton completes the circuit, and current flows from the positive side of the power source, first through the pushbutton, then through the module causing its Input bit status to be 1, and back to the negative side of the power source. Similarly with the Sourcing Input module, pressing the NO pushbutton completes the circuit, and current flows from the positive side of the power source, first through the module causing its Input bit status to be 1, then through the pushbutton, and back to the negative side of the power source. Note that in both cases, if the NO pushbutton is not pressed, there is no completed circuit, and therefore the module Input’s bit status is 0.
In the case of Figure 5 with the Sinking Output module, we have a completed circuit with current flowing from the positive side of the power source, first through the illuminated light bulb, then through the module because its Output bit status is 1, and back to the negative side of the power source. And finally with the Sourcing Output module, we similarly have a completed circuit with current flowing from the positive side of the power source, first through the module because its Output bit status is 1, then through the illuminated light bulb, and back to the negative side of the power source. Note that in both cases, if bit status is 0 for the module’s Output, then it behaves as an open switch, so there is no completed circuit and hence no current flow to illuminate the bulb.
It is beyond the scope of this article to go into more detail, but the foregoing should provide a basic understanding of how devices can be connected to the Digital Inputs and Outputs of a PLC, and how the status of the bits representing those Inputs can change depending on the behaviour of the input devices, and how the status of the bits representing those Outputs affects the behaviour of the output devices. Now let’s discuss how a PLC program using Ladder Logic can be used to process the status of bits representing Inputs, and in turn decide how to control the status of bits representing Outputs.
Programming using Ladder Logic
A rung in Ladder Logic is simply denoted by a horizontal bar, with symbols denoting Input instructions placed on the left-hand side, and symbols denoting Output instructions placed on the right-hand side:
Figure 6: Ladder Logic Rung
Here are some of the basic symbols used in PLC programming, two of which (Examine ON and Output Coil) have been placed on the rung shown in Figure 6, and associated with Input X0 and Output Y0 respectively.
Figure 7: Symbols in PLC Programming
Fundamentally how a rung in Ladder Logic works is that the Input instructions (or conditions) on the left-hand side evaluating to True or False controls how the Output instructions on the right-hand side get executed. In the case of the example in Figure 6, this means that if the Examine ON instruction evaluates to True (i.e. if the X0 bit is 1), then the Output Coil instruction sets Output bit Y0 to 1, and otherwise to 0.
To clarify this further, let us apply this rung to the PLC of the practical circuit example already presented. The circuit diagram (Figure 3) is repeated here for convenience:
Figure 3: PLC Connected to Pushbutton and Light Bulb
Recall that there is an Input module and an Output module, and each Input and Output has a unique name (e.g. X0, Y0) similar to GPIO pins in a microcontroller (e.g. PA0, PB1). A NO pushbutton is connected to X0, and a light bulb to Y0.
In this context, then, the operation of the previous rung example takes on a new practical meaning – since the NO pushbutton’s bit status is 1 when the button is pressed and the light bulb illuminates when its bit status is 1, as the previous section explained, therefore pressing the button causes the light bulb to turn ON.
The example rung diagram is repeated in Figure 8, this time with nicknames SW1 and Light_Bulb assigned to Input X0 and Output Y0 respectively. In turn, the above description of the rung’s behaviour can be restated as follows: If the Examine ON instruction evaluates to True (i.e. if the SW1 bit is 1 – in other words, if SW1 is pressed), then the Output Coil instruction sets Output bit Light_Bulb to 1, and otherwise to 0 – in other words, turns Light_Bulb ON, and otherwise OFF.
Figure 8: Ladder Logic Rung with Nicknames Assigned
Note also, in fact, that due to the conditional nature of its operation, a single rung in a PLC program is analogous to a set of if-else statements in the C language, and the above rung functions in a manner similar to the following C code:
Figure 9: C Code Analogy for Rung in Figure 8
Different Variations on the Switch and Light Bulb Program
Now using similar reasoning, the rung logic can be changed to suit the following connections and requirements.
Figure 10: PLC Connected to NC Pushbutton and Light Bulb
Requirement:Turn light bulb ON only when pushbutton is pressed.
Figure 11: Ladder Logic Rung with Examine OFF Instruction
Here the Input condition Examine OFF is used because when the NC pushbutton is pressed, the status of the Input bit changes to 0, and Examine OFF becomes True when bit status changes to 0.
Figure 12: PLC Connected to NO Pushbutton and Light Bulb
Requirement:Turn light bulb OFF only when pushbutton is pressed
Figure 13: Ladder Logic Rung with Examine OFF Instruction
Here the Input condition Examine OFF is used because when the NO pushbutton is pressed, the status of the Input bit changes to 1, and Examine OFF becomes False when bit status changes to 1.
The following list summarizes some advanced concepts which are beyond the scope of this article to discuss in detail:
- In a rung we can also implement AND and OR operations to combine multiple conditions, and NOT operations to invert conditions.
- Latching is one of the most interesting concepts in PLC programming using Ladder Logic.
- Different types of timers and counters are available, just as they are in microcontrollers.
- We can also implement the concepts of structures and functions, as we do in C language programming. For example, in the place of an Output Coil instruction, we can instead call the HTTPCMD function depicted in Figure 14, which performs an HTTP GET request to a web server. As we can see, there are several parameters that need to be entered for this function to execute properly.
- We can also create tasks, as we can with real-time operating systems using the C language.
Figure 14: Example of a Function in Ladder Logic
Please stay tuned for a future blog article which will elaborate on these topics.
Ease of Programming a PLC versus a Microcontroller
More effort is required to write a microcontroller program than a PLC program of similar functionality, as we will now demonstrate.
Figure 15: Programmable Components in Microcontroller and PLC
Figure 16 depicts the various components that a designer has access to program in a microcontroller as compared to a PLC. We can see that while the programmer deals only with a component called the Application Layer in the case of the PLC, for the microcontroller there are several other components to program in addition to the Application Layer. If we were to write the same program in C for a microcontroller and in Ladder Logic for a PLC, and factor in how Ladder Logic rungs can be easily represented in the C language as we showed earlier, it should be intuitive that the Application Layer would account for the same high level features (or “business logic”) in both cases, and that implementing it would involve a similar amount of effort in both cases. So the fact that there are other components to take care of for a microcontroller means that our C program must also handle lower level features – features we don’t need to program in Ladder Logic because they are already part of a PLC internally. Hence there is less effort to write a PLC program.
Note that not all microcontroller-based projects involve substantial additional effort to implement those components that reside below the Application Layer. Usually, templates and automatic code generation tools are available from the manufacturer to help minimize the complexity of implementing these program elements. The degree to which the programming effort can simplify, and approach the effort to program a PLC, will depend on the quality and user-friendliness of those templates and tools provided by the microcontroller manufacturer.
Making changes to an existing C program will be similar in complexity to making the corresponding changes to an existing Ladder Logic program with similar functionality when only the “business logic” (residing in the Application Layer) is being changed.
We should also point out that the behaviour of a microcontroller can be modified to a far greater extent than the behaviour of a PLC because the microcontroller programmer has access to customize those various system components other than the Application Layer.
Adding a Microcontroller to Update an Existing PLC System
Advancements in Artificial Intelligence (AI) and the Internet of Things (IoT) have led to Industry 5.0 which demands customized solutions that may not be available off the shelf. Many industries want to enable smart capabilities for their existing PLC-based systems. This typically involves sending data from those systems to the cloud, where machine learning algorithms can be then used to process it intelligently. At the same time, major changes to the existing systems for obtaining the required internet connectivity would be impractical, and this is where microcontrollers play an important role. To meet the communication needs of those smart capabilities, a module can practically be designed and introduced into the system to act as a bridge between the PLC and the network.
When to Choose a Microcontroller over a PLC
Often a completely custom embedded system is preferred over an off-the-shelf controller. Two notable situations are:
- A unique use case: Not all industrial processes use off-the-shelf controllers because some require unique features which are not readily supported by a PLC. In such cases a custom designed solution using a microcontroller is often the practical approach.
- A large quantity requirement: Because PLCs are very expensive, they are preferred for applications where they are deployed in lower volume. Even though the effort in microcontroller-based development has a higher cost than PLC-based development, the overall cost of the custom solution is often much lower for high volume deployments because the hardware price per unit can be as low as one tenth the price of a PLC.
This article has presented the key aspects of PLC programming using Ladder Logic, comparing some of them to related aspects of microcontroller programming using C, and hopefully has given you a sense of the effort involved with each.
Both approaches are viable in the industry, and based on your hardware and programming skill set, experience, volume requirement and even industry application, you can choose to go with either custom microcontroller or off-the-shelf PLC. There is no “one size fits all” solution.