How to Teach Yourself PLC Programming

Chapter 29 - Basics of Programmable Logic Controllers (PLCs)

PDF Version

First and foremost, you need to get your very own PLC to work with. Computer programming of any kind is not a spectator sport, and can only be learned by significant investment of time and effort at the keyboard. In many ways, learning to program is like learning a new spoken or written language: there is new vocabulary and new grammatical rules to master, and many ways to make mistakes.

Fortunately, many low-cost PLCs exist on the market for individuals to purchase. My own personal favorites are the “CLICK” PLC models manufactured by Koyo and marketed through Automation Direct, and also the Allen-Bradley MicroLogix series of PLC (especially the 1000 and 1100 models).

The first document you should read once you get your PLC is something called a Getting Started guide. Every PLC manufacturer publishes a document with this name (or something similar such as Quick Start or Getting Results). This manual will step you through all the basic procedures for entering a simple program into your PLC and getting it to run. It is generally far easier to learn programming by copying and adapting a worked example than it is to start from a “blank page” on your own, just as it is easiest to learn a spoken or written language by practicing sentences spoken in that language by other people before constructing your own sentences from scratch.

In order to work with your PLC, you will need a convenient way to simulate different input conditions coming from discrete (switch) devices. Any set of hand-operated switches will do, my recommendation being household light switches (very inexpensive and rugged). Attaching an array of these switches to a wooden board along with the PLC and interconnecting terminal blocks forms what is often called a PLC trainer. The following photograph shows one such trainer344, using an Allen-Bradley MicroLogix 1000 PLC:

Another example of a student-built PLC trainer is this unit, housed inside of an attaché case. Not only does this trainer contain an Allen-Bradley MicroLogix 1100 PLC along with input switches and output indicator lights, but it also includes an HMI touch-screen panel on a fold-down bracket:

The educational value of building your own PLC trainer is difficult to overstate when learning about PLCs. Learning how to build properly-functioning I/O circuits is every bit as important to a working technician as learning how to develop PLC programs. Additionally, the experience gained in general wiring layout and fabrication are valuable skills for any instrumentation practitioner.

Once you have learned the basic steps for entering, running, and saving a PLC program, you are ready to begin building your knowledge of the language’s vocabulary and grammar. In computer programming (of all types), there are different functions of the language one must become familiar with in order to do useful tasks. A great way to learn how to use these functions is to create your own “demonstration” programs illustrating the use of each function.

For example, if you open up the pages of almost any computer programming book, somewhere near the beginning you will find a demonstration program called “Hello World!” The purpose of a “Hello World!” program is to do nothing more than display the words Hello World! on the computer screen. It is an entirely useless program to run, but it is highly useful for gaining teaching the programmer the basics of program construction and text message functionality.

By the same token, you may learn the basics of each programming function by writing simple “Hello World”-type programs illustrating each one of those functions. These demonstration programs will not serve any useful purpose (other than to help you learn), and should be kept as simple as possible in order to minimize confusion.

For example, every PLC provides instructions to perform the following tasks:

  • Turn discrete outputs on and off
  • Count discrete events
  • Time events
  • Control events in a specific sequence
  • Compare numerical values (greater than, less than, equal, not equal)
  • Perform arithmetic functions

Just as every spoken or written language has verbs, nouns, adjectives, and adverbs to describe actions and things, every PLC programming language has specific functions to perform useful tasks. The details of how to perform each function will vary somewhat between PLC manufacturers and models, but the overall functions are quite similar. The reference manuals provided for your PLC will describe in detail how to use each function. Your task is to write simple demonstration programs for each function, allowing you to directly explore how each function works, and to gain an understanding of each function by observing its behavior and also by making (inevitable) mistakes.

After writing each demonstration program, you should add a lot of comments to it, so you will be able to understand what you did later when you go back to your demonstration program for reference. These comments should cover the following points:

  • Proper use of the function
  • A verbal description of what the function does
  • A list of possible (practical) uses for the function
  • Idiosyncrasies of the function (i.e. odd or unexpected behavior, tricky points to watch out for)
  • Mistakes you may have made (and thus might make again!) in using the function

Years ago when I was teaching myself how to program using the C language, I wrote a set of “tutorial” programs demonstrating common programming functions and techniques. The following is a partial list of these tutorial programs, which I still keep to this day:

  • Program that accepts and then prints alphanumeric characters (including their equivalent numerical values)
  • Program demonstrating how to use command-line arguments to the main() function
  • Program demonstrating basic “curses” commands for plotting characters at arbitrary locations on the screen
  • Program illustrating the declaration and use of data structures
  • Program illustrating how to prototype and then call functions (subroutines)
  • Program executing an infinite loop
  • Program illustrating how to return a pointer from a function

Each one of these tutorial programs is heavily commented, to explain to myself in my own words how they work and what they are doing. Not only did they help me learn how to write programs in C, but they also serve as a handy reference for me any time in the future I need to refresh my knowledge. The act of writing tutorial programs is akin to journaling as a way to work through complex problems in life – in a way, it is like having a conversation with yourself.