Cezembre
Posts: 2
Joined: Thu Sep 08, 2016 10:42 pm

wiringPi Interrupt counter issue

Thu Sep 08, 2016 11:30 pm

Hello

I face to an issue I can't understand nor solve when testing wiringPi library in c++ and my Rpi Zero.
I just want to test interrupts, using a small button and a counter.

Here are the results :
When I press the buton, the counter incremments by 1
When I release the buton, the counter doesn't incremments ... immediately, but one second later, it increase by 200 or 300 !

Why does it increase by 200 or 300 ? Is it du to deboucing problem ? How can I stop that ?

Here's my very simple c++ program :

Code: Select all

#include <iostream>
#include <wiringPi.h>

using namespace std;

#define BTN_WPI	6	// BCM25
int myCounter;

void myInterrupt(void) {
	myCounter++;
}

int main() {
	wiringPiSetup ();
	wiringPiISR (BTN_WPI, INT_EDGE_RISING, &myInterrupt);
	myCounter = 0;	
	while(1) {
		cout << myCounter << endl;
	};
	return 0;
}
And here's my wiring. I use a pull down resistor and a debounce capacitor :

Code: Select all

     __________________________ 3.3v
     |               |
     |               |
     |               ° /
    === 220uF         / btn
     |               |
     |_______________|_________ GPIO
			            |
                    [ ]
               10Ko [ ] 
                     |
                     |_________ GND
Thanks !

User avatar
rpdom
Posts: 12743
Joined: Sun May 06, 2012 5:17 am
Location: Ankh-Morpork

Re: wiringPi Interrupt counter issue

Fri Sep 09, 2016 6:37 am

That capacitor is way too big!

When the switch is closed it will discharge quickly (with a heavy load going through the switch). When the switch is opened it will slowly charge up through the 10K resistor. That will mean the voltage on the GPIO will slowly fall from 3.3V to 0V as it charges. This is not good for a digital input which is expecting either a low or high voltage. There is a band in the middle where very small voltage changes can trigger lots of interrupts, which is what is happening in your case.

Use a much smaller capacitor for the debounce. Something like 0.1uF or lower should be plenty.

Cezembre
Posts: 2
Joined: Thu Sep 08, 2016 10:42 pm

Re: wiringPi Interrupt counter issue

Tue Sep 13, 2016 11:03 pm

Thanks rpdom for your answer.

I reduced the capacitor to 1uF... but the problem persists.
(I have no lower capacitor in stock)

Then I did a new test : I just connected pin #10 to pin #5 with a simple 1ko resistor, in order to get interrupt on pin # 10 (input) triggered by the pin #5 (output). No switch, no capacitor, used for this test.

A got the strange following results :

Code: Select all

Start / State LOW ....
State HIGH
interrupt !
interrupt !
State LOW
Stop
Here's the code :

Code: Select all

#include <iostream>
#include <wiringPi.h>

using namespace std;

#define OUT_WPI	5	// BCM25
#define IN_WPI	10	// BCM8

unsigned int i;
unsigned int d = 64000;

void myInterrupt(void) {
	cout << "interrupt !"  << endl;
}

int main() {
	
	wiringPiSetup ();
	
	pinMode(OUT_WPI, OUTPUT);
	digitalWrite(OUT_WPI, LOW);
	wiringPiISR (IN_WPI, INT_EDGE_RISING, &myInterrupt);
	
	
	cout << "Start / State LOW ...." << endl;
	for(i=0; i<d; i++) { // waiting 
	}
	
	cout << "State HIGH" << endl;
	digitalWrite(OUT_WPI, HIGH);
	
	for(i=0; i<d; i++) { // waiting 
	}
	
	cout << "State LOW" << endl;
	digitalWrite(OUT_WPI, LOW);
	
	for(i=0; i<d; i++) { // waiting 
	}
	cout << "Stop" << endl;
	
}
Why did I get 2 interrupts on pin #10 when pin #5 risen from LOW to HIGH ?

Thanks

User avatar
davidcoton
Posts: 3125
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK

Re: wiringPi Interrupt counter issue

Tue Sep 13, 2016 11:24 pm

See paulv's work in this thread
(long but worth it) and this specifically for Python.
"Thanks for saving my life." See https://www.raspberrypi.org/forums/viewtopic.php?p=1327656#p1327656
“Raspberry Pi is a trademark of the Raspberry Pi Foundation”

Return to “Troubleshooting”

Who is online

Users browsing this forum: 5ft24dave, Bing [Bot], murksiuke and 50 guests