Skip to main content

STM32 Phase Control

Overview

The STM32 controls the dimmable 230 V AC socket using phase-angle control.

The ESP32 periodically sends the mains frequency and the required output setpoint to the STM32 through a unidirectional serial connection.

ESP32 → STM32

The STM32 does not send any data back to the ESP32.

The output is intended exclusively for resistive loads with a maximum permitted power of 200 W.

Program Function

The ESP32 firmware performs the following tasks:

  • Creates a fixed 6-byte telegram
  • Adds the configured mains frequency
  • Converts the output setpoint from 0...100 % to 0...255
  • Calculates the telegram checksum
  • Periodically transmits the telegram to the STM32

The STM32 uses the received frequency and setpoint to control the phase-angle output.

Communication Direction

SenderReceiverDirection
ESP32STM32Unidirectional

No acknowledgement or process data is returned by the STM32.

Serial Interface

ParameterValue
InterfaceUART
ESP32 InterfaceSerial2
CommunicationUnidirectional
SenderESP32
ReceiverSTM32
Telegram Length6 bytes
Baud RateDefined in the Serial2 configuration
Data BitsDefined in the Serial2 configuration
ParityDefined in the Serial2 configuration
Stop BitsDefined in the Serial2 configuration

The baud rate, parity and stop-bit configuration are not defined in the shown phase-control module.

Telegram Structure

ByteDescriptionValue / Range
0Start ByteDefined by l_bStartByte
1Mains FrequencyDefined by POWER_GRID_FREQUENCY
2Socket Setpoint0...255
3Reserved0
4Reserved0
5ChecksumSum of bytes 0...4, lower 8 bits

Setpoint Conversion

The requested output setpoint is received internally as a percentage from 0...100 %.

Before transmission, the ESP32 converts the percentage to an 8-bit value:

telegram[2] = (setpoint * 255) / 100;
SetpointTransmitted Value
0 %0
25 %63
50 %127
75 %191
100 %255

Integer division is used, so intermediate values are rounded down.

Checksum

The checksum is calculated by adding bytes 0...4.

Only the lowest 8 bits of the result are transmitted:

uint16_t sum = 0;

for (int i = 0; i < telegramSize - 1; i++) {
sum += telegram[i];
}

checksum = sum & 0xFF;

The STM32 can validate the telegram by calculating the same sum and comparing it with byte 5.

Default Telegram

During initialization, the transmission buffer is prepared with an output setpoint of zero:

ByteInitial Value
0Start byte
1Configured mains frequency
20
30
40
5Calculated checksum

This ensures that the initial command represents an inactive output.

Load Restrictions

ParameterValue
Supply Voltage230 V AC
Mains Frequency50 Hz, depending on configuration
Control MethodPhase-angle control
Load TypeResistive loads only
Maximum Permitted Load200 W
Integrated Fuse1 A