High-Z triggers interrupts at exactly 50Hz ?

Hello,

I am curious about a behaviour I observed on my Arduino-like (I use a cheap copy of a Nano but I expect it would be the same with pretty much any Arduino)

I uploaded this code, and did not connect anything physical to the board

volatile int state = 0;

void setup() {
  pinMode(13, OUTPUT);
  attachInterrupt(0, blink, RISING);
  Serial.begin(9600);
}

void loop() {
  state=0;
  delay(1000);
  Serial.println(state);
}

void blink() {
  state++;
}

I would expect that the pin 2 is in high-impedance mode and should either fluctuate randomly or stay in any state, but actually the above program prints out "50" !

Why does my floating pin oscillate ? Is it because of the 50Hz mains ? I have plugged in my board to my computer using a mini-usb cable, I did not expect to have any noise from the mains...

50hz noise from mains - the pin is picking it up like an antenna.

You'll always pick up mains strongly in a building as you are surrounded by it and mains
wiring is not shielded.

You set the value of the variable "state" to zero, and then print out it's value 1 second later.
If the interrupt is being triggered by mains interference, then it will have incremented "state" to 50 in that time.

You could make the interrupt pin less sensitive to noise pick-up by connecting a resistor and/or capacitor between that input and ground. Also used screened cable to connect to your input.

You haven't told us what you want the interrupt to detect.

That code is not supposed to do anything, just curious about this behaviour...

Is the 50Hz perturbation coming from the PC (maybe the USB is not properly regulated), or is it coming from ambient electromagnetic fields created by the mains in the house ?

I can think of many applications such as detecting a power short out (I saw someone looking for a way to to just that here about a week ago)

mathers:
Is the 50Hz perturbation ... coming from ambient electromagnetic fields created by the mains in the house ?

Exactly.

mathers:
I can think of many applications such as detecting a power short out (I saw someone looking for a way to to just that here about a week ago)

Unreliable.

Wow ! I tested this today, and I can follow my mains through the wall with it ! In europe (220V 50Hz), with a simple jumper wire connected to port 2 as an antenna, it detects 50Hz up to 30cm of the wire...

Just like the ones you buy in the shops!