Hi,
it may make a difference what digital input pin is used. With a 2560 controller I observed the following:
In my project I needed to detect the zero crossings of a 230 V AC line. I used the following circuit:
With pin 21 I got a signal about 12ms high and 8 ms low.
With pin 30 I got a good signal with 10ms high and low.
I measured the signal with an oscilloscope. On pin 21 the signal was good when the 5V supply of the controller was switched off. But with 5V switched on the signal got worse as shown above.
I cannot explain the effect, but it is worth to know that digital inputs of a controller don't behave the same way.
If pin 21 means SCL then this pin may have a pullup resistor on board that can affect weak signals as in your case. The same effect may be found with pin 20 SDA. It has nothing to do with different pin behaviour but with externally added pullup resistors.
Why is the circuit not isolated unless you are an expert with mains voltage. "But with 5V switched on the signal got worse as shown above." Nothing shows on my system. What is your reference point for your voltage measurements and your controller. Posting a schematic, not a frizzy thing will help us help you. Be sure to show all connections especially the grounds. You stated "2560 controller". I found several things matching that description, are you talking an assembly, chip, adapter???? Post links to technical information on your hardware devices.
This circuit is a death trap - you should never be connecting directly to the mains live or neutral like this - either live or neutral wire can have lethal voltages on it and should be treated with equal respect.
Secondly most single resistors are not rated for mains voltages (remember during lightning strikes and other transient events you will see kilovolt level voltage spikes on the mains, leading to flashover on a low-voltage resistor, starting a fire and breaking the isolation barrier. Special high voltage resistors are used in these situations.
Thirdly Schottky diodes have excessive reverse leakage for high voltage use, so if the zener fails you'll risk thermal runaway of the Schottky and possible failure - again risking starting a fire.
Unless you understand the risks and regulations of circuitry directly connected to the mains, do not do it...
When you see mains equipment with things like a UL (Underwriters Laboratories) logo (*), it means the equipment has been thoroughly tested for many failure modes and doesn't pose a risk of electrocution or fire in those events.
Get a small (UL listed!) mains transformer and use the secondary voltage for your sensing - this is the safe and legal. Don't forget a fuse (the fourth error with that circuit BTW).
(*) cheap rubbish from China excepted - this stuff is often unsafe and to be avoided.
Oh dear, I did not expect so many responses. I just intended to give a hint to consider what pin to select for some purpose.
The response of Dr. Diettrich that pin 21 of a Mega2560 controller has an internal pullup resistor is the explanation for my observation. The pullup resistor obviously is active even when the pin is used as a normal input pin (pinMode(21,INPUT)), i.e. not as SCL.
I know the threat to work on 230V mains and I am aware of the possible danger.
If the Zener diode fails the MCU will be the weak spot, not the Schottky diode. With 330k resistor the internal protection diodes will be probably enough to protect the pin anyway.
It's likely because many (most) external modules have on board pull up resistors, for better or worse. Also, you would not want pull ups on an analog input A4, A5. I checked the UNO R3 schematic, it has no pull ups. The Mega has them, probably because it doesn't have to share I2C pins with ADC inputs.