How to Use Digital INPUT and INPUT_PULLUP on Arduino

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video i'll show you how to build a circuit and read digital values from that circuit using pull up and pull down resistors for this example you'll need an arduino a breadboard two 10k resistors two push buttons solid core jumpers and stranded jumpers let's start by connecting 5 volts and ground to the power bust rails then place a button over the gap of the breadboard connect a jumper from 5 volts to one of the leads of the button then use a 10k resistor to connect the other side of the push button to ground use a jumper to connect pin 2 to the resistor wired in this way the resistor is called a pull down resistor since when the button is not being pushed the voltage is being pulled down to ground by the resistor now let's add a second push button connect another 10k resistor from 5 volts to the push button then ground the other side of the button connect a jumper from pin 3 to the resistor in this case the resistor is called a pull up resistor because the resistor is pulling up pin 3 to 5 volts so let's connect the arduino and read the values from pin 2 and 3 with some code start by opening up a new sketch i'm going to go ahead and save this you'll notice that it creates a folder with the same name in every arduino sketch you need something called void setup and void loop in the setup section any code that is written in here will be executed one time in the loop section the code written here will be executed over and over again above the setup i'm going to define some variables i'm going to define a variable called button1 and set it equal to 2 using pound define i'm going to do the same thing with a variable called button2 for pin 3. this is the same thing as saying int button1 equals 2. let's move down to the setup in order to view values in the serial monitor i need a command called serial.begin 9600 this initiates serial communication 9600 is what's called the bowed rate i'm then going to use the command pin mode to define button 1 and button 2 as inputs now we're going to go down to the loop and create a new variable with an integer data type set this equal to the value that pin 2 is reading i do this using the digital read command i do the same thing with the other pin now i'm going to print to the serial monitor i'm going to start with a string in quotes that says button1 state using the serial.print command then i'll use that same command to print the value of button1 state i'll follow that with a slash t to create a tab then i'll repeat the print statements with the second button i use the command serial.println for the last statement in order to go to the next line now let's upload you can see that when i click over here and open up the serial monitor pin 2 is reading 0 or low and pin 3 is reading 1 or high if i press the first button the state switches to high if i press the second button the state switches to low now let's explore the circuit with the pull up resistor i'm going to remove the resistor and then open up the serial monitor again you can see that the value of pin 3 is changing without having to push the button i can even change it by just touching the metal on the jumper this is pretty clear if you look at the circuit diagram without the pull up resistor there's nothing pulling pin 3 up to 5 volts but arduino actually has a built-in pull-up resistor you can activate it by changing the pin mode from input to input pull-up i'm going to upload again now you can see that the circuit is behaving exactly as it was before thanks for watching and if you like my video subscribe to stay up to date as i release more introductory arduino videos
Info
Channel: The Bored Robot
Views: 6,587
Rating: 4.8657718 out of 5
Keywords: Arduino, Digital, Pull Up Resistor, Pull Down Resistor, Pull-up Resistor, Pull-down Resistor, digitalRead(), Serial.print(), Serial.println(), INPUT, INPUT_PULLUP
Id: WcPU-7UnDWw
Channel Id: undefined
Length: 3min 53sec (233 seconds)
Published: Tue Aug 25 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.