Digital inputs behave differently

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.

Which pin numbers do you refer to?

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.

Forget about that circuit, your whole controller can become live if connected the wrong way around.


arduino AC zero crossing detector

There are many hits on how to do it safely.
Here is a sample;

Tom... :smiley: :+1: :coffee: :australia:

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.

I think that stuff only has the CE label.


Which CE ???

Tom.. :smiley: :+1: :coffee: :australia:


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.

Not internal, the pullups are soldered on the Mega board to SCL and SDA. This applies to many other boards as well.


No you have to pinMode it to turn on the pullup resistor when you declare it an input.

pinMode(21, INPUT_PULLUP);

Tom... :smiley: :+1: :coffee: :australia:

The pullup resistors on SCL/SDA can not be deactivated in software, only by a hammer, soldering iron or the like.

Is it reallly so hard to understand that these pins have external pullups on the board? See the circuit diagrams for the various Arduino boards.

1 Like


Just measured the resistance on clone Nano, 5V to A4(SDA), 5V to A5(SCL), R > 20M Ohm.

Just measured the resistance on clone Uno, 5V to A4(SDA), 5V to A5(SCL), R > 20M Ohm.

No pullup on the PCB.

Tom.. :smiley: :+1: :coffee: :australia:

My Mega has 10k pullups on SCL and SDA.

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.

You are right, they would interfere with analog signals on an Uno. But as the controller of interest is a 2560 there is no such restriction.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.