Unwanted Interrupt!!

Hi,

I have a simple sketch with interrupt 0 (in pin 2 with INPUT_PULLUP), and another INPUT_PULLUP in pin 5.

When I connect pin 2 to GND, an interrupt is generated and microseconds printed in Serial. Thats correct.

The problem comes when I connect pin 5 to GND: pin 5 value is ok and its printed in Serial, but an interrupt is generated too, and microseconds is printed in serial too!! just like the pin conected to GND were pin 2.

This is the code, please help! Thank you in advance..

String pzControl="000";
int pinInterrupt=2;
int pinP=5;
boolean statusChange = false;
volatile long previousPulse=0;
volatile long speedTime=0;
volatile boolean pulse = true;
volatile long T0 = 0;

void interrupt() {
if (micros() > T0 + 2000) {
long t = micros();
pulse = !pulse;
if (pulse) {
speedTime = t - previousPulse;
previousPulse = t;
Serial.println(speedTime); // only for debugging!!
}
}
T0 = micros();
}

void setup(){
pinMode(pinInterrupt, INPUT_PULLUP);
pinMode(pinP, INPUT_PULLUP);
Serial.begin(9600);
attachInterrupt(0, interrupt, CHANGE);
}

void loop() {

String str;
str = "T3_POK_";
statusChange = false;

if (digitalRead(pinP) == LOW) {
if (pzControl == "000") {
pzControl = "001";
statusChange = true;
}
}
else
pzControl = "000";

str += pzControl;

if(statusChange)
Serial.println(str);
}

NEVER print within an interrupt. Many things can and will go wrong. Fix that first.
Also, all time variables must be declared unsigned long.

What jremington said. Your interrupt routine should wiggle the values of some volatile variables, which your main loop inspects when it gets around to it.

hfxalvarez:
The problem comes when I connect pin 5 to GND: pin 5 value is ok and its printed in Serial, but an interrupt is generated too, and microseconds is printed in serial too!! just like the pin conected to GND were pin 2.

As others have said plus ...

Make a pencil drawing showing all your connections and post a photo of the drawing. NO Fritzing diagrams please.

...R

The pin 5 Input comes from the output contacts of a mechanical relay. When the 24V relay coil is on, pin 5 is connected to GND.

It seems like the relay coil is inducing a current in the wire connected to pin 2, and the interrupt occurs.

If I put a wire on pin 2, although is not connected to anything else, the interruption occurs when the relay is activated.

The relay has a 24V coil. The relay output contacts are connecting pin 5 to GND.

What can I do?

Thank you!

Do you have a flyback diode on the coil? Do you have a lot of capacitance on the 24V supply, especially near the relay? Can you slow down the relay switching signal?

Now is posted :slight_smile:

Are aware your interrupt code will be executed twice every time the relay is actuated? Once when the relay closes and once when it opens. Perhaps you want to interrupt when the level goes high, or goes low.

Paul

Images from Reply #7 so we don't have to download them. See Image Guide

...R

Thank you. The interrupt code is ok.

When relay of pin 5 is actuated, the interrupt in pin 2 is generated.
The realy coil is producing some kind of noise...
I suppose there is a easy solution by hardware electronics, but I dont know How!

KeithRB:
Do you have a flyback diode on the coil? Do you have a lot of capacitance on the 24V supply, especially near the relay? Can you slow down the relay switching signal?

I dont know, idont know, idont know... :confused:

At the very least you need a flyback diode (1N4001) across the 24 V relay coil.

hfxalvarez:
Thank you. The interrupt code is ok.

When relay of pin 5 is actuated, the interrupt in pin 2 is generated.
The realy coil is producing some kind of noise...
I suppose there is a easy solution by hardware electronics, but I dont know How!

When the relay is turned off, the interrupt is also generated because you have it set for "change". Is this what you want?

Paul

Why not tell us what you are really trying to do?

If you are trying to use relays to detect 24 volts, you can expect problems. I would use two resistors, or an optocoupler.

Ok, its very simple.

I have an inductive sensor connected to relay1 coil (relay1 is electromechanical relay). The relay1 output contacts are connected to arduino UNO pin 5, and GND. This sensor tells me if theres something closer than x mm.

I have another inductive sensor connected to relay2 Input (relay 2 is optocoupler SSR relay). The relay2 output contacts are connected to arduino UNO pin 2 (int 0), and GND. This sensor tells me the speed of a metalic pin attached on a wheel.

Pin 2 is handled through interrupt code, CHANGE, because i manage FALLING/RISING edge by my own, in the interrupt code.

When the wheel rotates, the inductive sensor activates relay2, generates the interrupt and i know the speed of the wheel. It works ok.

But when the other inductive sensor (located 3 meters further) activates relay1, pin 5 is LOW, correct because is INPUT_PULLUP, but it also geneates an interrupt in pin 2. (I dont know why).

Probably because relay1 coil noise..?? Relay1 coil is 24V, Relay2 input is 24V too.

My strength is programming, my weak point is the hardware and electronics, so i dont know what to do about diodes, etc..
Should I move the post to the part of electronics? Thank you.

optocoupler SSR relay

This "relay" might have problems if it is expecting to switch AC current and you're feeding it 5V DC.

I think both relays could do with some good pullups. The 10K INPUT_PULLUP is OK for little switches but for long wires or noisy relays, a proper pullup will hold the voltage much closer to 5V. Try 1K or even go as low as 100ohms.

You can also add capacitors near the Arduino pins to absorb any high-frequency noise. The capacitor value will depend on the pullup value. I have found 10nF is good when using just the internal pullups. This is kind of an unusual value - it's more common to see pF or μF = 1000 times bigger or smaller. A good "junk box" with a selection of different capacitors is invaluable for experimenting with this stuff.

And double-check the grounds. The schematic shows the grounds taking a big tour around the back of the Arduino. Keep the ground wires all terminated to one point on the Arduino and run them alongside the input wires to their respective relays.

I agree completely with MorganS +1, at least 1K pullups are needed. The mechanical relay contacts would have a minimum DC current rating ... it could be a few milliamps or more. Also, the SSR may have a minimum load rating, so 1K or lower here too.

This should at least resolve the "crosstalk" interference problem. Extra interrupts caused by relay contact bounce could be managed (debounced) in software if desired.

Example circuit as per reply5 and reply15:

Most any diode is OK, 1N4001 to 1N4007, etc.

Do you have access to an oscilloscope so you can visually see if there is any change in the pin 2 voltage?

Paul

SOLVED!! with the flyback diode.

I did not know that the use of a diode between the poles of the relay coil was necessary.

No diode: interference and unwanted interrupts on pin 2 when the relay is disconnected appear.
With the diode: everything is perfect, even with the INPUT_PULLUP resistance.

Thank you KeithRB !!

hfxalvarez:
SOLVED!! with the flyback diode.

I did not know that the use of a diode between the poles of the relay coil was necessary.

No diode: interference and unwanted interrupts on pin 2 when the relay is disconnected appear.
With the diode: everything is perfect, even with the INPUT_PULLUP resistance.

Thank you KeithRB !!

Its always essential to prevent/mange inductive kickback with any inductive load - the high voltages
will damage the insulation over time and cause failure as well as generating massive amounts of noise.

For DC coils diodes are commonly used, for AC coils RC snubber networks are typically used.

Inductive loads can generate massive voltages depending on inductance and and current levels, from
100's of volts to 10's of kV. An electromagnet stores energy in the magnetic field and if you interrupt
the current suddenly that energy has to go somewhere (usually destructively). Diodes and snubber
networks provide a path for the current to continue (but subside gradually), limiting the max power
level pushing back from the coil.