a computer-simulated PLC
Published in issue 300, June 2001
Elektor project page.
PLCs are micro controller circuits boards that are frequently
used in a wide variety of industrial control and automation
The PLC described here is implemented in software. A complete PLC is simulated in a standard Windows PC. Using an external connection box, the PC can also sense and control many kinds of real items.
The abbreviation 'PLC ' stands for 'Porgrammable Logic Controller'. These devices are also sometimes referred to as 'freely programmable controllers' in the trade. This term refers to an electronic circuit that can be used to automate machinery and processes. A PLC thus can replace a conventional relay logic circuit.
A PLC always consist of a CPU module and several digital input and output modules. The number of input and output modules depends on the size of the process to be controlled.
One the major adventages of a PLC is that it makes it very easy to extend or modify the process. Consequently, PLCs have for years been an indispensable element in the automation of industrial systems.
Recently, PLCs have also like to have certain things automated in their house. Automatic garden sprinkler systems, awning controllers security systems and many other types of systems can easily be controlled using a PLC. Extensions or modifications can be made relatively easily after the system has been installed.
Figure 1 shows an example of a simple motor circuit, one case built using push buttons and relays and in the other case using a PLC. If you want to modify the operation of the circuit, you will have to modify the hardware if you use the relay circuit, but with the PLC all you have to do is make a few changes to the program.
The objective of the DCIPLC design is to implement a PLC in a relatively simple an inexpensive manner using a minimum of hardware, a serial interface and PC software. This saves trouble and expense of building a real PLC. In addition programming this controller is very intuitive and user-friendly.
DCIPLC is actually a program that replaces the CPU module of a
PLC, and within which control programs can be developed and
modified.With the aid of a serial interface cable and some
hardware, the states of 32 inputs can be monitored and 32 outputs
can be driven. The program runs under the Windows.
The minimum system requirement is a 100-MHz Pentium processor. The installed software takes up approximately 3 MB, including the examples.
The hardware of this design consist of nothing more than an assembly that converts PC serial port information into a number inputs and outputs. The actual input and output interface must be developed by the user. It can be connected to the DCIPLC interface circuit board using flat cables. It is not possible to design a universial interface that satisfies all possible needs.
If you look at the schematic diagram for the hardware (figure 2), you will see that it actually consist of little more than a handful of CD4094 and CD4021 shift registers. The 4094 shift register is an integrated circuit containing eight flipflops connected in series. With each clock signal, the logic level on the input ('0' or '1') is shifted by one position, so that after eight clock pulses and one strobe pulse we have an 8-bit output signal available. In our circuit, four of these shift registers are connected in series(IC5-IC8). This gives us a total of 32 outputs.
The 4021 shift register has the (relative to the 4094) that can also be used 'the other way around'. It can thus be used to read in the levels on its eight inputs. The output levels of the eight internal plipflops appear on after the other on the serial output when clock pulses are applied to the clock input of the IC.
I have chosen to use the PC serial port for the connection to the because it offers several advantages, namely:
- The serial port is relatively robust (short-circuit protection).
- The cable may be connected while the PC is operating.
- No driver needed.
In the interface hardware, the RS232 signal levels are limited by
Zener diodes D1-D3 to +4,7 V and -0,6 V. The resistors R1-R3
provide current limiting.
Immportant: Place the 9-way sub-D connector female on the bottom (connectors) side!
In addition to the shift registers, the circuit board includes a voltage regulator to provide a 5-V supply voltage. The input voltage may range from 10V to 20V. Thera are also 32 LEDs (D5-D36) with associated series resistors, whitch make the levels on the 32 ouputs visible. Finally, we can note that all 32inputs are fitted with pull-up resistors, so that a High level is present on any unused input.
Elektor electronics magazine have made a printed circuit board that has been designed for this hardware.
It is a double-sided board, in order to avoid the use of wire bridges.
Assembling the circuit board is certainly not difficult. Since the ICs draw only a small amount of current, the 7805 doas not need a heat sink. We have chosen to use rectangular LEDs, so that they can be placed close together in groups of eight located exactly opposite the associated header pins.
Standard 2x10 pin headers or box headers are used for connections to the outside world. A length of flath cable can easily be connected to each header using a press-on (IDC) connector. Each connector has +5 V and ground connection on the two extra pins, and a ground connection is present opposite each connection (see the schematic diagram). When assembling the circuit board, keep in mind that the connectors must be fitted on the solder side. This will make the numbering of the inputs and outputs match the numbering on the front-panel foil (see figure 4).
If you want to finish the assembly a tidy manner, you can mount the circuit board in a small enclosure fitted with a front panel foil, such as the one shown in figure 4.
The DCIPLC program is designed to allow you easily prepare a PLC
program by simply placing place blocks.
The length of the ladder logic diagram that is constructed in this manner is limited to 99 lines. Ladder Logic Diagrams, which are in many ways similar to relay logic diagrams, are frequently used to graphically generate programs. When putting together a ladder logic diagram, you should always bear in mind that the symbols in the diagram represent instructions rather than physical contacts.
When the instructions in the program are executed, the states of the inputs are monitored. Depending on the states, various ouputs are set either High or Low.
- Test for ON.
This instruction checks whether a voltage is present. It can be assigned to an input, output, flag, timer or counter. The status of this instruction is TRUE whenever a voltage is present (if the instruction is assigned to an input or output).
- Test for OFF.
This instruction checks whether a voltage is absent. It can be assigned to an input, output, flag, timer or counter. The status of this instruction is TRUE whenever no voltage is present (if the instruction is assigned to an input or output).
- Positive edge detection.
The status of this instruction becomes TRUE when a rising edge is detected. This remains valid for the duration of only one program cycle. This instruction can only be assigned to a flag.
- Negative edge detection.
The status of this instruction becomes TRUE when a falling edge is detected. This remains valid for the duration of only one program cycle. This instruction can only be assigned to a flag.
- This symbol stands for an internal or external output and can be assigned to a flag or an output. This output will become High when the result of the logical evaluation of the entire row is TRUE.
- SET internal or external output. This symbol can be assigned to a flag or an output. This output become High when the result of the logical evaluation of the entire row is TRUE. Even if the logical evaluation of the entire row later becomes FALSE, the output will remain High. Only RESET output can change the output state back Low.
- RESET internal or external output. This symbol can be assigned to a flag or an output. This output become LOW when the result of the logical evaluation of the entire row is TRUE. Even if the logical evaluation of the entire row later becomes FALSE, the output will remain LOW. Only RESET output can change the output state back High.
- The timer is an instruction that remains High or Low for a set interval (it can also be alternately High and Low). This instruction must always placed in the last column of the row. The timer starts when the logical evaluation of the entire row is TRUE, and continues to ran as long as the logical evaluation remains TRUE. If the logical evaluation of the entire becomes FALSE, the timer is reset. The variable t1 represents the interval in seconds during which the timer is LOW, while the variable t2 represents the interval in seconds during which the timer is HIGH. The variables t1 and t2 may have the values from 1 to 9999. When a timer is assigned, a value must be assigned to t1 or t2, since otherwise an error message will occur. A 'flasher' can be created by assigning values to both t1 and t2. To implement very long intervals, a timer can be combined with a counter.
- The counter instruction generates a High level after a defined number of pulses. Counting starts when the logical evaluation of the row is TRUE, and it continues as long as the logical evaluation remains TRUE. If the logical evaluation of the entire row becomes FALSE, the counter is reset. The counter value can be preset from 1 to 9999. A counter has two inputs one for counting up and the other for counting down. A flag, input, timer, output or counter can be assigned to each of these inputs. At least one of the inputs must receive an assignment. The state of the counter becomes High only when the count equals the preset value. The counter instruction must always be placed in the final column.
- Start or end of an OR instruction. This instruction can only be placed in coumns 1 trough 8, and it must always have a start and an end. Only one OR instruction can be used per row, and the maximum span of an OR instruction is 7 rows. In the dciplc\examples folder, there are two files (or.plc and bador.plc) that show what can be done (and what cannot be done) with a OR function.
- Line instruction. This instruction is the equivalent of a signal line in a schematic diagram. It always shows the level of the previous cell.
- Delete an instruction.
- Insert a row.
- Delete a row.
Note: A 'flag' is a sort of internal output that is used to store an intermediate result. A flag can be 'set' and 'reset' just like an output, so it is effectively an invisible output.
When generating a program, it is good idea to work from left to
right and to only start a new row after the current has been
completed. The second point is also necessary because the program
checks for empty rows.
You should also make a habit of always assigning a particular output only once when generating a PLC program.
Every PLC works in the following manner:
- When the PLC start up (changes to Run mode), all outputs are set to inactive (no voltage).
- The states of all inputs are read in.
- The user program processes the instructions sequentially using the signal states acquired in step2. If the state of an input signal changes while the instructions are being processed, this does not have any effect during the cycle currently being executed. However, the states of the timers, counters and flags are taken into account during the processing of the program.
- After the user program has been processed, the results are send to the outputs.
- Steps 2,3 and 4 are continuously repeated in a cyclic manner.
If the same output is used more than once in a program, this output will be assigned the state that it receives in the last row in which it is located. The DCIPLC program processes a complete user program 20 to 100 times per second. The actual rate depends on the speed of the PC and the length of the program.
The following example is a simple sprinkler controller for a
We assume that two locations in the garden must be watered. We want to be able to choose from three options, depending on how dry the ground is: a short sprinkling time, a long sprinkling time and a very long sprinkling time.
This what we need:
- Two sprinkler valves:
- QY1: Output Yes no.1
- QY2: Output Yes no.2
Three selection pushbuttons:
- IY0: Inut Yes no.0 for 10 minutes 'sprinkling
- IY1: Inut Yes no.1 for 20 minutes 'sprinkling
- IY2: Inut Yes no.2 for 30 minutes 'sprinkling
Now let's see how to enter the program.
First, in the DCIPLC program op a new file and save it as 'sprinkler.plc'. Select the button Test for ON in the toolbar(number 1 in figure 5) and click on cell 0 in row 1.
The chosen instruction will now be located in this cell, and a dialogue window will appear to allow you to assign the input and add a comment. In this dialogue, select 'input' and enter '0' as the assignment. This will cause input 0 of the PLC to be assigned to this instruction. If you wish, you can enter a comment, which can later by viewed by clicking on the cell with the right-hand mouse button. If you now click on 'OK' input will be assigned. This is shown by the fact that 'IY0' appears at the top of the cell.
Next, place a Test for OUT instruction (2) in each of the following cells, and assign these instructions to F11 and F12.
F11 and F12 are flags that will be set and reset later on the program. They are used to prevent another timer from being chosen after one of the pushbuttons has been pressed.
Now select the Line instruction (12) and click once in cells 3 trough 8. The lines have now been placed.
Select the Set output instruction (7) and click in cell 9. The output instruction will be placed and a dialogue window appear. Select 'Flag' and enter '10'. You can also add a comment if you wish. Now you can see that the first row is already finished. Generate the remaining rows on a similar manner, as shown in Figure 6.
Once you have finished, you can save the program, and it is then ready to tested.
To test the program, or to run it without the hardware, you can use the simulation mode. To do so, click on 'Simulation mode' in the 'Status' menu. A window will appear, in which you can enter the states of the outputs.
Set input 1 High and then Low. This starts timer 2, and outputs 1 and 2 will then remain high for 20 minutes.
The program gives a visual representation of the states of the inputs and outputs and also display the current values of the timers.
Once sprinkling has started, setting one of the inputs High no longer has any effect. This is because rows 1,2 and 3 have been blocked (F11 and F12).
After the hardware has been connected, you can make the proper port settings using the 'Ports' menu.
You can choose from COM1 - COM4, and you can also set the speed. It is a good idea to initially set a low speed and then check in RUN mode whether the inputs generate the proper responses and the outputs are correctly driven. Click on 'Run mode' in the 'Status' menu, or click on the toolbar button with the green arrow.
You can return to the edit mode at any time by clicking on 'Edit mode' in the 'Status' menu or click on the button with the red circle. This causes all hardware outputs to be reset to zero.
The following tips will help you obtain the best results from the program in the run mode:
- Make sure that as few programs as possible are active.
- Disable screen savers.
- Do not start any other programs.
- Dot not install a diskette or CD-ROM in the PC while the program is running.
These measures allow Windows to reserve the maximum possible amount of time for the DCIPLC program, which has beneficial effect on its performance. If a CD-ROM is inserted in the tray of the CD player, for example, the DCIPLC program will stop responding for a few seconds, since Windows gives priority to the CD-ROM drive. During this interval, the timers are no longer updated and inputs are not checked. If youobserve the above recommendations, the program will check the inputs at least ten times per second and the timer error wil not exceed two seconds in 24 hours.
On a 400-MHz Pentium machine, the sprinkler program achieves a speed of 90 cycles per second.
For coupling the controller to various types of devices, relays and sensors, readers of Elektor electronics magazine can draw on a vast store of circuits that have been published in earlier issues. Still, we would like to provide a pair of simple examples.
Figure 8a shows an 8-channel output interface consisting of a buffer IC and eight relays. This can be used to supply power to eight devices or circuits (with proper choice of relay, it is even possible to switch the mains voltage, but you must make sure a safe construction).
Figure 8b shows an 8-channel input interface. This consists of eight optocouplers with associated current limiting resistors. If the two terminals are connected together, a High level appears in the corresponding PLC input.