Interrupts problems due to noise

Hi!

I have the following setup (sorry for noobish picture):

http://shrani.si/f/g/12I/kkTrrGw/image.png

Since the bell ringing machine’s relay “ON” time is minimally 100ms, I had to go through Arduino Uno (where solenoid can be turned on for the needed 40ms). I am using the following code:

void setup()
{
	pinMode(hammerPin, OUTPUT); // Initialise pin as output.
        pinMode(2, INPUT_PULLUP); // Interrupt pin for striking.
        attachInterrupt(0, initiateStrike, FALLING);
}

void loop()
{
        if (strikeHammer == 1)
        {
	         hammerStrike( hammerPin, hammerPowerTime );
                 strikeHammer = 0;
        }
       ... some other code not relevant to this...
}

void initiateStrike()
{
        static unsigned long last_interrupt_time = 0;
        unsigned long interrupt_time = millis();
        // If interrupts come faster than 50ms, assume it's a bounce and ignore
        if (interrupt_time - last_interrupt_time > 50)
        {
                int reading = digitalRead(hammerPin);
                strikeHammer = 1;
        }
        last_interrupt_time = interrupt_time;
}

void hammerStrike( int pin, unsigned long delayTime )
{
	digitalWrite( pin, HIGH );
	sleepMicros( delayTime );
	digitalWrite( pin, LOW );
}

void sleepMicros( unsigned long t )
{
	unsigned long oldTime = micros();
	while (( micros() - oldTime ) < t ) {}
}

My problem that literally ANYTHING makes Arduino strike the solenoid:

  • if one of the other relays on the ringing maching activates
  • if i turn on or off the light connected to the same outlet
  • pulling the 3V adaptor out of outlet or putting it back in
  • etc

If I connect the interrupt pin manually (for test) directly to ground it activates when I pull it out too, which it shouldnt since its set to FALLING not to CHANGE. Im assuming its hapenning due to bouncing and noise, but shouldnt 50ms debounce time be enough? I cant really go too high with this since it will become too unresponsive. I tried adding capacitors (480uF then also 3x 100nF) with no luck either on one side of optocouplers or the other. All it did is that solenoid didnt strike when its relay activated, but it did strike when one of the reasons above happened. Does anybody have an idea what could be going on? Thanks!

I have traced the problem to the Arduino side, it is happening even if I disconnect interrupt pin from optocoupler. Internal pullup must have problems…

Unlikely. We need your schematic.
Do you:
Have lots of bypassing?
Seperated the power for the arduino and the relays?
Put diodes on the relays?
Tried opto-isolating the relays?

KeithRB:
Unlikely. We need your schematic.
Do you:
Have lots of bypassing?
Seperated the power for the arduino and the relays?
Put diodes on the relays?
Tried opto-isolating the relays?

You were right, completely disconnecting cable from pin 2 stops these interrupts caused from lights etc.

Sorry, im a noob, what do you mean by "bypassing"?

I have separate adaptor for each of them, but they share the same mains outlet.

On the relays directly no, no diodes, since I cant do much within the machine http://shrani.si/f/10/142/FnimS4J/2014-03-31-205949.jpg http://shrani.si/f/3Y/vn/2BwZzEJ1/2014-03-31-210002.jpg (please ignore that GND on one of the pins), these relays were made to turn on 220V big solenoids (with diode bridge in between). I was hoping that the diode I have put before optocoupler would help. I also have 2 additional ordinary brushed motors connected to the relay and the 3V power supply, but putting diodes across them didnt help.

Im afraid that for now I cant opto-isolate the relays without disassembling the machine, but in later stage I might replace the relays with optocouplers (modify the machine).

I must also say that between relay and optocoupler there is 2m of wire.

6 volts to the external jack on the Arduino is too low, use 7 or 9 volts.

You do appear to have diodes and are optoisolating, which is good. You need lots and lots of capacitors on the arduino side. That is bypassing.

Could inversing the logic help? Because now HIGH is off and LOW is on, which I am currently using because of internal pullups?

Terraviper-5:
I have traced the problem to the Arduino side, it is happening even if I disconnect interrupt pin from optocoupler. Internal pullup must have problems...

The output of that opto coupler is more like an analog signal than a logic signal.

You need to first find the best value of pullup resistor to make it high voltage when
off and low voltage when on, then you should pass the signal through a 74HC14
or similar schmitt-trigger gate with hysteresis so that it becomes a fast
logic signal with high noise-immunity. The built-in pull-ups are rather high values,
try something like 2k2 instead.

Logic signals are required to switch very fast. In a fraction of a microsecond.
The photo-transistor in a standard opto coupler doesn't switch fast enough to
be reliable without a schmitt-trigger afterwards.

There are opto couplers with built-in logic-conditioning circuitry that would
do the right thing, though they are a bit more expensive.

Thanks!