# Introductory PLC Programming
## Introduction
### What is a Programmable Logic Controller (PLC)?

A Programmable Logic Controller, or PLC, is more or less a small computer with a built-in operating system (OS). This OS is highly specialized and optimized to handle incoming events in real time, i.e., at the time of their occurrence.
The PLC has input lines, to which sensors are connected to notify of events (such as temperature above/below a certain level, liquid level reached, etc.), and output lines, to which actuators are connected to affect or signal reactions to the incoming events (such as start an engine, open/close a valve, and so on).
The system is user programmable. It uses a language called "Relay Ladder" or RLL (Relay Ladder Logic). The name of this language implies that the control logic of the earlier days, which was built from relays, is being simulated.
Some other languages used include:
* Sequential Function chart
* Functional block diagram
* Structured Text
* Instruction List
* Continuous function chart
A programmable logic controller, PLC, or programmable controller is a digital computer used for automation of typically industrial electromechanical processes, such as control of machinery on factory assembly lines, amusement rides, or light fixtures. PLCs are used in many machines, in many industries. PLCs are designed for multiple arrangements of digital and analog inputs and outputs, extended temperature ranges, immunity to electrical noise, and resistance to vibration and impact. Programs to control machine operation are typically stored in battery-backed-up or non-volatile memory. A PLC is an example of a "hard" real-time system since output results must be produced in response to input conditions within a limited time, otherwise unintended operation will result.
Before the PLC, control, sequencing, and safety interlock logic for manufacturing automobiles was mainly composed of relays, cam timers, drum sequencers, and dedicated closed-loop controllers. Since these could number in the hundreds or even thousands, the process for updating such facilities for the yearly model change-over was very time consuming and expensive, as electricians needed to individually rewire the relays to change their operational characteristics.
Digital computers, being general-purpose programmable devices, were soon applied to control industrial processes. Early computers required specialist programmers, and stringent operating environmental control for temperature, cleanliness, and power quality. Using a general-purpose computer for process control required protecting the computer from the plant floor conditions. An industrial control computer would have several attributes: it would tolerate the shop-floor environment, it would support discrete (bit-form) input and output in an easily extensible manner, it would not require years of training to use, and it would permit its operation to be monitored. The response time of any computer system must be fast enough to be useful for control; the required speed varying according to the nature of the process.[1] Since many industrial processes have timescales easily addressed by millisecond response times, modern (fast, small, reliable) electronics greatly facilitate building reliable controllers, especially because performance can be traded off for reliability.
In 1968 GM Hydra-Matic (the automatic transmission division of General Motors) issued a request for proposals for an electronic replacement for hard-wired relay systems based on a white paper written by engineer Edward R. Clark. The winning proposal came from Bedford Associates of Bedford, Massachusetts. The first PLC, designated the 084 because it was Bedford Associates' eighty-fourth project, was the result.[2] Bedford Associates started a new company dedicated to developing, manufacturing, selling, and servicing this new product: Modicon, which stood for MOdular DIgital CONtroller. One of the people who worked on that project was Dick Morley, who is considered to be the "father" of the PLC.[3] The Modicon brand was sold in 1977 to Gould Electronics, later acquired by German Company AEG, and then by French Schneider Electric, the current owner.
One of the very first 084 models built is now on display at Modicon's headquarters in North Andover, Massachusetts. It was presented to Modicon by GM, when the unit was retired after nearly twenty years of uninterrupted service. Modicon used the 84 moniker at the end of its product range until the 984 made its appearance.
The automotive industry is still one of the largest users of PLCs.
Early PLCs were designed to replace relay logic systems. These PLCs were programmed in "ladder logic", which strongly resembles a schematic diagram of relay logic. This program notation was chosen to reduce training demands for the existing technicians. Other early PLCs used a form of instruction list programming, based on a stack-based logic solver.
Modern PLCs can be programmed in a variety of ways, from the relay-derived ladder logic to programming languages such as specially adapted dialects of BASIC and C. Another method is state logic, a very high-level programming language designed to program PLCs based on state transition diagrams.
Many early PLCs did not have accompanying programming terminals that were capable of graphical representation of the logic, and so the logic was instead represented as a series of logic expressions in some version of Boolean format, similar to Boolean algebra. As programming terminals evolved, it became more common for ladder logic to be used, for the aforementioned reasons and because it was a familiar format used for electromechanical control panels. Newer formats such as state logic and Function Block (which is similar to the way logic is depicted when using digital integrated logic circuits) exist, but they are still not as popular as ladder logic. A primary reason for this is that PLCs solve the logic in a predictable and repeating sequence, and ladder logic allows the programmer (the person writing the logic) to see any issues with the timing of the logic sequence more easily than would be possible in other formats.

### PLC usage scenarios

A PLC is primarily used to control machinery. A program written for a PLC consists basically of instructions to turn on and off outputs based on input conditions and the internal program. In this respect, it is similar to how a standard computer application is used. Once a PLC program is activated, however, it will typically run continuously as a loop for an indefinite period. PLC-based systems are frequently used not only to control simple devices such as a garage door opener, but also for solving complex application scenarios, such as controlling a whole house, including switching lights on or off at certain times, monitoring custom built security system, and so on.
Most commonly, a PLC is found inside of a machine in an industrial environment. A PLC can run an automatic machine for years with little human intervention. They are designed to withstand most harsh environments.

