KL6821 Integration into TwinCAT (BC9191)
This example explains how to write a simple PLC program for DALI in TwinCAT and how to link it with the hardware. The task is to control an individual dimmable lamp via a button.
Hardware
Setting up the components
The following hardware is required:
- 1x Bus Terminal Controller BC9191
- 1x potential feed terminal 24V DC e.g. KL9190
- 1x digital 2-channel input terminal KL1104 (for the dimming and reset functions)
- 1x DALI terminal KL6821
- 1x end terminal KL9010
Set up the hardware and the DALI components as described in the associated documentation.
This example assumes that a Dim button was connected to the first KL1002 input and a Reset button to the second, and that a dimmable lamp is connected to DALI address 0. Set the initial fade rate of the ballast to 7 to achieve suitable dimming.
Software
Creation of the PLC program
Create a new PLC project for BC-based systems (BCxx50 via AMS) and add the libraries TcDALIV2.lbx and TcSystemBCxx50.lbx. Then navigate to Project→Options... →Build and select TreatLREAL as REAL.

Under Controller Settings, select BC9191 and set the Maximal Number of POUs to 1000.
Next, generate the following global variables:
VAR_GLOBAL
bSwitch AT %I* : BOOL;
bReset AT %I* : BOOL;
stKL6821InData AT %I* : ST_KL6821InData;
stKL6821OutData AT %Q* : ST_KL6821OutData;
stCommandBuffer : ST_DALIV2CommandBuffer;
END_VAR
bSwitch: Input variable for the Dim button.
bReset: Input variable for the Reset button.
stDALIInData: Input variable for the DALI terminal.
stDALIOutData: Output variable for the DALI terminal.
stCommandBuffer : Required for the communication with DALI.
Since BC devices can only process one task, communication with DALI cannot run separately.
Therefore, create a MAIN program (CFC) in which the FB_KL6821Communication() and FB_DALIV2Dimmer1Switch() function blocks are called. Make sure to link the communication block with stDALIInData, stDALIOutData and stCommandBuffer.

Connect the input bSwitchDimm of the dimmer block with the global variable bSwitch and stCommandBuffer with the global variable stCommandBuffer.

![]() | Parameter Make sure that the specified parameters nMinlevelMasterDevice and nMaxLevelMasterDevice match the minimum and maximum values of the device, to avoid malfunction. |
Go to the task configuration and give the task a lower interval time. More detailed information can be found in the FB_DALIV2Communication() block description.

Now load the project as a boot project to the BC and save it.
Configuration in the System Manager
Create a new TwinCAT System Manager project, select the BC as the target system, and search for the associated hardware.
Add the PLC program created above under PLC configuration.

Now link the global variables of the PLC program with the Bus Terminal inputs and outputs, create the allocations, and activate the configuration. Then start the device in run mode.
Your BC is now ready for use.
The lamp can now be controlled by pressing or holding the dimmer button. Use the Reset button to reset the entries in arrBufferMaximumDemandMeter and arrBufferOverflowCounter.