User Tools

Site Tools


EtherCAT based zero crossing detection

The frequency measurement solution in Lübeck, at the east coast of northern germany, is based on industrial automation components as EtherCAT power monitoring terminals from the company Beckhoff Automation.


An Embedded-PC of type type CX8110 with the Windows Embedded Compact 7 operating system is used. Attached to the Embedded-PC is the EL3773, an EtherCAT Power Monitoring Terminal whose allows to measure 3 phase voltage and current with a sampling rate of up to 10 kHz. Connected to this terminal is a single phase of the voltage from the 230V in-house voltage of the supply network in our office.

An Embedded-PC of type CX5130 with the TwinCAT/BSD operating system (details below) is used. Attached to the Embedded-PC is the EL3783, an EtherCAT Power Monitoring Terminal whose allows to measure 3 phase voltage and current with a sampling rate of up to 20 kHz. The actual hardware setup is somehow oversized for this use case, as well as the additional two phases voltage and the current measurement, which are irrelevant to this use case of mains frequency measurement. Nevertheless, this hardware was available from day-to-day operations and was therefore used. A competitive solution could e.g. be to combine a CX7000 with an EL3443 or EL3773.


On the Embedded-PC a TwinCAT/BSD is installed, which provides a TwinCAT 3 Runtime on a FreeBSD operating system. The TwinCAT Runtime is a so-called Soft-PLC, a programmable logic controller (PLC) with hard real-time capabilities in software. It is possible to configure the hardware as well as program a logic in languages from the IEC 61131-3, but also in C/C++, from Matlab/Simulink or a combination of thereof.

Within TwinCAT there is a EtherCAT Master integrated whose allows to interface the EtherCAT Power Monitoring Terminal. There are I/O terminals whose already internally calculate the frequency from the connected signal, but the chosen terminal EL3783 is sampling the voltage (and current) as instantaneous values. Based on the EtherCAT Oversampling principle the EL3773 samples the values with up to 10 kHz and passes them as array to the PC. Based on the EtherCAT Oversampling principle the EL3783 samples the values with up to 20 kHz and passes them as array to the PC.

Zero Crossing Detection

A task with a cycle time of 1 ms is configured in TwinCAT, so that with every cycle the values from the power monitoring terminal are retrieved and passed to the logic. Because of the sampling of 10 kHz in the terminal, every cycle an array of 10 values from the measured voltage is transferred to a program and evaluated there.

Because these raw values of the mains voltage are sampled with 10 kHz and therefore equally spaced in an interval of 100 us, it is necessary to calculate a mains frequency from it in the TwinCAT Runtime. There are libraries available to do so, but in this research project we intend to implement our own experimental algorithm. Therefore the following algorithm has been implemented whose detects a zero crossing within the voltage signal.

By iterating through the signal a zero crossing in the positive and the negative direction is detected by checking whether one signal has another leading sign in comparison to the next signal. If this is the case a counter will be initiated to count the amount of values until the next zero crossing with the same direction is detected. The value of this counter, multiplied with the sampling interval of 100 us correspond to the frequency. Because there are positive and negative directions of the zero crossing, this allows to use two independent counter and combine those for a single frequency value. In the European grid with a designated mains frequency of 50 Hz and therefore a period of 20 ms, this procedure allows to calculate a new frequency value every 10 ms.

Due to the fixed sampling rate, it is not possible to measure the zero crossing exactly and therefore the possible inaccuracy of up to 100 us also results in a corresponding deviation in the determined frequency. In order to correct this issue, the determination of the exact time of the zero crossing was optimized by using a linear equation: The shape of the actual sinusoidal signal at the zero crossing is assumed to be a straight line. Therefore the measured value before the zero crossing is combined with the measured value after the zero crossing and by solving the straight line equation of these two points an improved time of the zero crossing is determined.

Time Synchronization

With the measurement results from this algorithm, compared to other measurements from the project, we can now nicely check how well it is performing. However, in order for a real comparison of the values to be possible, a time stamp as exact as possible must also be assigned to each specific frequency value. This allows the measurements to be compared with each other and over time.

For this purpose an NTP time synchronization was integrated as part of TwinCAT, which determines an offset of the between the TwinCAT system and an external time source in the internet. With this determined time offset, the time stamps can then be corrected and used to provide the data.

MQTT Communication

Providing the data to the MainsCnt project and storing it together with the other measurements into a database has been implemented by a MQTT communication, sending the data in a JSON format. In the TwinCAT application the JSON string will be prepared and directly send via the in TwinCAT integrated MQTT client (part of the TwinCAT 3 IoT Communication suite).

This MQTT client connects via TLS to the MainsCnt MQTT Message Broker. The TLS communication is established by use of certificates to authorize the client, as well as for encryption and signing of the MQTT messages. In the MQTT messages the JSON string is sent as payload to a specific topic on the MQTT broker.

A dedicated Node-RED instance is used to connect also to the MQTT broker, subscribe to the topic and retrieve the JSON payload. By a MQTT Ingress Processor the JSON is evaluated and then stored into the database whose will be accessed by Grafana to visualize the measurements.


Location Changes
2021-08-20 Replace CX5130+EL3783 with CX8110+EL3773
devicebeckhoff.txt · Last modified: 2021/08/20 11:40 by nilsj