PLC programming
Welcome! I am excited to help you dive into the world of industrial automation. Before we map out our learning journey and start looking at how these industrial computers control massive factories, I would love to learn a little more about what brings you here.
To tailor this guide to you, could you tell me a bit about your background? Are you a student, a hobbyist, or working in a technical field already? Also, do you have a specific goal in mind for learning PLC programming?
I already work in a technical field and need this for my job.
It's great to have you here! Since you are working in a technical field and need this for your job, we will focus on building practical, industry-relevant skills. Let's start by laying out our game plan. Here is the path we will take to get you programming PLCs:
What is a PLC?
Think of it like the brain of an automated factory machine: it feels what is happening in the physical environment through its "senses" (inputs) and reacts by moving its "muscles" (outputs). Where in your current workplace do you think you might find a PLC in action?
In our assembly line's conveyor belt system
A conveyor belt system is a perfect, classic example! You have sensors detecting package locations (inputs) and motors driving the physical belt (outputs) all coordinated by a central controller.
The Architecture of a PLC
A block diagram of PLC architecture showing Input Modules, CPU & Memory, and Output Modules, with Power Supply connections to all three.
- The CPU (Central Processing Unit): This is the microprocessor that executes your program's logic. It reads the incoming electrical signals, solves your mathematical or logical equations, and decides what actions need to be taken.
- The Memory: This is where the PLC stores its operating system, your custom control program, and temporary data (like timer values or package counts). It uses robust, non-volatile storage so that if the factory suddenly loses power, the conveyor program isn't wiped out.
-
The I/O Modules (Inputs/Outputs): This is the physical interface to the real world.
- Inputs convert high-voltage electrical signals (like a 24V DC signal from a limit switch) into safe, digital data the CPU can read.
- Outputs take low-voltage command signals from the CPU and boost them to actuate physical equipment (like sending 120V AC to start a heavy conveyor motor).
Continue to the PLC Scan Cycle
Let's dive into exactly how the CPU makes decisions in real time using the (Scan Cycle).
The four sequential phases of a Programmable Logic Controller (PLC) scan cycle arranged in a continuous clockwise loop.
- Input Scan: The PLC looks at the physical input terminals (like limit switches on your conveyor belt) and copies their On/Off status into its internal input image memory table.
- Program Execution: The CPU runs your custom program logic from top to bottom, line by line. It uses the copied states from the input table to calculate what should happen, storing the resulting decisions in an output memory table.
- Output Scan: The PLC takes those calculated values from its memory table and writes them directly to the physical output hardware terminals, turning on your conveyor motor or warning lights.
- Housekeeping: The PLC performs internal self-checks, safety diagnostics, and communicates with any connected programming laptops or screens to make sure everything is running smoothly before looping back to step 1.
Continue to how PLCs contrast with standard PCs
It's easy to look at a PLC and think, "Why not just use a regular computer?" After all, standard PCs are incredibly powerful, dirt cheap, and can run complex software.
PLC vs. Standard PC
| Feature | Standard PC | PLC (Industrial Controller) |
|---|---|---|
| Operating System | General purpose (Windows/macOS) with background updates | Dedicated, lightweight Real-Time Operating System (RTOS) |
| Environment | Clean, temperature-controlled offices | Hot, dusty, vibrating factory floors with high electrical noise |
| I/O Handling | Done via slow, standard ports (USB, ethernet) | Hardwired, high-speed modular physical terminals |
| Execution Style | Multitasking (switches between many running apps) | Sequential loop (runs one single control program infinitely) |
Continue to Ladder Logic Basics
Welcome to Ladder Logic
- The Rails: Think of the left vertical rail as the "hot" power rail ( or ) and the right vertical rail as the neutral or ground rail.
- The Rungs: These are the individual horizontal lines of code. Power flows from the left rail, through your conditions (like switches or sensors), and over to the right rail to power an output device (like a motor coil).
A single-rung ladder logic circuit with labeled vertical rails, an open input contact, and an output coil.
This layout means that reading a PLC program is exactly like tracing a path for electrical current. If there is a closed, unbroken path of "true" conditions from the left rail to the right rail, virtual power flows down the rung, and the output device turns on. If any condition on the left is open or "false", the power is blocked, and the output remains off.
Continue to standard instructions (NO, NC, coils) to see how this works in code
Let's zoom in on the fundamental building blocks of Ladder Logic. Every program you write is constructed using three basic types of instructions that mimic physical electrical components: Normally Open contacts, Normally Closed contacts, and Output Coils.
The Three Essential Instructions
-
Normally Open Contact (NO): Represented by the symbol
[{<-| |--|}]. This acts like a standard light switch that is currently turned off. In a digital PLC, this instruction tells the CPU to examine a specific memory bit. If that bit is0(False/Off), the contact remains open and blocks the virtual power. If the bit becomes1(True/On), the contact closes, allowing power to flow past it. -
Normally Closed Contact (NC): Represented by the symbol
[{<-|/|--|}]. This is the exact opposite of the Normally Open contact. It acts like a switch that is naturally closed (passing power) until someone presses it to open it. If the monitored memory bit is0, the contact remains closed and lets virtual power flow through. If the bit turns to1, the contact opens up and blocks the power. -
Output Coil (OTE): Represented by the symbol
[{<-( )->}]. This represents the actuator or device you want to control. When virtual power successfully flows through all the contacts on the left side of the rung and reaches this coil, the PLC sets its corresponding output bit to1(turning on the real-world device, like your conveyor motor). If the power path is blocked, the coil is de-energized, and the output bit drops to0.
To tie this together, think of your conveyor belt: we could use a Normally Open contact representing a "Start" push button connected directly to an Output Coil representing the conveyor motor. When a worker pushes the physical button, the -| |-- contact closes, virtual power flows across the rung, and the -( )- coil turns on the motor.
Now, how do you think we would handle a safety "Stop" button? Would we want to use a Normally Open or a Normally Closed contact so that pressing it immediately cuts power to our motor?
I think we would use a Normally Closed contact
Spot on! You nailed it. We absolutely want a Normally Closed (NC) contact for the safety stop button.
Why Normally Closed for Safety?
Using a Normally Closed contact [{<-|/|--|}] means virtual power flows through the contact by default when the safety button is NOT pressed. When a worker hits the physical emergency stop button, it breaks the circuit, opening the contact, cutting off the power, and instantly de-energizing our output coil to stop the conveyor.
There is also a massive hardware safety advantage here. If a physical wire accidentally breaks or gets cut on the factory floor, the electrical circuit is broken. Because the PLC is looking for a closed path to keep running, that broken wire acts exactly like someone pressing the stop button, safely shutting down the machine. If we had used a Normally Open contact, a broken wire would mean the machine could never be stopped in an emergency. This design methodology is a cornerstone of [{
Now that we have covered Normally Open contacts, Normally Closed contacts, and output coils, we are ready to combine them into simple control circuits. The next step in our learning path is understanding basic boolean logic (AND, OR, NOT) using these symbols. Are you ready to continue to boolean logic?
Continue to boolean logic (AND, OR, NOT)
The AND, OR, and NOT Gates
- AND Logic (Series Circuit): To create an AND condition, we place two or more Normally Open contacts side-by-side on the same rung. Virtual power has to flow through the first contact AND the second contact to energize the output coil. If either switch is open, the path is broken.
- OR Logic (Parallel Circuit): To create an OR condition, we place contacts on parallel branches (often called a "branch" or "nested rung"). Virtual power can flow through the top path OR the bottom path. If either contact closes, the output coil energizes.
- NOT Logic (Inversion): This is simply a Normally Closed contact. If the memory bit is NOT active, power flows. If the bit becomes active, power is cut.
| Logic Gate | Ladder Representation | Real-World Example |
|---|---|---|
| AND | Contacts in series: -| |-| |- | Conveyor runs only if Guard Gate is Closed AND Start Button is Pressed |
| OR | Contacts on parallel branches | Conveyor can be started from Operator Panel A OR Operator Panel B |
| NOT | Single Normally Closed contact: -|/|- | Lubrication pump runs only if High Temp Limit is NOT Reached |
Let's put this into practice on your conveyor belt system.
We want the conveyor motor to run, but only if two conditions are met: the operator must press the Start button, and the safety guard gate must be fully closed (which keeps a safety limit switch activated). If someone opens the guard gate while the machine is running, it must instantly shut down.
To build this safety interlock, would we place our Start button contact and our Safety Gate contact in series (an AND gate) or in parallel (an OR gate)?
We should place them in series (AND gate)
Exactly! Placing them in series is correct because it creates an AND gate.
The AND Gate in Action
By placing the Normally Open [{<-| |--|}] contact for the Start button and the Normally Closed [{<-|/|--|}] contact for the Safety Gate in series on the same rung, we ensure that virtual power can only reach our conveyor motor coil if the first contact is closed AND the second contact remains closed. If the guard gate is lifted, the safety switch opens, instantly breaking the chain and shutting down the motor.
We have covered how hardware communicates, how the internal scan cycle functions, and how to build basic logical gates like AND, OR, and NOT. This completes the core fundamentals of Ladder Logic!
Next, we need to transition to our third major subtopic: Advanced PLC Programming Concepts, where we will look at how PLCs keep track of time and count events on the factory floor. Are you ready to see how timers work?
Continue to Advanced PLC Programming Concepts
Introduction to PLC Timers
Welcome to our third major milestone! Now that you have mastered basic contact and coil logic, we are stepping into Advanced PLC Programming Concepts. In a real factory, processes do not just happen instantly based on On/Off switches; they rely heavily on physical duration. This is where come into play.
- Timer On-Delay (TON): This is the most common timer in industrial automation. When the rung conditions leading into a TON instruction become true, the timer starts counting. It waits for a specific preset duration to pass before it finally turns on its output bit.
- Timer Off-Delay (TOF): This does the opposite. When the rung conditions leading into a TOF instruction turn off, the timer's output bit remains on for a set duration before finally turning off.
- Pulse Timer (TP): This timer generates an output pulse of a precise, fixed duration. When the rung input transitions from Off to On, the timer instantly turns on its output and keeps it on for exactly the set duration, regardless of whether the input switch remains held down or is released.
- Preset Value (PRE): This is the target duration you want the timer to track (for example, for a delay).
- Accumulated Value (ACC): This is the live stopwatch value, showing exactly how many milliseconds have elapsed since the timer started counting ( up to your PRE value).
- Done Bit (DN): This is the digital output bit. For a TON timer, this bit turns from
0to1the moment the ACC value reaches the PRE value.
Let's put this straight into your conveyor belt system.
When a box passes by a physical sensor, we want to trigger a spray nozzle to apply a label, but we want to wait exactly after the sensor detects the box before we turn the spray nozzle on.
To create this delay, which of our three timer types would we use?
We should use a Timer On-Delay (TON)
You are absolutely correct! A Timer On-Delay (TON) is precisely what you need here.
How the TON Timer Solves the Box Detection Problem
[{<-| |--|}] closes. This starts the TON timer block.
Because we set the Preset Value (PRE) to , the timer's Accumulated Value (ACC) begins counting up from . During those first two seconds, the Done Bit (DN) remains at 0, keeping the spray nozzle off.
As soon as the ACC reaches , the DN bit instantly flips to 1. We use this DN bit to trigger the Output Coil [{<-( )->}] for the spray nozzle, perfectly applying the label exactly two seconds after detection.Now that you have got timers completely figured out, we can transition to the next critical concept in advanced automation: Counter instructions (CTU, CTD), which let us track and limit discrete physical events on the line.
Continue to Counter instructions (CTU, CTD)
Let's pivot seamlessly from tracking time to counting physical items on your assembly line.
In PLC programming, we handle this using to monitor discrete physical events, like boxes passing by on a conveyor belt.
The Up and Down Counters
- Count-Up (CTU): Every time the rung transitions from false to true, the counter increases its current tally by 1.
- Count-Down (CTD): Every time the rung transitions from false to true, the counter decreases its current tally by 1.
- Preset Value (PV): The target number of events you want to count (e.g., 10 boxes for a full case).
- Accumulated Value (CV): The live, ongoing tally of counts stored in the PLC's memory registers.
- Done Bit (DN): The digital status bit. For a CTU, this bit flips to
1the instant your Accumulated Value (CV) becomes equal to or greater than your Preset Value (PV).
Let's apply this directly to your conveyor system.
We want to pack boxes into shipping cases. A sensor counts each box as it slides off the conveyor. When exactly 6 boxes have passed by, we want to temporarily stop the conveyor belt so an operator can seal the case.
To implement this logic, we will feed the box sensor's Normally Open contact [{<-| |--|}] into a CTU block with a PV of 6.
How do you think we should use the counter's Done Bit (DN) to control the conveyor belt motor? Would we use a Normally Open contact or a Normally Closed contact in series with the motor coil to stop it?
Use a Normally Closed contact of the DN bit
Spot on again! You are flying through these logical concepts. Using a Normally Closed (NC) contact of the counter's Done Bit () in series with the motor coil is the perfect way to pause the system.
The Counter-Based Stop in Action
1.
This transitions our Normally Closed contact from closed to open, immediately breaking the electrical path, de-energizing the motor coil, and stopping the conveyor. Once the operator finishes sealing the box and presses a physical Reset button, the PLC program executes a (Reset coil) (RES) to clear the accumulated value back to 0. This drops the bit back to 0, re-closes our Normally Closed contact, and allows the conveyor to start running again.Introduction to PLC Data Registers
Show me a mathematical register example
Let's dive straight into a worked example of how a mathematical register works in practice. This is where we bridge the gap between simple On/Off states and actual numerical calculations.
The Total Throughput Calculator
Continue to Hands-on Setup and Simulation
Simulating PLCs on Your PC
- The Siemens Ecosystem: Siemens offers a programming suite called TIA Portal (Totally Integrated Automation). You can download a free, fully functional 21-day trial of this industrial software. When paired with PLCSIM (Siemens' virtual controller software), it lets you write actual ladder logic, download it to a simulated S7-1200 controller, and watch the registers and bits execute on your screen.
- The Codesys Platform: Codesys is the hidden gem of industrial automation. It is a completely free, manufacturer-independent software suite that adheres strictly to global programming standards. Codesys has a powerful built-in software simulator. With a single click, you can turn your personal laptop into a running PLC, complete with interactive visualization screens you can design yourself.
Which path appeals more to you? We can walk through downloading Codesys to get you programming immediately, or we can look at setting up the industrial Siemens trial.