I am currently trying to figure out a problem that I couldn’t understand the reason for.
Here is the schematic of the circuit: 
Last weekend, I soldered a PCB on a prototype board. The circuit contains a transformer and an STM32 (which is a Blue Pill) for time counting. The transformer is rated for 230/9 V at 50 Hz and has a power rating of 1.2 VA. The STM32 works great when I power it using the programming header with the ST-Link device. It also works with 230 V.
However, the problem arises when I apply 230 V to the circuit: the STM32 RTC time jumps randomly. For example, if I set the time to 10:45 and then power off the circuit, when I re-plug the circuit into the mains voltage after a while, the time jumps to something like 15:10. This behavior is completely random, and it doesn’t occur when I power on/off using the programming header with the ST-Link device. Therefore, the problem must be somewhere on the transformer side.
As you can see, the secondary side of the transformer is regulated by an LM317. The output of the regulator is around 4.1 V. There is an onboard regulator on the Blue Pill (RT9193) for 3.3 V regulation, which provides sufficient power for the STM32F103.
What do you think ? Any idea ?