5
\$\begingroup\$

I have to write an Arduino sketch to accomplish some tasks with the number of digital pulses detected by a pin. For this purpose I chose to use interrupts in order not to miss any pulse and perform the rest of the algorithm virtually at the same time.

In order to make a quick test, I put on this circuit:

enter image description here

and I loaded this sketch (based on the example in the attachInterrupt documentation):

volatile unsigned int pulses=0;

void setup() {
  attachInterrupt(0, countpulses, FALLING); // interrupt 0 = pin 2      
  Serial.begin(9600);
}

void loop() {
  Serial.println(pulses);
  delay(500);
}

void countpulses() {
  pulses++;
}

What I expected here is that whenever I press and release the switch (and the red led turns on and off), it should read one pulse. The weird thing is that it counts two pulses instead, that is, it counts a pulse for each state change instead of just for the "falling" one.

Is this a matter of code or it's due to the switch? Will I have the same issue handling direct digital signals on pin 2?

\$\endgroup\$
1

3 Answers 3

5
\$\begingroup\$

Mechanical switches experience "contact bounce", which means that one press (or release) of the switch might cause the electical contact to make/break contact several times in rapid succession. This is what you are seeing with your code as it stands currently. The Arduino is fast enough to respond to each of the interrupts caused by this bouncing, even though you can't see it on the LED.

\$\endgroup\$
7
\$\begingroup\$

Like Jon says the LEDs don't have current limiting resistors; the red one is connected directly between +5 V and ground, and the blue one between the I/O pin and ground. I'm a bit puzzled by the tact switch. This type of switches normally has the two left pins connected, and the two right pin.

enter image description here

When you push the switch left and right are connected. (This way of internal connections simplifies wiring up a matric of switches.) Here you can read how to wire a switch to a microcontroller's input.

If you do that you're shorting the red LED, so that will go out. But, and worse, you'll also short-circuit the power supply: the switch will connect the +5 V on the left side with ground on the right.

Please fix those issues first and report back.

\$\endgroup\$
4
\$\begingroup\$

A couple comments before getting to what could be the real issue:

  • Are you using a DPST button there? (double pole, single throw) If I recall, most of the buttons that I've seen of that type have two leads shorted on one side which could be a really bad thing if that's true in your circuit (power and ground shorted together).
  • You don't have any current limiting resistors for the LEDs (the arduino may have one built in but there certainly isn't one on the breadboard)

Assuming the above are non-issues, you are probably experiencing switch bouncing

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.