Tip:
Highlight text to annotate it
X
Hello and welcome to the second video in my line of Arduino tutorials. Today I'm gonna
show you how you can control an LED, or any other output device, using a button on your
Arduino. And this is, although you can do this from just, say, a nine-volt battery,
an LED, a resistor and a button, this is just a base for a lot more stuff you can do with
a button, because with a nine-volt battery you couldn't make a button dim a light, say,
which we'll go into in a future episode. But just start a new sketch as I have done and
do 'void setup' and 'void loop'. In 'void setup', as well as having 'pinMode(7, 1)'
you're gonna need to have 'pinMode(6 [or any other IO pin], 0)' to mean it's an input,
rather than an output. Then, under 'void loop' you are going to have an 'if' statement which
will say 'if(digitalRead(6) == 0)' then 'digitalWrite(7, 1)' to turn it on. Then, after that, just
close up the 'if' and put 'else' then 'digitalWrite(7, 0)' to turn it off. Then close the whole thing
up. And that is the whole thing to make a button turn on or off an LED or a buzzer,
or any other output device. And then just upload that, and now I will show you how to
wire up the circuit you're gonna need. Okay, so to wire this up, yo are going to need a
breadboard, of course, and just plug your pushbutton into where you would normally plug
a pushbutton in, so there's the gap in the breadboard, plug it over the top of that gap.
Then wire one corner of it (I've chosen this corner for no reason whatsoever) to ground,
it can be any of the three grounds on the board (Uno), then, on the diagonally opposite
corner, so in this case that corner, wire up, on the same rail, a resistor, and then
positive voltage from that resistor down to either five volts or three point three volts,
it doesn't matter, and that makes it so there's actually current flowing through it, otherwise,
when you push the button, there wouldn't be anything to monitor, so it would just be either
a constant 1 or a constant 0. Then connect, on the same rail as you connected the resistor
to the pushbutton, connect your pushbutton to pin 6, or whatever pin you're using for
the button, and that means that, when you press the button, the current flows through
the negative, and is allowed to go all the way through, not only to complete the circuit
back to 5 volts, but also so that this pin can pick it up. And that is how you wire it
up so that your button should activate your LED. And that's the end of this tutorial!
Next time I'm gonna be showing you how you can use PWM to control an LED's brightness
on your Arduino, and like and subscribe for more tutorials. Thanks for watching!