I built a frequency counter using an ESP32 to measure frequency in Hz.
To measure mains frequency, I used a PC817 optocoupler. First I drove the LED of the optocoupler using 3.3 V PWM signal and measured it. The results were accurate.
Then I used a 200 kΩ resistor and a diode in reverse (for protection) to drive the optocoupler from mains 230V AC.
The microcontroller measured ~900 Hz. I reduced the resistor from 200k to 47k and the meter measured ~200 Hz,
The optocoupler has a 10k pull up.
Why does it behave this way? Is AC interfering with the measurements of the microcontroller?
Update: I used PWM on opto again and surprisingly it was not behaving the way it should and reading was messy. Used another (817) opto, it was fine but also got drunk after receiving a dose of 230 V AC