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:

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.

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.

22 Comments, RSS

  1. ypi

    Hello,

    Im looking for a way to programatically activate the free-run state of a TwinCAT3 project. Do you know any way to do this?

    Thanks and super great blog!

    -A

  2. sumith

    I have been watching your tutorial for the last few days and it’s very useful for me, keep going.
    It would be a great help for those who interested in twincat .
    Thank a lot

  3. BusaRider1

    Love it!! Keep up the great work Jacob! AWESOME!!! Trust me, it’s very well appreciated from all of us using TwinCAT.

  4. Pick

    Hello, I’am training on my computer and waiting for the next tutorial I try to give back 1 core to windows, because even if the twincat programm is not running the core is not free. But if I change to share core does that mean twincat still own a piece of the core? And if I let a programme (running or not) and delete the folder? To cut short, how to be sure windows get back all the ressources after a dummy played with twincat?

  5. Mykhailo

    Bardzo dziękuję za szkolenie, bardzo chciałbym nauczyć programowaniu ruchu i bezpieczeństwa, ale to prawdopodobnie będzie w przyszłości, mam wielką nadzieję.

  6. Shun-Fa

    Hi,
    Those tutorials are great! And they are very useful when I am starting to use TwinCAT3. Please help to keep the great work for us who using TwinCAT3. Really Appreciated !!

    Thank you very much.

  7. David Vrabec

    Thank you Jakob, I have only watched the first video but it already looks like it has great production quality.
    I come mostly from Siemens HW PLCs, so having everything run on a PC is quite unusual, it will be interesting to learn how Beckhoff does things, even though I won’t use the brand at my current position. It seems like there is an extreme number of components and possibilities you can use in TwinCAT compared to TIA Portal.

    Did you think about setting a patreon or “Coffee tip” button, I am sure a lot of people would like to give back for your effort on this blog and on youtube.

    • Hi David! Thanks for your response. I’ve only very briefly used Siemens, and yes it’s a big difference in the philosophy between the two companies in how to do PLC-programming.
      I’m thinking of setting up some form of membership on YouTube or Patreon, for “coffee-level” money at some point, not sure when (if ever). Right now my focus is to finish the remaining parts of the tutorial. My ambition is to share some of my knowledge, and my hope is of course that the TwinCAT3-software community will do the same. Cheers!

  8. Devashish Patel

    Hi Jacob, I just landed a job that requires me to work extensively on PLCs, specifically TwinCAT3 and for a person who has no background in PLC programming, your videos are a real blessing. Cannot wait for all the upcoming parts.

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.