(upbeat music) - [Tutor] Hello, how's it going? In today's tutorial, we're gonna talk about
Floating Pins with Arduino. Now, usually you might think, hey, if something's floating,
that's like a good thing. Your ship crashes, you're
floating in the ocean and hey, you're floating that's
better than sinking, right? Well, with electronics
that's not the case. Think about hairdryer in the tub, that doesn't necessarily mix. So, the bottom line is
floating pins are bad. What this tutorial is gonna
seek to answer is, well, why the heck are they bad? What really is a floating pin? What's the deal there? Let's go ahead and set up a
reference for this conversation. Sometimes if you've got your
Arduino and you wanna read some type of digital input, okay. So for example, let's say
you have this push button and you wanna tell whether
or not the push button is being pressed down, or
it's not being pressed. So it's either on or it's off, there's like two States to it, right? Or maybe you have some
other type of sensor that gives you again like a
binary answer either on or off. First, maybe you've got
something like water detector and either water is present
or it's not present. And so you have these sensors or a button, whatever hooked up to a
digital pin on your Arduino and then you're simply reading the voltage from that pin using the
digital read function and you're trying to find out is the pin high or is the pin low? So let's use a simple push button circuit to figure out what this whole
floating business is about. So I've got my Arduino board here and it's hooked up to a breadboard and I've got a push
button on that breadboard. One side of the push
button goes to ground, and the other side of the push button goes to digital pin two and digital pin two is where I'm gonna sample
the voltage I'll use. I'll be using the digital read function to determine the voltage there. So you can see what the setup, when I press the button, digital pin two will quote unquote, "see ground voltage "It will see low voltage." And then that will return a low. So in my program, I'll use an if statement and I'll say, if digital Pin 2 is low, then go ahead and do whatever. Maybe we'll turn on an LED. Well, the question becomes
what is happening at Pin 2, when I'm not pressing the button and that's kind of the key here. So I'm just curious what you think, what is happening at Pin 2, when I'm not pressing the button? Well, why don't we just go look, let's just check it out for ourselves. So let's go ahead and
go to the Arduino IDE and mess around a little bit. So here we are in the Arduino IDE, and I'm gonna go ahead
and go to File, Examples, Basics, BareMinimum. And I'm just gonna set up a simple sketch. So the first thing I'm
gonna do is use a pin that's going to designate Pin
2, that will be the Input Pin. Now I'm gonna go to setup, I'm gonna set the mode of
that Pin Input to an input just like we talked about before. And then I'm gonna start
serial communication so I can look at information
coming over the serial monitor. And by the way if some of
the stuff isn't making sense, just go ahead and check
out my other videos. I got a ton of other videos
on this type of stuff. Okay, a little bit of housekeeping. All right, so now let's
go down to the loop and the first thing I wanna do is I wanna do the
digital read of that Pin. So remember, that's kind
of the premise of this. I wanna determine what the
voltage is at that digital pin. So what I'm gonna do is I'm gonna declare and initialize a variable, and I'm gonna set that
variable equal to the output of the digital read function. Let's do that. Okay, so I've declared and
initialized sensor value, and that variable is
going to hold the output of the digital read function, and digital read is gonna
return either high or low. And that's gonna look like one or zero, so one is high and zero is low. Now I wanna look at that value. So in order to see that value, I'm going to use this
Serial Write function and we'll display it on
the serial monitor window. Okay, there we are. I might've said write,
I meant to say print. So Serial print and I'm
actually using print line, and what that does is, well, you'll see it when we look at the Serial monitor. It allows a carriage return at the end of every value return. So let's go ahead and verify
that and let's upload it. And then let's look at the Serial monitor. Okay, now look at that Serial monitor, notice what's going on
here, there's ones and zeros ones and zeros ones and zeros. It looks pretty random. I'm just going to go ahead
and turn off auto scroll so we can see this. You can tell there's really no... Maybe there's some
random sequence to this, but for the most part,
it looks just like noise. So I feel like this answers the question. What is going on at Pin number 2 when the button is not being pressed. And the answer is, well,
we really have no clue. It's who knows? It's just floating out there. We've got no idea what
this pin's going to do. So, is this a problem or does it matter? Well, yes, it does matter. And why does it matter? Well, let's go ahead and write our program with the circuit set up this way and see how our LED performs. So I'm gonna go ahead and
close the Serial monitor window and let's add a little more code. I'm gonna add an if statement. Okay, so I added an if/else statement. Now this, if/else statement is saying, if sensor value is equal to low, then do a digitalWrite Pin 13 high. So pin 13, it's got a built
in led on most Arduino boards. If you're using the
Arduino UNO, like I am, then you'll definitely
have a built in led there. So I'm saying if Sensor_Value is low, I want you to write the pin high. And that might seem a
little counterintuitive, so just think back to the circuit. We know when we're pressing the button, Pin 2 is then connected to ground. So Pin 2 will be reading zero volts when we're pressing the
button and zero volts is low. So we're pressing the button Sensor_Value is going to be assigned to
the output of digitalRead. If we're pressing the button, then digitalRead would be equal to zero, so Sensor_Values zero. And then what we're gonna
do is we're going to say, if his Sensor_Value zero, is it low? If it is, go ahead and turn the LED on, do a digital right to Pin 13 high. And then if it's anything
else, then turn it low. So what we're supposed to be doing here is when I'm pressing the
button the light comes on, when I'm not pressing the
button the light goes off. So let's go ahead and see how this works with our current setup. Now, before we upload it, we need to set digital
Pin 13 as an output. So let's do that. Okay, and then I'll upload it. Now I've uploaded it to my board. And as I look at my
Arduino, the LED a pin 13, it's kind of like pulsating. It looks like it's rapidly blinking. Okay, so first off what's weird is it's on in the first place even
if it's rapidly pulsating, that shouldn't be happening 'cause I'm not even touching the button. Now, when I touch the
button, it stops pulsating and the led comes on a little
brighter and then I let it go and it starts pulsating again. So what is the deal here? Well, why don't we go ahead and look at the serial monitor again? Okay well, the serial monitor
is doing the same crazy thing. See, we're getting all
these ones and zeros, each kind of random
spattering of ones and zeros. So the problem is with the floating pin, is that regardless of what
I'm doing at the push button, there's noise interfering with that Pin and that in determinant value is giving false positives
for my button press. I'm not pressing the button, we are still getting a
low reading at that Pin and that's not good, that's not right. So how do we solve this? Well, what we use in this
instance is a pull up resistor. A pull up resistor is going
to tie that floating Pin to a known voltage a known state. And in this case, we're
gonna tie it to five volts. So let's look at this
new breadboard layout using an external pull up her resistor. So now I've got in addition
to the right side of the push button being attached to Pin 2. Now I've got to pull up resistor
that's going to five volts. Now it's just a resistor
like any other resistor, it's just pull up 'cause
it's the way we're using it. And what we're doing is we're pulling that Pin to a specific state. And in this case it's going to be high because we've connected it to five volts. So let's ask our question again, what is the value at Pin 2 when we are doing a digitalRead and the button is not being pressed. Well, now we can see that
the pin's gonna be reading five volts because it's
connected to five volts through that 10K resistor. We know the answer to our question. So let's go ahead and go
back to the Arduino IDE and check if that makes
sense on the serial monitor. Alright, so let's go ahead
and go to the serial monitor and now we can see it's all ones. So it's always high. I'm not pressing the button I'm
not doing any of that stuff, but serial monitors returning one. We know the answer to our question. The Pin is not floating anymore. Maybe it's like it's in a
lifeboat now, I don't know. I guess the lifeboats
floating but you get the gist. So now everybody's hunky dory. I'm not getting these false inputs. So let me go ahead and press the button, and my LED comes on,
is that cooler or what? So I let go, I press it led comes on and I let go and it goes off. So if we think about this circuit, when we're pressing the button, the path of least resistance
is what for pin 2? The path of least resistance
is to go to ground and so it's seeing that ground voltage. When we're not pressing the
button Pin 2 has no options. It's gonna see the five volts coming off of the fivefold pin on the Arduino. And that's why it would be
reading five volts or high. Well, hey, thanks a ton for
joining me in this tutorial. I hope it was helpful. Have a great day. Bye. (upbeat music)