How to View I/O Status on the Arduino Opta

This document introduces ladder logic code allowing the Arduino Opta to display the Input and Output (I/O) on the status LEDs as shown in Video 1. This technique will reduce the time required to troubleshoot failures typically associated with the Programmable Logic Controller (PLC). Tasks such as identifying a broken or misaligned sensor become trivial.

Download the code (5.8 MB)

This post assumes that the reader has some knowledge of Ladder Logic (LL). It also assumes that the reader has previously configured and programmed the Opta using a ladder logic program. For example, it assumes the reader can program the classic 3-wire motor starter code featuring start and stop pushbuttons as well as a latch. If you are new to industrial automation, you are encouraged to review this short glossary.

Glossary

Arduino Opta

Arduino Opta: The Opta is one of the newest members of the Arduino family. It is a Programmable Logic Controller (PLC) ideally designed to help students transition into industrial automation. It may be programmed from the traditional Arduino IDE or from a new PLC specific IDE featuring IEC 61131-3 languages of ladder logic, function block, structured text, instruction list, and sequential function chart.

Global Variables

PLC code is often broken into smaller POU (Program Organization units). Global variables are often used to communicate between these programs. This is similar to the concept of memory scope that you learned when programming the Arduino.

Hardened

This term implies that a PLC may operate in a harsh industrial environment. A PLC is expected to operate for many decades at elevated temperatures. There is also a measure of survivability from accidental electrical connection including shorted wires of incorrect voltages applied to input terminals.

Hungarian Notation

Hungarian Notation: This variable naming scheme encourages programmers to include the data type and scope along with a variable name. This style is open for debate. However, it is the author’s experience that beginner programmers benefit from the reminder. It can save considerable time in troubleshooting code, especially when advanced topics such as user-defined function blocks are encountered.

Input/ Output (I/O)

see screw terminal.

Ladder Logic

Ladder Logic is one of the IEC 61131-3 languages. It is a graphical language with symbols that share commonalities with physical relays.

Mapping

This technique is used to transfer the contents of one memory location to another. As a rule, programmers are not constrained by the amount of memory available in a PLC; program clarity is given priority over size. Mapping is a robust method that adds clarity and reduces troubleshooting time. For example, the programmer can make a single reference to a physical I/O. This prevents errors when the program is modified as opposed to tedious work required to locate and change all references to that I/O. This is particularly advantageous if a PLC contact is damaged as the technician can change a single line of code and move to a still functional contact.

Screw-terminal

The physical connections to a PLC. They are generally grouped as the input block and the output block. An associated term is “field devices.” These sensors and actuators are connected to the PLC’s screw terminals.

Self-Documenting Code

PLC have a lifecycle measured in decades. Generally, they are NOT upgraded every few years like a cell phone. In an ideal setting, the program will be written using clear names for the physical actions performed by the PLC. For example, a rung of ladder logic may be read as “start the drill motor 2 seconds after the clamp sensor is active.”

Self-Resetting

Pertaining to this article, the term refers to timers and counter. For example, a timer may be coded to produce a regular pulse. A counter may be programmed to count to a specific number and then reset itself. When combined, a clock like structure may be constructed; one that counts from 0 to 59 with a one second delay each iteration.

State Machine

The PLC is designed to control a physical machine or other industrial process. Often the machine’s operation depends on a series of physical steps as the work material enters the machine, is manipulated, and then ejected from the machine. The term State Machine refers to the PLC code that tracks and controls these steps. In general, the term may be used to describe any code structure that contains unique steps.

Related Forum Posts


Video 1: The status of the Opta’s inputs and outputs are displayed in the status LEDs, green for the first input nibble, red for the second input nibble and blue for the output nibble.

Introduction

Many of us were introduced to the Arduino in an education setting. We all learned how to breadboard circuits and program the microcontroller to perform a variety of clever, fun, and interesting functions. As we transition from the breadboard to an industrial setting, we need to consider a different frame of reference.

The Arduino Opta is a Programmable Logic Controller (PLC). This is a hardened industrial computer. Here the term hardened implies that the Opta is designed to survive in a harsh industrial environment. The sensitive inputs are conditioned to allow the microcontrollers delicate 3.3 V I/O pins to accept 24 VDC and to tolerate a reasonable amount of abuse. The base unit of the Opta features four relay-based outputs with the capability of directly driving a variety of medium power loads up to and including a motor starter with a 120 VAC coil.

It is a natural instinct to keep all of the familiar Arduino ideas and habits. However, that can get us into trouble. For example, suppose you design a small machine featuring an Opta. You successfully program the Opta and on that celebrated day, the machine is commissioned and begins productive work on the factory floor. Some would view this commissioning as the successful result of hard work.

