I have a pin (34) setup as an input. This pin is connected to a triac opto that generates a clean signal. I have it setup to generate an interrupt on falling edge. This is so I can detect zero crossings that will be used to fire an output triac. The problem I am having is that with each pulse the interrupt is firing MANY times on both rising and falling edges. It is acting like it is seeing bounces from a mechanical switch not from a clean sign wave signal.
Here is some of the code:
#define ZX_PIN 34
#define TRIAC1 26
void zero_cross()
{
// just toggle the pin for now
digitalWrite(TRIAC1,!digitalRead(TRIAC1);
}
void setup()
{
pinMode(ZX_PIN,INPUT);
pinMode(TRIAC1,OUTPUT);
digitalWrite(TRIAC1,LOW);
attachInterrupt(digitalPinToInterrupt(ZX_PIN),zero_cross,FALLING);
}
void loop()
{
}
Pin change interrupt multiple fire
- jgustavoam
- Posts: 165
- Joined: Thu Feb 01, 2018 2:43 pm
- Location: Belo Horizonte , Brazil
- Contact:
Re: Pin change interrupt multiple fire
Hi,
Please specify wich pulses are in the Scope screen .
Are Upper pulses from output of opto coupler ?
And lower pulses ?
Please specify wich pulses are in the Scope screen .
Are Upper pulses from output of opto coupler ?
And lower pulses ?
Retired IBM Brasil
Electronic hobbyist since 1976.
Electronic hobbyist since 1976.
Re: Pin change interrupt multiple fire
I had a similar problem on the ESP32. (If you really want a clean signal I think that you will need an external Schmitt trigger.)
I solved it differently with a timer as follows:
I solved it differently with a timer as follows:
Code: Select all
static uint32_t prevT = 0;
void IRAM_ATTR IOhandleInterrupt()
{
uint32_t t = micros();
uint32_t dt = t -prevT;
if (dt<150) return;
prevT = t;
..... your code
}
Re: Pin change interrupt multiple fire
NOTE the green wave in your 2nd scope picture.
In each tower it is switching on and off at half the tower height.
Not having your schematic, this leads me to think you are not at true GND with ref. to zero crossing or there is a wrong scope setting.
NOTE the spikes on both the leading and trailing edges of the pulses.
This looks like your load has both Capacitive and Inductive features to it.
OttoES suggestion of an external Schmitt trigger is probably a good idea however if your Zero Crossing is not true this may not work by itself.
A simple software work around is easy;
- In the ISR
This simple way stops the ISR from bouncing and waits a safe time to re-attach.
You only need to decide the time length to detach the ISR.
You probably need a time just wider than a normal pulse.
For the time shots; Remember Arduino can read milli seconds as well as micro seconds according to your circuit needs.
== Hope it Helps ==
In each tower it is switching on and off at half the tower height.
Not having your schematic, this leads me to think you are not at true GND with ref. to zero crossing or there is a wrong scope setting.
NOTE the spikes on both the leading and trailing edges of the pulses.
This looks like your load has both Capacitive and Inductive features to it.
OttoES suggestion of an external Schmitt trigger is probably a good idea however if your Zero Crossing is not true this may not work by itself.
A simple software work around is easy;
- In the ISR
- 1 Detach the ISR
2 Take a time snapshot ISR_var = millis();
3 Store ISR_FIRED = true;
4 Process the rest of the isr
Code: Select all
if( ISR_FIRED )
{
if( ( millis() - ISR_var) >= SomePreSerTimeVar )
{
re-attach irq
ISR_FIRED = false;
}
}
You only need to decide the time length to detach the ISR.
You probably need a time just wider than a normal pulse.
For the time shots; Remember Arduino can read milli seconds as well as micro seconds according to your circuit needs.
== Hope it Helps ==
Who is online
Users browsing this forum: No registered users and 33 guests