Grafcet DIN EN 60848 on the Raspberry Pi

Read here how to use the Grafcet Engine on a Raspberry Pi and then transfer a Grafcet plan into the device.

Information about the Raspberry Pi can be found here.



You need Grafcet-Studio version or newer!

Users of the Raspberry Pi usually use the Python programming language because it is very easy to learn and easy to use. For this reason we provide the Grafcet-Engine as a Python (V3) library.

How does it work?

The Raspberry Pi runs a Python script (V3) and communicates via MQTT with a Windows PC running the Grafcet Studio software. With Grafcet-Studio, Grafcet charts can be created and downloaded to the Raspberry Pi at the push of a button. The Grafcet logic then runs independently on the Raspberry Pi.

The combination of Raspberry Pi and Grafcet Studio offers the following possibilities:

  • Creation of Grafcet charts with Grafcet Studio
  • Download the Grafcet logic into the Raspberry Pi (this only takes 1 second)
  • Monitoring (debugging) of the Grafcet logic
  • Any sensors and actuators that can be used via Python can be used with the Grafcet logic (= no restrictions).
  • The Raspberry Pi can be connected via the local network or via the Internet (MQTT makes this possible).

Below is a diagram of MQTT communication between PC and Raspberry Pi (with and without the Internet).

MQTT communication over the Internet

Here a public MQTT broker is used and the communication is handled over the Internet.

MQTT communication in the local network

It is also possible without Internet: In the local network the PC or the Raspberry Pi can provide an MQTT broker.

System requirements

  • Grafcet Studio Pro Edition or IoT Edition or Grafcet Studio Demo version
  • Raspberry Pi (any variant) with Python3

Prepare Raspberry Pi

We recommend to use the operating system Raspian. After the installation Python3 is immediately available.

