Hi, welcome to another tutorial from Robojax. in this video we are going to
learn why we need to use a resistor with push button when we use it with Arduino.
when I press it now it says LOW when I release it that's HIGH. you can get the
code for this project by clicking at the link at the description below which will
take you to www.Robojax.com/learn/arduino
let's get started with this.
my name is Ahmad Shamshiri, presenting this tutorial from Canada.
this is an Arduino and you want to connect a push-button switch into pin 2
you can do it either this way that pin 2 is connected to a resistor to the ground
this processor can be anything greater than 300 ohm so you can put one kilo 10
kilos 20 kilos all of them will work fine so this is by default is ground and
when you push the button the 5 volts will be connected and this pin goes high
the second option will be the opposite the pin is connected HIGH (to 5V or 3.3V) whether it's a
greater than 300 ohm and when you push this button you you connect it to the
ground and you make it LOW. and as you can see the this pint 2 is connected
via resistor to 5 volts so this is pin to pin 2 is here connected and then from
pin 2 the same resistor is connected here where this wire to 5 volts. and if I
follow this from pin 2 there is a push button that goes to the ground from pin
2 this is pin 2 I have a push button which has these 2 pins. as you can see
these two pins one is connected here the other is connected here and the yellow
pin. the yellow is connected here to the ground of Arduino. so when I push this
button I'm connecting this pin 2 the white pin to the GND. when I release
it , the push button is released not pushed then this pin 2
connected via resistor to 5 volts. and here is the demonstration of that the
pin this is always HIGH. it's connected to 5 volts when this button is not
connected so this one shows high when I press this button you will see on the
screen that it says LOW. when I release it this is now connected
to HIGH so you can see HIGH . when I push it it goes LOW. now here is this
configuration that you see on the screen pin 2 is connected via resistor to the
ground so this is pin 2 the white wire is pin
2 so pin 2 is connected via this resistor as you can see via this
resistors to the GND. and then from pin 2 we have connected it to 5 volts
when it's pushed. so pin 2 here this is a push button and then this orange wire
goes to 5 volts. the pin 2 is always LOW(via resistor) when you push it it becomes HIGH. and
here is a demonstration of it as you can see it's LOW I have not pressed it but
when I press it so when I press it it goes HIGH. because I'm connecting this to
5 volts and when I press it, it goes high release it goes LOW. but when you want to
use a push-button switch to the ground you can eliminate the usage of
resistors and you can use input pull-up and internally Arduino will create this
virtual resistance between the pin that you are intended to the VCC or 5 volts
this way you are eliminating the usage of resistor. now let's have a look at the
code and the only difference between previous code and this one is that we
just put in put an underscore INPUT_PULLUP but if you put this INPUT_PULLUP the system
will create a virtual resistance here and it will be always connected to the
positive via this resistor. so when you put your push-button we are just
checking it to see if it's low so when you push it you are connecting this to
the ground and this pin will be low. so we check so we say that button is pushed
and otherwise button is not pushed. as you can see here this is now
connected from to to this push button and from the other side of pin 2 is
connected to the ground. and as you can see this button is not pushed and
now when I push it it says button is pushed and I'm holding it. release this
button is not pushed. and now the noise and those stuff that you were expecting
anything get closer to this wire it will not affect it and you can eliminate the
usage of resistor and I'm wondering why they created this PULL_UP and they did
not create the PULL_DOWN so we could have put the push button up. and in this
case we didn't even need a resistor at all
in both cases. now the question is what happens if you remove the resistor and
just put a push button here to connect it or maybe just put a push button to
connect it to the ground let's find out practically I'm going to show you now.
now this is the case that the push button is connected between pin 2 and 5
volts so from 5 volts I have one wire that orange and from pins 2 I have
another one exactly this configuration pin 2 is here and from 5 voltage here
and now let's see what happens. that is the codes as you can see. it just
goes HIGH and LOW automatically without me doing nothing. of course when I push
it it always stays high you see it never goes LOW. but when I release it now you
see this become LOW, HIGH and randomly it becomes LOW and HIGH.
you see even some noise simple thing with just when I touch this wire it
changes the input state. for that reason you need to have resistor. you see even if my hand gets
closer you see when my hand gets closer it changes due to capacitive noise.
now I'm showing you this scenario so when I use this push button. the push
button is connected between pin 2 and the ground so pin two is on one side
of this push button and ground this on the other side. or not it's LOW some
noise if I touch this wire as you can see it becomes HIGH and LOW. to make the
input immune against all these noise and make it stable we need to put resistors.
so it can work properly. now I have connected the resistor to 5 volts so by
default it's always HIGH. it never changes even if I touch or anything
because this resistor yes in the case that if you put for example 10 mega ohm
resistor it's possible that it not it might not have any effect it's like open
but anything around bigger than 300Ω, 10kΩ, 20kΩ or 100kΩ will
work fine. now I have connected back the pin 2 via resistor to ground and on
this side of push button is connected to 5 volts as you can see here by default
because it's connected by a resistor to a ground we are reading low low and when
I push it and becomes HIGH and never changes by any noise or anything and if
I release it it slow because of this resistor and no noise or anything can
change.
Code explained
now let me explain the code. I've set in side the setup I've set pin 2 as input other
input because you are using pin 2. and then we are setting the serial monitors
this is the monitor that you when you click and show serial monitor so this
will be the serial monitor with 9600 baud the value that you're using if this
one it must match of 9600 baud otherwise you will not be able to read it. and this
is just a text for our information. inside the loop we are reading the
digital read using digital read we are reading pin 2 and the result of the
reading will be stored in a variable called push-button of type integer so
now this is holding the HIGH or LOW the value that you read here. and we check if
push-button is equal equal this is comparison equal LOW then we say print
pin 2 LOW which is doing it now. because the other resistor is connected
to the ground and then we say else otherwise it prints: pin 2 is HIGH. now if I push
the button you see this will be HIGH. release that it will be LOW. and we are
just waiting 500 millisecond it continues like that. thank you for
watching this was why you need it to have a resistor with the push button
please thumb up 👍 the video also subscribe so you can get updates of my upcoming
videos.
Subtitle type and corrected by Ahmad Shamshiri on July 11, 2020 in Canada.
I did my part to help you learn. Now it's your turn.
You can subscribe to my channel, thumb up 👍 the video and say "thank you" in the comment section.
The link to my PayPal account is below this video if you want to send me chase donation. Thank you.