Dear friends welcome to another tutorial!
This is Nick and today we are going to see the easiest way to connect a push button to Arduino.
If you are interested, keep watching!
In previous videos we learned how to connect a button to Arduino and how to debounce it.
You can check them out now before watching this video.
Then, a couple of viewers commented that there is an easier way to connect
the push button to Arduino.
I tried it and it works!
That's the power of our community.
It's a kind of collective knowledge and wisdom.
Thank you guys! Let's see.
Every digital pin of Arduino has an internal pull up resistor.
The value of that resistor is between 20-50KΩ
depending on the microcontroller chip.
Imagine this as a resistor which is connected to 5V.
We can enable that resistor by software.
So, if we enable that internal pin resistor and connect a LED to that pin,
the LED will dimly light as it is now connected to 5V via a 50KΩ resistor.
So, the idea is that we can exploit this internal pin resistor
in order to get rid of the external resistor we were using so far
in order to connect a button to Arduino.
So, the new easy connection of the button to Arduino is this:
One leg of the button goes to Arduino GND
and the other goes to any digital pin we like!
Now, if we run the simple program that lights up the LED each time we press the button,
we can see that it works fine!
Easier, isn't it?
We don't use a resistor any more and the connection can't be easier.
Now, let's see the code of the project.
As you can see, the code of the project is a modified version of the button sketch of Arduino examples.
In order to enable the internal pull up resistor, we have to declare the pin that the button is connected to
as INPUT and then write HIGH to it, even though it is declared as INPUT.
That way, we can enable the internal pull up resistor of the pin.
If we now run the program, we are going to notice a strange behaviour.
The LED stays on if the button is not pressed and goes off when the button is pressed.
That's the opposite of what we want, isn't it?
The reason for that is that our circuit now works as ACTIVE_LOW, because when the button is not pressed,
it is connected to 5V and when it is pressed it is connected to Ground.
All we need to do is to make a small change to the code and we are ready.
We define the state of the button as ACTIVATED when it is LOW which means that it is connected to Ground
and that happens when the button is pressed.
Then we change this line here.
If the button is ACTIVATED we light up the LED.
That's it. As you can see, the program is working now as expected.
As you can see, with a few changes in the code, we can connect a button to Arduino much easier.
I am going to follow this procedure from now on when I plan to use a button in a project,
because it is easier and of course it uses less parts!
If this is your first time here, I would love to have you subscribed.
In this channel i post videos about DIY projects every Saturday.
I build projects with Arduino, Raspberry Pi, i build robots and simple electronics circuits.
I hope you will join our community.
Until next Saturday, Watch, Learn, Build!