The experienced engineer knows that this is just the beginning.

That machine is now an integral part of the factory infrastructure. It needs to work reliably. When, not if, it breaks, the technician must be able to quickly restore the machine’s operation. This is essential as a broken machine is very costly in terms of lost production and idle personnel.

Ideally, a technician can restore the equipment without using any test equipment and certainly without resorting to finding a computer to open and then interact with the IDE. In fact, it’s not uncommon for technicians to have zero access to the intellectual property that resides in the PLC. Given today’s difficult work environment, the technician may be new to the company with no experience with the PLC in question.

One useful method to assist technicians is to program the Opta to echo the I/O status on the Opta’s LEDs.

In this post we will explore a ladder logic solution that accomplishes this task. The status LEDs along the red, green, and blue indicator LEDs will allow the technician to quickly determine the status of every screw terminal Input and Output (I/O). For example, if a proximity sensor is broken or misaligned, the technician can perform the service with no test equipment or computer present. The result is fast troubleshooting thereby minimizing equipment down time.

Status LEDs

Figure 1 presents a picture of the Arduino Opta. Observe that it has 4 status LEDs. There is also a green/red LED above the RESET pinhole and a blue LED above the USER pushbutton. Together these LEDs will be used to inform the technician about the status of all I/O.


Figure 1: The Arduino Opta is a small PLC ideal for learners transitioning to the industrial environment.

For this discussion we will assume all inputs are digital. Together there are 12 I/O: 8 inputs digital inputs and 4 digital (relay) outputs. Given four status LEDs and 3 indicator LEDs we have enough resources to display the status of all I/O provided we do so in a round-robin manner.

Figure 2 presents one way of doing this. Observe that each nibble (set of 4 binary values) is color coded. When the green LED (above the reset pinhole) is activated the values of inputs I1 to I4 are echoed to the four status LEDs. After a two second delay the code will shift to the red LED and values in I5 to I8 will be display on the Status LEDs. Finally, after another 2 second delay, the blue LED will illuminate, and the values of the PLC’s outputs will be echoed to the status LEDs.

Figure 2: Color association of the Opta I/O to the status LEDs. For example, I5 to I8 are displayed on the status LEDs when the Opta’s red LED is illuminated.

Ladder logic

There are several different ways to program the Opta to perform this function. For this post, we focus on Ladder logic as this is the traditional language of PLCs and there is good reason to use this IEC 61131-3 language as described in the Tech Tip.

Tech Tip: There is some controversy surrounding Ladder Logic (LL). It’s been suggested that LL is an old technique that should be replaced with modern programming languages. Perhaps, but it is still in common use and will likely remain so for decades. Even if we abolished LL from all PLC programming effective today, we could expect existing machines to continue to use LL for the remainder of their natural lifecycle of 2 to 3 decades. On a related note, LL is a close analogy to relays and their physical connection to real world devices. All PLC programmers and technicians need to embody this information to be effective in their respective jobs.

An outline of the code is presented in Figure 3. This design is complicated by the fact that prgStatusDisplay is to be written once and then reused in multiple projects. This requires careful naming of variables which explains the apparent redundancy of mapping operations.

Figure 3: The program to display the status of the I/O resides outside of the self-document code.

Let’s start with the center of the diagram and work our way out. The core of the PLC programming is identified as self-documenting programs. Here, we find a program for input Mapping (prgInMap), the control program (prgCtrl), and the output mapping program (prgOutMap). Note that we are using Hungarian Notation.

For our purposes, we will define “self-documenting” as code that contains descriptive names associated with the physical world. For example, the physical control panel may contain a selector switch, a start pushbutton, and a stop pushbutton. Internally, these could be named gxEnable, gxStart, and gxStop respectively. Where the letter g indicates a global memory location, x indicates a Boolean type followed by a descriptive name. The ladder logic representation is shown in Figure 4 where we see that gxMotor is activated via the start / stop latch provided the is enable switch is activated.

Tech Tips: Programming with a consistent style is essential for any language even for something a “simple” as LL. Your workplace may already have established a style guide. If not, a good place to start is the Coding Guidelines document provided by PLCopen. This open document explains the rationale behind the use of prefaces such as prg and the use of Hungarian Notation such as gxPBStart.

Figure 4: Ladder Logic latch for a motor start circuit.
Figure 4: Ladder Logic latch for a motor start circuit

