PLC programming using TwinCAT 3 tutorial

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:

Swedish and Polish subtitles will soon follow. If you know a language that you think should be included as subtitle, I’d of course be happy if you want to volunteer by translating so we can add it.

I will start by publishing the first three parts of the tutorial, and will subsequently post the others one by one.

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 this part by looking at how we can convert between different data types.

Part 5 – Structures & functions

With structures (structs) you can group variables together into logical groups. We are also going to go through functions and some various ways of getting data in and out of functions.

Part 6 – Function blocks & interfaces

The closest thing we will get to concept of classes in regular programming languages in the PLC-programming world are function blocks. In this chapter we will dive into some of the equivalent building blocks of object-oriented programming languages.

Part 7 – Instructions

Here we will cover if/else, case-switches, for/while-loops and some other basic instructions.

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 chapter we’ll learn how to use one of the most used Beckhoff libraries for various purposes. We’ll add the library to our project and use some of the functions located inside of it.

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.

  • Share on:

3 Comments, RSS

  1. Stefan Henneken

    Respect. Great work. I’m sure your videos will be a great help for many TwinCAT beginners. Especially for those whose background knowledge is Java, C, or C++.

  2. Kumaraswamy Gaviyappa

    I can’t thank you enough for all that you have done for PLC Beginners. Respect!!

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.