D2 calls an ISR at RISING edge.
The problem is that the interrupt gets false triggering from nearby AC switches and contactors. By the way, I am using no debounce for input at D2.
Shall be grateful, if anybody can come out with some effective solution.
If you're sure it's an electromagnetic interference (how you can say that?), you need a better shielding and/or filter. But you should at first investigate which side is affected by noise: the sensor itself, the 24V output line (how long is it?) or the Arduino input from optoinsulator?
Whenever the contractor (2-3 meter away) activates, the interrupt is triggered.
Whenever the power supply is switched off the interrupt gets triggered before switching off. Both the supplies are SMPS and housed in a common metallic console box.
The sensor is placed less than a meter but its cable is about 3 meter long.
Need some tips for that, please.
Both supplies have separate ground.
The sensor has 3 wires, so naturally the 24V ground is very close to the output of the sensor.
Similarly, the pin #3 of the opto-insulator is connected to the ground of 5V supply and the output from pin #4 is very close to pin #3.
If I disable the interrupt and digitalRead instead the pin D2 in the main loop then I don't get the interference. But in that case, other conditional processes within the main loop are affected.
Have you tested the so called false triggering by operating the nearby AC switches? If yes, then it is an EMI affect which must be minimized by proper shielding.
You need to determine if the INT0-pin (D2-pin) of NANO is responding to nearby electrical disturbances or the ripples of the proximity sensor.
I would suggest to perform the following experiment to verify that the INT0-pin of the MCU is still responding to the nearby electrical disturbances.
In this experiment, the MCU will be continuously generating 1-Hz signal at DPin-9 which you connect with a jumper with INT0-pin (D2-pin) of NANO.
The NANO is expected to be interrupted at 1-Hz and accordingly a message (Interrupt) will appear on Serial Monitor at 1-sec interval.
You randomly create disturbances by activating (ON/OFF) nearby electrical switches and check that the interrupt process is affected by viewing more messages within 1-sec period time slot on the Serial Monitor.
Keep checked the "Show timestamp" box of the Serial Monitor.
I agree with the OPs diagnosis, I have seen it many times. The OP stated contactors etc, that indicates a lot of probably 3 phase power to me. As suggested shielding will go a long way in resolving the problem but I would also recommend trying a low pass filter (RC) on D2 as mentioned. I always used a variation of the HC14 or an opamp with the RC in front of it. Also placing a cap across the LED would not hurt. This type of problem is extremely hard to solve unless the electronics is inside a grounded metal box (faraday cage).
I had hard time to minimize 6kV - 10kV firing spikes of the CNG Taxicab to operate the 89S52-based Taximeter. I had to place the MCU in a little sized-conductive box and then grounding it. It was a mess. Then I opened a Korean made Taximeter and was amazed to observe their skilled way of installing extra electronics to keep away the core electronics from the EMI disturbances of HV firing.
The longest are the servo cables.
Then is the sensor cable, which is a 3-wire cable about 3 meter long; ground wire (24V) is included.
Should I use 2-wire cable + a separate wire for ground and wrap it around the 2-wire cable?
So the servo drive is in the same box as the microcontroller...
Is the decoiler powered from the same box, or controlled from the same box?
Is the optocoupler near the nano or near the proximity sensor?
What quality is the 24V source? Does it feed other equipment (motors, solenoids)?
What is the 3rd wire for?