Introduction to Arduino 4 – Button Controlled LED

So now that we've learned about variables, what we're going to do is build our first circuit.

So we've seen with the Blink program that we can make an LED on the board turn on and off, and now we're going to be able to make this decision based on us pressing a button.

First, we need to know about digital pins.

On the sides of the Arduino are theseheaders, where we can access the different pins.

We'll be able to decideif they are an input or an output.

An input will be able to measure if avoltage is high or low and an output will allow us to make the pin either high or low.

On the Arduino Uno, high is equal to 5 volts and low is equal to zero volts, but this might change for different boards.

As you can see on-screen now, this is the most basic way to wire up a button and this effectively allows us to measure either 5 volts or 0 volts depending on how it's connected and so this green cable will be the one that has changed to either 0 or 5 volts.

Now if we go back over to our camera and see that my Arduino is still blinkingfrom before, first thing I'm going to do is unplug it.

I suggest the first thingyou do in wiring up a circuit, is power these rails on the side, because thatmakes it easy to get power in and out from all the different rows in thebreadboard.

So I'm going a wire up 5 volts and ground Moving on from there, I'm going to place the button and the button goes across this gap down thecenter of the breadboard and now that I put the button in place and put a resistor here, attached to this bottom row of the button.

The other side of this resistor, I'm going to wire to ground then we're going to wirethe other side of the button to power and then on the side where we have theresistor connected, is where we're going to be measuring from and I'm gonna putthis into Digital Pin 10.

Okay, so now that's all wired up, we canstart programming it.

To start programming it, we're going to need a few basic functions, and these functions are Arduino functions used to interact with these pins.

We're going to need 'pin mode', and this is where we declare whether the pin will be an input or an output.

This is similar to declaring avariable.

We're going to need 'digital read' and that simply means to readfrom a digital pin, and we're going to need 'digital write', which is where wewrite a value of high or low to a pin making that pin high or low.

Okay, so now we're going to head over to the Arduino IDE and we're goingto make a new project.

When we make a new project, it will give us 'set up' and 'loop' to help us out but, of course, we're going to have to put some more code in.

The first thing I need to do when I actually start coding is declare the variablesI'm going to use.

So the top of file, I'm going to declare a bool, and this isgoing to be 'button'.

Now, boolean, I talked about being true or false, where true is equal to high and false is equal to low, so effectively our buttoncan actually be true or false.

In the 'setup', I'm going to initialize the pins.

So we are going to use 'pin mode', as we talked about, we're going to type in 'LED built in'.

Now LED built in, you can see it'scolored a bit differently, is actually predefined by Arduino.

I believe it's actually equivalent to writing 13 in code but this is so it's easy to read, and we're going to put output.

We're also going to use 'pinmode' again for our button.

Now, we connected that button to Pin 10 and we're going to declare it as an input.

Alright, so now we've done everythingwe need to do in 'setup', we're gonna move to the 'loop'.

So effectively, what we wantto do, is you want to read the button then tell LED if button is pressed.

So how we're going to do that in code, is using our bool that we made before button andI say the button equals.

.

.

digital read of 10.

So we're going to read Pin 10 and thebutton is going to be set to that value and then we're going to simply writethat value.

.

.

out to the LED, writing it like that.

So in the loop, we're going togo through the loop, we're going to read the pin with the button, set the buttonvariable to that value, then we are going to write out to the LED on the boardwhat the button is Next thing we want to do is plug in the Arduino again.

.

.

and we are going to upload our code.

And, of course, save it.

Now that we've uploaded the code, wecan go to the camera and we can see if, we press the button, that onboard LED will turn on.

Now, when you're doing this, make sure that the button is properlyconnected into the board.

If it's slightly loose, you'll get a noisy signal, where it might not behave like you expect, but make sure it's properlypressed into the board.

Now, if you've got that circuit to work, good job! Being able to create a circuit and create a program that works together can be quitedifficult and can take some time, so if not, let us know on Slack if you've got any questions and if we can help, but this type of thing is exactly whatmechatronics is about, what robotics is about, and, to a degree, what electronicsand software engineering is about.

So these are the fundamentals thateverything else is built on.

.

Leave a Reply