When I started out with TwinCAT programming some time ago, I had basically not heard about PLCs. My background was not even close to automation, mostly doing C/C++ on various Linux-based systems. I had two friends which previously were doing a little automation, and both of them said “Don’t start working with PLCs. It’s boring“. Now that I’ve dabbled in “traditional IT” software development and industrial automation software development, I definitely don’t agree with the latter being boring. Industrial automation using PC-based control is insanely fun! At my first job where they used “this thing called TwinCAT” (which was unknown to me at that time), I did what I usually do: I started googling stuff like “free TwinCAT tutorial, how to program with TwinCAT, free TwinCAT course, TwinCAT open source” and so forth. I didn’t find much on Google or in any other of the places I usually used to learn a new programming language (forums, stack overflow, open source projects, etc). The amount of free resources was very limited. After having my TwinCAT blog for a few years, I even got an (anonymous) message via the contact form on my blog from someone saying that I’m an idiot that shares information and knowledge for free, and that nothing should be done for free and that I should basically stop writing my blog. For these reasons, I decided to do a free TwinCAT 3 tutorial.
When I started out and was trying to look for resources to get me a kick-start, I quickly found out that the learning material ecosystem for PLCs/automation mostly had these limitations:
- you have to register on a website to get access to the tutorial or
- the tutorials are available only in a specific country/region or
- the tutorials are costly
Or a combination of all above. This means that the learning curve will be slightly steeper for learning TwinCAT. If I want to learn Python, C++, C#/.NET, windows batch-scripting ( 🙂 ) or any “conventional IT” programming language, there are tons of free information on the Internet available. If I want to learn a new programming language, I’d like to be able to start quickly (kudos to Beckhoff for having a cost-free development environment and runtime making this possible) and just start to hack. When creating the content of this tutorial, the ambition was that it should be the tutorial that I wish I had when I started with TwinCAT 3 development. It’s available on YouTube, so no registration, no region limitations, and zero cost. All you need is a computer with the TwinCAT 3 development environment and runtime (free from Beckhoff, although unfortunately you have to register at their website to download it), then you can start to watch, listen & learn. This tutorial has taken me several hundreds of hours of my spare-time to create, so I really hope that people will find this series of tutorials useful. Know anyone that wants to learn PLC programming? Please share the tutorial among your friends!
The tutorial consists of a total of 18 parts, covering various aspects of TwinCAT 3. The 18 parts are divided into two main groups, ■ basic and ■ advanced.
Each part has English and German subtitles, which are enabled by clicking the following icon on a PC/Mac:
or this icon on Android/iPhone/Mobile:
Content of the tutorial
■ Basic group
■ Part 1 – Introduction
The first part will be an introduction to why this tutorial was created, the scope and an overview of the content of this tutorial.
■ Part 2 – Basics & installation
Here I will give some background information to PLCs and TwinCAT 3, and describe the difference between the development environment and the runtime. We will also download and install the TwinCAT 3 development environment (XAE) so that we have everything up and set for our first program.
■ Part 3 – Tasks, programs & “Hello world”
In this part we will go through some basics regarding why a standard operating system is not suitable for many of the industrial automation tasks, and why you need something like TwinCAT for certain applications. We will look into how TwinCAT is co-existing next to the operating system and how they co-operate. Next, we will create our first TwinCAT 3 solution and learn how to configure the real-time properties of our project. In order to run any TwinCAT software, we will also need to enable virtualization in the BIOS of the computer, which will be shown. We will briefly look at how licensing works in TwinCAT 3, and after that we will create the classical “Hello world” program, although it will probably be different compared to the conventional programming languages you might have worked with previously.
■ Part 4 – Data types & arrays
In this video we will look into the various data types that are available in the IEC 61131-3 standard, pointers & references and how to work with arrays. We will cover why pointers can be dangerous and why you generally want to prefer to use references instead. Then we will finish the tutorial by looking at how we can convert between different data types.
■ Part 5 – Structures & functions
Arrays allow us to define types of variables that can hold several data items of the same kind. In this part of the tutorial we will look at a data unit type that allows us to hold several data items of different kind. We will also look at one of the basis of modularization and re-use in software development called functions. We will look at how we can get data in and out of functions, and we will also look at the difference between passing parameters by value and by reference. We will finish this part by writing our very first function!
■ Part 6a – Function blocks & interfaces
In this part we will start to look into the object oriented programming parts of structured text and IEC 61131-3 called function blocks. For those that are Java, C, C++ or just “traditional” programmers, going into the realm of function blocks is like going from C-style structures and functions, and into classes in C++. With function blocks we can go from working in a procedural style programming into objected oriented style programming.
■ Part 6b – Function blocks & interfaces
In this part we will continue our journey of the object oriented features of IEC 61131-3 and look into something called interfaces. Interfaces provide a layer of abstraction so that you can write code that is ignorant of unnecessary details. Interfaces aid you in designing more modular and robust software. With interfaces it’s possible to decouple direct dependencies between objects in your software.
■ Part 7 – Instructions
Here we will cover if/else, case-switches, for/while-loops and some other basic instructions. We will utilize our knowledge to write a CSV (comma separate value) event logger by using a state machine.
■ Part 8 – Tc2_Standard
As TwinCAT 3 conforms to the IEC61131-3 standard, there are certain things it has to be able to do. The Tc2_Standard library has many of the standard IEC functions such as timers and triggers, which we will look into in this chapter.
■ Part 9 – TwinCAT utilities
In this part we’ll learn how to use one of the most used Beckhoff libraries for various purposes. We’ll learn how to measure execution time of PLC code, how to use a FIFO buffer and how to combine the power of using a TwinCAT real-time program with an application running in user-space (Windows).
■ Part 10 – I/O
When designing and building a control system you will eventually want the control system to actuate something, be it a relay, a mixer, a boiler or maybe a pneumatic system. To get feedback of the actuation, sensors are needed. In this part we will cover how we communicate with the environment using inputs and outputs.
■ Part 11 – Libraries
Once the complexity of your software gets to a certain level it might be a good idea to start to think about splitting the software into different libraries. This part will go through how this is accomplished and what you need to think of.
■ Part 12 – TwinCAT functions
When installing the TwinCAT development environment and runtime you get access the core functionality, but sometimes you might want to extend this with additional functionality as for example adding an SQL database connection. In this chapter we will investigate some TwinCAT functions that can be added.
■ Advanced group
■ Part 13 – Version control
Version control in the world of automation has historically not been the highest priority, but with TwinCAT 3 this is entirely possible using standard version control software. In this part we will go through how we can do proper version control using the Git version control system.
■ Part 14 – Handling of different TwinCAT versions
When developing TwinCAT software over time you will most likely end up in a position where you must be able to develop and maintain software for various versions of TwinCAT. This chapter will go through how this is done.
■ Part 15 – ADS
The Automation Device Specification is Beckhoffs middleware to communicate with Beckhoff PLCs. It’s used for all kinds of use cases, as software deployment, reading/writing of variables and for internal communication of software modules. In this part we’ll go through the theory and also write some C# software to communicate with a PLC. We’ll also do a simple C++ program running under Linux that talks to the PLC!
■ Part 16 – TwinCAT automation interface
With the TwinCAT automation interface it’s possible to automate certain parts of the configuration, development and deployment process of your PLC software. In this chapter we will look into the possibilities with the TwinCAT automation interface and look at one example.
■ Part 17 – Test driven development
Test driven development (TDD) has been around in the field of software engineering for quite some time. The world of automation has been lacking behind, but nowadays it is entirely possible to do unit testing with TwinCAT using the TcUnit unit testing framework. This chapter will show how to do TDD with TwinCAT.
■ Part 18 – Final words
A tutorial is not complete without a few final words to wrap up what we have learned and where to go from now.