### History of PLCs

Evolution of PLC started from the proposal by GM to develop a Standard Machine Controller to alleviate the problems related to the Electro-mechanical circuits.
When the first electronic machine controls were designed, they used relays to control the machine logic (i.e. press "Start" to start the machine and press "Stop" to stop the machine). A basic machine might need a wall covered in relays to control all of its functions. There are a few limitations to this type of control.
* Relays fail.
* The delay when the relay turns on/off.
* There is an entire wall of relays to design/wire/troubleshoot.
A PLC overcomes these limitations, it is a machine controlled operation.

### Recent developments

PLCs are becoming more and more intelligent. In recent years PLCs have been integrated into electrical communications such as Computer network(s) i.e., all the PLCs in an industrial environment have been plugged into a network which is usually hierarchically organized. The PLCs could be supervised by a control center or a supervisory system known as SCADA and often are connected to Human Machine Interfaces (HMI). There exist many proprietary types of networks based on serial communication and Ethernet communication.

## Basic Concepts
### How the PLC operates

The PLC is a purpose-built machine control computer designed to read digital and analog inputs from various sensors, execute a user defined logic program, and write the resulting digital and analog output values to various output elements like hydraulic and pneumatic actuators, indication lamps, solenoid coils, etc.

#### Scan cycle

Exact details vary between manufacturers, but most PLCs follow a 'scan-cycle' format. PLC scans programme top to bottom & left to right.
Overhead - Overhead includes testing I/O module integrity, verifying the user program logic hasn't changed, that the computer itself hasn't locked up (via a watchdog timer), and any necessary communications. Communications may include traffic over the PLC programmer port, remote I/O racks, and other external devices such as HMIs (Human Machine Interfaces).

##### Input scan

    A 'snapshot' of the digital and analog values present at the input cards is saved to an input memory table.

##### Logic execution

    The user program is scanned element by element, then rung by rung until the end of the program, and resulting values written to an output memory table.

##### Diagnosis and communication

    is used in many different disciplines with variations in the use of logics, analytics, and experience to determine "cause and effect". In systems engineering and computer science, it is typically used to determine the causes of symptoms, mitigations, and solutions. It communicates with the input module and sends a message to the output module for any incorrect data files variations.

##### Output scan

    Values from the resulting output memory table are written to the output modules.
Once the output scan is complete the process repeats itself until the PLC is powered down.
The time it takes to complete a scan cycle is, appropriately enough, the "scan cycle time", and ranges from hundreds of milliseconds (on older PLCs, and/or PLCs with very complex programs) to only a few milliseconds on newer PLCs, and/or PLCs executing short, simple code.

## Basic instructions

Be aware that specific nomenclature and operational details vary widely between PLC manufacturers, and often implementation details evolve from generation to generation.
Often the hardest part, especially for an inexperienced PLC programmer, is practicing the mental ju-jitsu necessary to keep the nomenclature straight from manufacturer to manufacturer.

### Positive Logic (most PLCs follow this convention)

    True = logic 1 = input energized.
    False = logic 0 = input NOT energized.

### Negative Logic

    True = logic 1 = input NOT energized
    False = logic 0 = input energized.

### Normally Open

    (XIC) - eXamine If Closed.
    This instruction is true (logic 1) when the hardware input (or internal relay equivalent) is energized.

### Normally Closed

    (XIO) - eXamine If Open.
    This instruction is true (logic 1) when the hardware input (or internal relay equivalent) is NOT energized.

### Output Enable

    (OTE) - OuTput Enable.
    This instruction mimics the action of a conventional relay coil.

### On Timer

    (TON) - Timer ON.
    Generally, ON timers begin timing when the input (enable) line goes true, and reset if the enable line goes false before setpoint has been reached. If enabled until setpoint is reached then the timer output goes true, and stays true until the input (enable) line goes false.

### Off Timer

    (TOF) - Timer OFF.
    Generally, OFF timers begin timing on a true-to-false transition, and continue timing as long as the preceding logic remains false. When the accumulated time equals setpoint the TOF output goes on, and stays on until the rung goes true.

### Retentive Timer

    (RTO) - Retentive Timer On.
    This type of timer does NOT reset the accumulated time when the input condition goes false.
Rather, it keeps the last accumulated time in memory, and (if/when the input goes true again) continues timing from that point. In the Allen-Bradley construction, this instruction goes true once setpoint (preset) time has been reached, and stays true until a RES (RESet) instruction is made true to clear it.

### Latching Relays

    (OTL) - OuTput Latch.
    (OTU) - OuTput Unlatch.
Generally, the unlatch operator takes precedence. That is, if the unlatch instruction is true then the relay output is false even though the latch instruction may also be true. In Allen-Bradley ladder logic, latch and unlatch relays are separate operators.
However, other ladder dialects opt for a single operator modeled after RS (Reset-Set) flip-flop IC chip logic.

### Jump to Subroutine

    (JSR) - Jump to SubRoutine
    For jumping from one rung to another the JSR (Jump to Subroutine) command is used.
* Programmable Logic

## External links

Wikipedia:
* Programmable logic controller
* Ladder logic
* IEC 61131-3 PLC programming language standards
* SCADA
Wikiversity:
* Automation template
by philip