Go Down

### Topic: Button - hardware debouncing with 1uF capacitor (Arduino Uno R3) (Read 1 time)previous topic - next topic

#### Aranha

##### Dec 27, 2013, 06:32 pmLast Edit: Dec 27, 2013, 10:42 pm by Aranha Reason: 1
Hello !

I hope to have a lot of fun and learn many things with my Arduino Uno I got a few days ago. Before I start, I'd like to notice that I looked for the solution, there are many tutorials and short explanations about how to handle button bounces on hardware level. Sadly, I did not find anything that could be wrong so I decided to create a new topic (though I know this must be some stupid mistake of mine).

What I wanna do is a circuit with 2 LEDs (and a resistor for each of course) and a button which, pressed, will turn one (shining) LED off and the other - on. To do that I connected a resistor to ATmega's pin, a + leg of LED to that resistor and the - to the ground. The same for the second LED. The button is connected to a pin using a pull-up resistor (I'll show the code later) so I only have to take care of two legs of that button. The other leg goes to the ground.

When I test it without the capacitor - there are bounces but generally it works fine (what I means - it switches between these two LEDs). When I insert the 1uF capacitor in parallel with two legs of the button and press the button, both LEDs turn on. When I let it go, the first one always turns off so the second one is the only shining.

I have no idea what's wrong, I hope someone will help me solving this.

Code: [Select]

bool which=false;
int ledPin1=2;
int ledPin2=3;
int btnPin=4;
int btnState=LOW;

void setup()
{
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(btnPin, INPUT_PULLUP);
}

void loop()
{

if(btnState==LOW)
{
if(which)
which=false;
else
which=true;
}

if(which)
{
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin1, LOW);
}
else
{
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin1, HIGH);
}
}

Greetings,
Daniel

#### polymorph

#1
##### Dec 27, 2013, 08:58 pm
Look in the Examples sketches of the Arduino IDE for "Debouncing".
Steve Greenfield AE7HD
Drawing Schematics: tinyurl.com/23mo9pf - tinyurl.com/o97ysyx - tinyurl.com/q7uqnvn

#### Aranha

#2
##### Dec 27, 2013, 09:15 pm
Hm, I did not find any solution there. The only one is for software which is not what I need. I want to solve this problem adding the capacitor, not writing code. Writing code is what I do everyday.

#### polymorph

#3
##### Dec 27, 2013, 10:06 pm

1uF is kind of large for a debouncing capacitor. What value is the pullup resistor?
Steve Greenfield AE7HD
Drawing Schematics: tinyurl.com/23mo9pf - tinyurl.com/o97ysyx - tinyurl.com/q7uqnvn

#### SirNickity

#4
##### Dec 27, 2013, 10:17 pm
Three suggestions:

Don't use a 1uF cap.  If you must do hardware debouncing, use something like a 100nF ceramic... but really you're probably better off going with software.  (Take that from someone who used to be dead-set on using HW debouncing.)

Next, you'll get better visibility if you upload images straight to the forum instead of using an image host.  A lot of those sites are firewalled where I work, so I won't even see them.  Others will probably decline to follow the link.

Finally, use code tags for your code samples, not quote tags.  The main difference is that code tags prevent the forum software from accidentally parsing valid code statements as formatting instructions, which is not the case with quote alone.

#### Aranha

#5
##### Dec 27, 2013, 10:56 pmLast Edit: Dec 27, 2013, 11:26 pm by Aranha Reason: 1
I found 330nF capacitor, that's the best I have... When I insert it, bounces are still there (but works as it should be - it sometimes switches the LEDs). In a few days I'll get 100nF, as you suggest, and try it.

I study computer science and I have really have a lot of programming there but noone will teach us how to create even simple circuits or explain how the basic elements work. I just try to do it on my own because it is what I'm interested in. I can sit and try to write a program whenever I want because I was taught how to do it but I can't sit and just build a circut like that. I wanna simply change it .

I attach another picture of what I did (I know this is not actual schematic, sorry for that). If everything but the value of capacitor is good, I'll just get 100nF as soon as I can and try it.

@polymorph
I'm sorry, I forgot to answer your question. I use the internal pull-up resistor, which, following the datasheet, is 20k.

I attached the photo to my post and changed quote tags to code tags, thanks for that.

#### SirNickity

#6
##### Dec 28, 2013, 12:13 am
The difference between 100nF and 330nF isn't going to be a deal-breaker.  The gist of the problem with a 1uF cap is that it's overkill.  It stores more energy, which you short to ground when you press the switch, subjecting the contacts to avoidable stress.  Instead of, or in addition to a smaller cap, you might want to put a 100R resistor between the cap and the switch.  (Cap + to input pin, cap - to ground, cap + also through 100R resistor to switch +, switch - to ground.)  This helps limit current surges when the button is pressed.

BTW, the cap in your image there is a polarized electrolytic, and you have it backwards.  The stripe is usually the negative side.

One more tip:  Keep an eye on your image resolution.  If the width is > 1000 pixels, it means most users will need to scroll around to see it all, which is really distracting when tracing parts of a diagram.  Try to keep it under 1000 unless you need the resolution to convey detail.

#### polymorph

#7
##### Dec 28, 2013, 06:14 am
Perhaps the button is not making good contact.

In the Arduino IDE, look up Examples, Bounce, and open the sketch Change.
Steve Greenfield AE7HD
Drawing Schematics: tinyurl.com/23mo9pf - tinyurl.com/o97ysyx - tinyurl.com/q7uqnvn

Go Up

Please enter a valid email to subscribe