PLC software programming and hardware tips. (Part III)
"Design for reliability and program check for the worst"
Programmable logic controllers use "relay ladder logic" (RLL) as a preferred PLC programming control language. The Arduino "C" functions, made for programming the Arduino and compatible boards, is extremely easy to use. The Home Automation "Gator" PLC uses the same "C" functions.
The PLC scan sequence includes "input scan", "user program scan" and "output scan", which works in the microcontroller's main Loop(). Reading the inputs (analog or digital), CPU processing the inputs by "if, then, else statements" and other comparison logic and then outputing the results to PLC outputs is all that takes place during each complete "PLC Scan" pass in the main loop(). All PLC scan sequence routines are polled and interrupts are not allowed, which could halt the CPU.
We tried to closely mimic a modern PLC by timing each task in the control sketch software. If a input or control action takes too long, in execution, the PLC will halt and an error is generated. The extremely useful Arduino function millis() command which is used throughout our control software to detect any delays in the overall PLC scan sequence performance.
On reset, reboot or power on reset, the system startup routines <setup()> thoroughly checks for any device or peripheral failure. The system watchdog will not be reset and no control output action will take place until all system devices and peripherals are 100% operational. Simple diagnostic checks like I/O board logic power (checked by polling a spare input photo isolator), I2C addressing checking and a 2 second RTC update checking is all that is needed to verify that all devices are functional and 100% working. Even in the main PLC scan sequence, we run a system diagnostic check on every scan.
I2C bus is the weakest link, on this PLC and on any Arduino. Our design spec. requires a I2C real time clock and we had to make sure the I2C bus was reliable. When a I2C device like a RTC loses its 5 Volt power source, has a loose wire, or fails, or has a low battery or just hangs, the microcontroller I2C master hangs or waits for a response (forever) from the slave I2C device. We checked for this I2C device failure by installing an external WatchDog to prevent any control output(s) from functioning until the I2C failure is fixed.
Note: The I2C/TWI wire library should be rewritten to include a instruction "skip" on any I2C bus timeout failure - if possible ????
Most PLCs have LED lights to easily indicate visually what control I/O actions are being executed. LED lights for the inputs and outputs on the digital I/O board makes troubleshooting a snap. Software generated (Pin 13) red LED for uploading, PLC running, and PLC scanning also helps. The "relay power LED" and the "logic power LED" along with the +5 VDC power supply LED also help in troubleshooting PLC problems. The Watchdog has a red flashing LED to indicate watchdog "time out" and also has a yellow LED for watchdog reset (software controlled LED). By observing all LED status indications, this PLC can be easily troubleshot without meters and diagnostic tools.
During power on reset, the BlinkM cycles thru the rainbow of colors and also flashes green on the I2C bus check completion. The BlinkM is used as the PLC system annunciator. Green is system running and Red is system stopped or failure. You probably need sun glasses on to read this LED PLC system status annunciator - this RGB LED is bright!
Software delays should not be used except during the setup or startup bootup. The PLC scan time should not be slowed down by unnecessary software delays. In our applications, we need every bit of real time for both the PLC scan and the IOBridge scan loops, so the use of any software delays are prohibited. Also, by having extra 4 MHz on the Gator/Gator+ (@ 20 MHZ) provides for a faster PLC scan sequencing.
Most serial LCDs are slow but having one helps in system diagnostics. The LCD we are using takes over 100 ms. to display our LCD data, which slows down the PLC scan. We only display diagnostic data on the LCD when there is a error, failure or fault but never use it as a "running data" information display due to its slow speed.
Note: The Arduino has a receive buffer but does not have a transmit buffer, which slows down all transmited bytes to the LCD! <--- The Arduino design team, one day, should add this variable Tx buffer!
Ambient temperature and light changes affects the readability of all LCD displays. Since the LCD display, we are using, has software controlled brightness and contrast control, then we used a LM34/35 temperature and ambient light sensor (TEMT6000) to adjust the LCD brightness and contrast to different ambient conditions.
There are four ways we show PLC faults, errors and failures to the end user. Each way is dependent on where, how and when a failure has occured in the PLC.
The first way is by the 1st USB serial comm. port via laptop serial terminal program.
The second way, if the I2C bus passes diagnostics, the numerous software colors generated by the RGB BlinkM I2C LED system annunciator. Green = System OK & Red = System Failure
The third way is by the 4x20 LCD serial terminal information display.
The fourth way is to observe on board LEDs on all of the electronic sub modules.
The PLC RTC, (Macetech.com) "ChronoDot" DS3231 is an extremely accurate real time clock. In fact, Maxim-IC claims its DS3231 to be "the industry's most accurate real time clock?"
On daylight savings time changeovers in March and November of each year (In the USA), we decided to preprogrammed in all of the projected DST dates for ten years, which would keep us from resetting the RTC time every year (twice). The lithium battery would run low (8 years) before we would change the time again.
Note: This precision "ChronoDot" RTC is good for +- 1 minute per year!
Power distribution on the PLC requires "clean" and "dirty" power supplies. The internal +5 VDC switching power supply provides "clean power" to the A/D devices, I2C bus devices, the LCD and the "logic power" for the Parallax I/O board. This +5 VDC should NOT be used for any external device to the PLC. Doing so could induce noise and halt the Gator microcontroller. The "clean" +12 VDC supply input power that feeds the Watchdog, the I/O board relay power, the input of the +5 VDC power and the Gator supply voltage input should NOT also be used to externally power other devices. By obtaining very inexpensive switching +5 VDC and 12 VDC power supplies(Sparkfun) with LED indicators, should be done to power the external PLC devices and the photo isolator inputs. Do not power the photo inputs with the "clean" 12 VDC supply voltage!
Note: The external "dirty" power supplies are very noisey due to inductive switching of solenoids, relays and other loads that are connected to the PLC.
Note: The Parallax Digital I/O Board darlington driver (UN2803) has internal noise supression diodes to reduce the inductive switching noise generated by the on board mechanical relays.
Note: The Gator/Gator+ also has extra built-in ESD, overvoltage and overcurrent I/O pin protection, which greatly increases the runtime reliability of the Home Automation Gator PLC.
Note: Notice the huge capacitors on the Parallax Digital I/O Board (1000 uf), the Watchdog board (1000 uf) and the I2C bus board (330 uf) ... they are installed to filter out any noise spikes generated by the PLC sub modules.
Wiring a PLC requires special thought. Keep the AC loads as far as possible away from the DC loads. On the PLC panel (picture above) the farthest right hand corner of the Digital I/O board should be wired for only AC loads.
Summary: Any microcontroller can be used as a "programmable logic controller" to control external devices, but not having the proper designed photo isolation on the digital inputs, isolation on the digital outputs, watchdog safety protection, clean and dirty power distribution, noise suppression, board filter protection, control software that checks for task timing and also does system diagnostics then having a microcontroller to control external devices would be a waste of money due to system unreliabilty and poor runtime performance. Better have several spare Arduinos laying around for replacement!