Tech Tip: Those acquainted with ladder logic, you may have noticed the normally closed contact associated with the gxStop. This may have raised concerns as the physical stop switch should feature normally closed contacts. Stated another way, things that stop a machine must contain normally closed contacts as a failsafe against a broken or loose wire. To prevent confusion positive logic thinking is used in prgCtrl. Since prgInMap flips the polarity of the stop switch, there is no need for the mental gymnastics of simultaneously thinking about the switch type and how it is installed in the circuit. Instead, we can make a simple statement, “if not stop.”

Mapping challenges

One significant challenge in PLC programming is the mapping of these self-documenting names to the physical “screw-terminal” I/O. In Figure 3 we see prgInMap and prgOutMap situated on either side of prgCtrl. We also see input and output mapping in the Arduino Opta code space with a specific reference to the system resources.

Normally prgInMap and prgOutMap would not be required. For example, we could associate a physical start switch connected to screw-terminal I1 with the gxStart in the Arduino Opta input mapping block (second to the left in figure 3).

The reason prgInMap and prgOutMap exist is to allow a clean never changing instantiation of prgISStatusDisplay. Our objective is to write the status display program once and never change it. This cannot be done if we perform the mapping operation inside the Arduino Opta resources sections.

The mapping is shown in Figure 5. Observe that all variables are global in scope and Boolean. While this may aggravate the purist programmer, it is a necessary and normal operation for ladder logic programming. Global variables are used to communicate across the program blocks shown in Figure 3. For example, in the middle of Figure 4 we see the Opta’s I_O_mapping-ProgInp section. Implicitly we can see that the eight screw-terminal inputs have been mapped to gxI1 to gxI8. Likewise, the outputs from I_O_mapping_RelayOut have been mapped to gxO1 to gxO4.


Figure 5: The mapping operation performed within the Opta resources section

Mapping summary

The IOStatusDisplay program as depicted in Figure 3 takes in the variables gxI1 to gxI8 as well as gxO1 to gx04. The program has seven outputs; four drive the status LEDs and three drive the green, red, and blue indicator LEDs as presented back in Figure 2. Once again, we want to code prgIOStatusDisplay once and never touch it again. This requires the strict mapping operation described in Figure 4.

From within the Opta resources shown in Figure 5, we will always use the given variable names from within I_O_mappings_ProgInp, I_O_mappings_ProgOut, and I_O_mapping_LEDOut. Consequently, our user defined prgOInMap and prgOutMap are used to map the fixed resource names to the desirable self_documenting names commonly associated with PLC programing. For example, the physical start switch connect to I1, becomes gxI1, when then presented to the prgCtrl as gxSWStart.

Display I/O status program

The actual ladder logic code for prgIOStatusDisplay is relatively simple. As seen in Figure 6 located at the end of this post. It consists of 6 lines of code. The first two lines serve as crude state machine. The self-resetting timer TON_1 provides a pulse once every two seconds. The self-resetting counter CTU_1 provides a count from 0 to 2. Note that is does provide a count of 3 for one program scan. However, this is insignificant for this application.

Line 0002 contains a series of comparators. These will activate the red, green, and blue indicator LEDs based on the state held in CTU_1.

Line 0003 clears the four status LEDs. Then, depending on which state the machine is in, the status LEDs are set in lines 0004 to 0006. For example, if I1 and I3 are true, status LEDs gxLED1 and gxLED3 are set when the gxLEDGreen has its turn.

Conclusion

The techniques described in this post will allows the technician to quickly troubleshoot the PLC. They can do so without resorting to test equipment or taking the time to connect a laptop to view the PLC code. The program itself is relatively simple but required a fair amount of discipline with respect to mapping of variables.

We look forward to hearing from you as you design, build, troubleshoot, and repair your industrial control systems. Your feedback is invaluable to us and our readers. Please share your comments and question about this material on this page or back on DigiKey’s primary TechForum page.

Download the code (5.8 MB)

Best Wishes,

APDahlen

About the author

Aaron Dahlen, LCDR USCG (Ret.), serves as an application engineer at DigiKey. He has a unique electronics and automation foundation built over a 27-year military career as a technician and engineer which was further enhanced by 12 years of teaching (interwoven). With an MSEE degree from Minnesota State University, Mankato, Dahlen has taught in an ABET accredited EE program, served as the program coordinator for an EET program, and taught component-level repair to military electronics technicians. Dahlen has returned to his Northern Minnesota home and thoroughly enjoys researching and writing articles such as this. LinkedIn | Aaron Dahlen - Application Engineer - DigiKey

Related Forum Posts

Return to the Industrial Control and Automation Index


Figure 6: Ladder logic implementation of prgIOStatusDisplay

1 Like