Interference from power outlet with pulse counting

Hi.

First post. Getting familiar with Arduino Uno r3.

I have set up a photodiode to PIN2 and the goal is to count the pulses from a energymeter using an interupt. I have managed to get it working quite fine, until I noticed there are some wrong pulse counts. It seems that when my Arduino is attached to my laptop and I connect or disconnect the charger, it causes interference to the digital pin. There are a lot of interupts, ie. falling pulses. The same occurs even if there's an external power supply connected. And also if the laptop charger/external power is connected to a power outlet, and I switch the lights on and off, it causes pulse counts.

I am using the internal pull-up resistor, but could the problem be that it's not strong enough? I dont know what's the actual value of the resistor in Uno R3, 20kohm I guess... If I dont use the pullup, it causes pulses randomly. Any suggestions on the value of the pullup?

Is this just some kind of feature or am I missing something?

attachInterrupt(0, onPulse, FALLING);
digitalWrite(2,HIGH);
...
void onPulse()
{
Serial.println("Pulse caught");
count++;
}

Cheers.

The internal pull-up resistor is too high.
An external pull-up resistor is mostly set to 10k.
For a noise environment use 4k7.

You use a "Serial.println()" in the interrupt routine. You should not do that, it might have unpredictable results.
Increment a counter in the interrupt routine, and use that counter somewhere in the loop() function.

Ok, thanks for the advice. I'll try an external pull-up resistor instead.

The serial.println was just for testing purposes, but did not know you cant put serial outputs into interupt routine.

Okay, I tried external pull-up resistor instead of the internal one. Pullup seemed to get stronger with 4,7kohm resistor, since the blinking led did not generate an interupt anymore. Though still if plug and unplug the laptop, connect to Arduino, to a charger, it causes interrupts. So not matter if the pullup is very strong the voltage from the usb-port 'ripples' causing 5V to go up and down.

The weirdest thing still is that even turning the lights on and off, causes interupts. If the laptops charger is connected to a power outlet in the same room. If it's running on battery, obviously theres no interupts.

..this is not a showstopper, the pulsecounter will be connected into a power-outlet all the time. Just a bit suprised that this is so unstable... Perhaps, I'm just doing something wrong, if no one else has encountered this kind of behaviour.

I happened to me a number of times. But it is hard te tell from a distance what the cause it, and how to solve it.

Adding extra capacitors next to the voltage regulator is something worth to try.

Also adding ferrite coils for the power supply could help.

Sometimes only connecting the ground would cause pulse on an input. I would use an ferrite coil or 10 ohm between the grounds.

Sometimes a bad capacitor can not handle a spike. I have replaced new capacitors with 20 year old capacitors to make it work. (I did hand-pick the old capacitors for the least leak current).

A small capacitor of 1nF at the input to ground helps a lot with spikes.

The Arduino is not build for an unfriendly environment. So the goal is to isolate the Arduino from nasty things.