The image can be downloaded here:


  1. Download the operating system (see
  2. Copy the image to an SD card (min. 16 GB). For writing the image you can use the Win32 Diskmanager.
  3. Insert the SD card into the appropriate slot on the Raspberry Pi and turn it on. Screen, mouse and keyboard should be connected.
  4. After booting, select the country and the language. Then you have to set the password and the WLAN settings. After the reboot the desktop is available.

Install MQTT

The Grafcet Engine communicates with Grafcet-Studio via MQTT. Therefore the library "paho-mqtt" must be installed. Start the terminal and enter the command:


sudo pip3 install paho-mqtt

When the PC and Raspberry Pi are connected to the Internet, a public MQTT broker can be used. In this case, the steps in the next section are not necessary.

Install a MQTT Broker if you want to communicate over the local network.

To ensure that communication works without the Internet, install an MQTT Broker on the PC or on the Raspberry Pi. Here you can download e.g. a MQTT Broker for the PC free of charge:


MQTT Broker for the PC:

After starting the MQTT Broker a command window (console) opens and afterwards you can communicate via it in the local network. In Grafcet-Studio in this case you have to enter the local host address in the connection settings under "IP address":

Include the Grafcet engine in the Python script

Download the following ZIP file:

In the ZIP file you will find these files:

  • = 1st example with use of the GPIO
  • = 2nd example with using a E/A Shields
  • = Grafcet-Engine as Python code. The 2 examples use this library.

Example 1: Using GPIO directly

The test setup: 4 LEDs on pins 14,15,18,23 (BCM numbering)

The Python script

In this example 4 outputs of the R-Pi are used.

No Explanation
1 Pin 14, 15,18,23 are defined as outputs (BCM numbering).
2 An instance of the Grafcet engine is created.
3 Communication is configured.
4 In the loop: Here the first 4 digital inputs are described with 1-0-1-0 for test purposes. Hardware inputs are not used in this example.
5 In the loop: The Grafcet engine is called.
6 In the loop: The first 4 dig. Outputs are read and stored in Q0-Q3.
7 In the loop: The states of Q0-Q3 are written to the hardware outputs.

The following Grafcet chart could be used to run a test:

For the communication between the R-Pi and the PC to take place correctly, these three parameters must be identical:

  1. Address of the MQTT broker
  2. Topic 1
  3. Topic 2

Topic 1 and Topic 2 are used to identify the device. You should choose it like a password.

See picture:

How to set the MQTT parameters in Grafcet-Studio can be read below.

Example 2: Using a shield

In this example we use the Pibrella I/O Shield:

Properties of the shield:

  • 4 inputs (DI) with LED
  • 4 outputs (DO) with LED
  • 3 LEDs (green, yellow, red)
  • 1 simple speaker (buzzer)
  • 1 red button

With the following command the necessary library for the shield can be installed:


sudo pip3 install pibrella

Via the Grafcet logic the green and yellow LED flashes. The red LED and the speaker can be switched on via the red button. In the following picture, the red button is pressed:

The picture shows the Grafcet logic:


Here you can download the Grafcet plan in Grafcet-Studio format:

No. Explanation
1 Grafcet logic for LED green and yellow.
2 Grafcet logic for LED red and speaker. These are switched on via the red button.
3 I/O-Panel: Display of inputs and outputs and their states.
4 Symbol table
5 The outputs start at address 25.

Here the corresponding Python script

No. Explanation
1 The Grafcet engine is imported.
2 The library for the Pibrella shield is imported.
3 An instance of the Grafcet engine is created.
4 The communication is set up (the parameters must match the parameters in Grafcet Studio)
5 In the loop: the state of the button is written to the Grafcet input "I0" (address 0.0).
6 In the loop: The Grafcet outputs are read.
7 In the loop: The graphcet outputs are written to the shield.

Read and write symbols of the Grafcet-Engine


In order to address the hardware of the Raspberry Pi via the Grafcet logic, the Grafcet inputs must be written and the Grafcet outputs must be read out.

Explanation: Symbols for inputs and outputs are defined in Grafcet Studio. The Grafcet logic works with these symbols. A connection must now be established between the hardware of the Raspberry Pi and these symbols so that the Grafcet logic can use the hardware of the Raspberry Pi. Read and write functions are provided for this purpose. (The read_bit and write_bit functions have already been used in the example Python scripts.)

The Grafcet input symbols must therefore be written with the current values of the hardware inputs. And the states of the Grafcet outputs are written to the hardware outputs of the R-Pi.

The following read functions are available.

Read function Explanation
read_bit(addr, bit) Returns true or false (1 bit)
read_uint8(addr) Returns a byte value (8 bit) as the function result
read_uint16(addr) Returns an unsigned 16-bit integer value as a function result
read_uint32(addr) Returns an unsigned 32-bit integer value as a function result
read_int16(addr) Returns a signed 16-bit integer value as function result
read_int32(addr) Returns a signed 32-bit integer value as a function result
read_float(addr) Returns a float value (32 bit) as function result

The following write functions are available.

Write function explanation
write_bit(addr, bit, bitvalue) The given value is written at the given address
write_uint8(addr, bytevalue) The given value is written at the given address
write_uint16(addr, uint16value) The specified value is written at the given address
write_uint32(addr, uint32value) The specified value is written at the given address
write_int16(addr, int16value) The specified value is written at the given address
write_int32(addr, int32value) The specified value is written at the given address
write_float(addr, floatvalue) The specified value is written at the given address


Summary: The following three things must be done in a loop: the hardware input states must be written to the Grafcet inputs, the Grafcet cycle 'GrafcetCyclic' must be called and the output states of the Grafcet outputs must be written to the hardware outputs. A programmable logic controller (PLC) also works in a similar way.

Set up Grafcet Studio, that the Raspberry Pi can be programmed via "Grafcet"

Add an MQTT device in Grafcet-Studio:

Step Explanation
1 Display the 'Grafcet Engine' window and press the Plus button. If the simulator is still connected, press the disconnect button first.
2 The 'Add Device' dialog appears. Select from the list: 'Grafcet-Engine MQTT'
3 Enter a short description. This will be available later as a selection in the list.
4 Press the 'Ok'
5 Enter the connection parameters: Address of the Mqtt broker, Topic1 and Topic2. The two topics are arbitrary, but at least 3 characters long. The device is identified via these topics. These topics must match the topics in the Python code. Otherwise no connection is established.
6 Press the 'Ok' button.
7 Select the Device you just created
8 Press the Connect button. (The Python script should already be started.)

In the Python script the connection settings must be identical. See point 5. Otherwise no communication can take place. Here again the corresponding picture:

Summary of the possibilities

  • The Raspberry Pi can be programmed via "Grafcet".
  • Other microcontroller boards that support Python can also be programmed in the same way.
  • The state of the Grafcet can be monitored (debugged) via Grafcet-Studio.
  • Any type of shield can be used
  • The R-Pi can be located in the local network or it can also be accessed via the Internet.