Controlling LEDs with Arduino

Push Buttons and Switches

Your browser needs to be JavaScript capable to view this video

Try reloading this page, or reviewing your browser settings

This segment introduces the concept of controlling LEDs with hardware to achieve either an on/off effect or digital input. Also introduces the coding concepts of state changes and building push button circuits.

Keywords

  • push buttons
  • switches
  • led
  • digital I/O
  • led
  • arduino

About this video

Author(s)
Liz Clark
First online
09 April 2019
DOI
https://doi.org/10.1007/978-1-4842-4883-6_4
Online ISBN
978-1-4842-4883-6
Publisher
Apress
Copyright information
© Liz Clark 2019

Video Transcript

In this section we’ll begin to go over the use of added components in a circuit to control LEDsbeginning with push buttons and switches

To follow along, you’ll need the materials used in our previous sections, as well as a temporary push button, a latching switch, and some 10k resistors. Let’s get started.

So far we’ve gone over some ways to have the Arduino control LEDs with various coding methods. However, often in a project, you’ll want to physically control an LED with some type of peripheral. One of the most common is with a push button, where if you engage a button, the LED then reacts in some way. When using this method, the LED is essentially waiting to be triggered by a button’s input that is translated by the Arduino via the code.

First, let’s take our breadboard and build a circuit to include a button and an LED. We’ll keep our single LED circuit in place since it doesn’t need to change. But instead of having the positive rail on the breadboard carry the digital pin connection for the LED, we’ll instead connect the LED directly to digital pin 13.

We’re going to connect the positive around the breadboard to the 5 volt pin on the Arduino. We’ll be keeping this setup going forward.

For a button to be inserted into the circuit, you’ll need the button, of course, a 10k resistor, which can be identified with the color code brown, black orange. There are a wide variety of buttons available for prototyping. As long as you can successfully plug it into a breadboard, it will work properly.

Connect one side of the button to digital pin 2. On the other side, connect the pin directly across from the digital pin 2 connection to the ground rail with the 10k resistor. Then connect the remaining pin on the ground side to the 5 volt rail.

Now we can move to the code. Since we’re still at the beginning of our LED journey, we can reference another piece of included example code in the Arduino IDE. Go to Examples, Digital, and select Button.

Let’s take a look at the code. Much like other examples we’ve looked at, it begins by defining two pins on the Arduino, this time as constant integers. Constant integers are a little different from integers in that their values cannot be changed.

We do have an integer defined, though, called Button State. Button State will end up holding the status of the button, basically whether it is being pressed or not. We’ll see this utilized in the loop momentarily.

Next in the setup, we see that the LED is set as an output, and the button is set as an input. This means that the button will be sending data to the Arduino and the LED will be receiving that data.

Moving onto the loop, we see that the Button State integer is being set up to read and hold the status of the button’s digital pin, basically letting the Arduino know whether or not the button is being pushed.

This is followed by an if-else statement, where if the button is engaged, and as a result sends a high signal, the LED will also receive a high signal and turn on. Else, if the button is not engaged, and as a result is sending a low signal, the LED will follow suit and remain off. Essentially, if the button is pushed, the LED will turn on. If the button is not being pushed, then the LED will turn off.

Let’s see this in action by compiling the code and uploading it to our Arduino. As you can see, the LED is reacting to the button presses.

Now let’s make a simple change to our breadboard to affect the circuit. By swapping in a sliding switch or latching button for our push button, we can make it so that we have a light switch style setup to control our LED.

Remove the push button and insert the sliding switch so that the middle pin is connected to ground and one of the side pins is connected to digital pin 2 on the Arduino.

Using the same code, you’ll see that the LED will turn on when you slide the switch away from the pin attached to digital pin 2. We can adjust the code, though, so that the switch is switching between two LEDs.

Going back to the Arduino IDE, let’s create a new constant integer called LED Green for digital pin 12 and rename the old LED pin to LED Red.

For the switch, we’re going to rename the original Button Pin to Button Red and create a new constant integer called Button Green and attach it to pin 3.

We’re also going to create a new integer called Button State Green and have it equal 0. And you guessed it. We’ll also be renaming the original Button State integer, calling it Button State Red.

Moving onto the setup, let’s replace LED Pin with LED Pin Red, and insert a new pin mode declaration for our new LED Green constant integer that will essentially be a copy of LED Pin Red since LED will be an output. We’ll also rename Button Pin to Button Red and create a pin mode declaration for Button Green, having it be an input.

Next in the loop is where we’re going to have our fun. First, let’s rename Button State to Button State Red and have it read the status of Button Red. Then enter Button State Green, and have it read the status of Button Green. This will allow us for checking for whether or not the switch is engaged on either side.

Now for our if-else statement. We’re first going to edit our constant integer and integer labels in the original if-else statement, changing Button State to Button State Red and LED Pin to LED Red. After that, we can create a second if-else statement that will basically be a copy of our original, but using Button State Green as the integer and LED Green filling the place of the previous LED integer.

After our code is all set, we can move back to our breadboard. We’re going to insert a second LED, attaching its cathode to the ground rail, and its anode to pin 12 on the Arduino.

Then we’re going to leave our switch as it was in our previous example, but use an additional piece of jumper wire to connect to the previously unattached switch pin to pin 3 on the Arduino. Now we can compile our code and upload it to the Arduino.

If all goes well, one LED should light up when the switch is turned to the left, and the second LED should turn on when the switch is turned to the right while turning off the first LED.

We can adjust the circuit to use two push buttons instead of the sliding switch to have a slightly different effect with the same code.

On the breadboard, remove the switch and insert two push buttons, wiring one to be connected to digital pin 2 and the second to digital pin 3. Each button will also have a 10k resistor as a pull-down. Now you can have both LEDs on if you want by pushing both buttons.

When working on a project with physical inputs, though, you may want to use a serial monitor to make sure you’re sending and receiving the proper data. Much like we did with blink, let’s edit our button code so that the status of each button is printed to the serial monitor.

We’ve been using states to store our button status in this code. And this can sometimes be more complicated to write, but it helps to have streamlined code that runs smoothly. We’re going to use states for our serial data as well.

First, we’re going to create two new integers, Last Button State Red and Last Button State Green. Both will equal 0.

Next, go to Setup, and open our serial connection with serial.begin9600.

Next, we’re going to add two if statements to the loop. Basically, our goal is to only print data to the serial monitor if either of our buttons are pressed. We want to know when a button is pressed and when it is released. If we didn’t set this up with these parameters, we’d be constantly checking for data. And it wouldn’t be as precise, neat, or streamlined.

We’ll enter if Button State Red does not equal– expressed with an exclamation point and an equal sign– to Last Button State Red, then we’re going to write Button Red equals, expressed as a string, since this will be text that will print every time, followed by Button State Red.

By using serial.print for the string and serial.println for the Button State data, it will allow the string and data to print on the same line, but send whatever data comes in next to print on the next line, keeping things nice and neat.

Now we’re going to create the same if statement for the last button state green integer so that both of our buttons’ data will be collected.

And finally, we’re going to add two more lines to loop that are both very important. We’re going to set Last Button State Red to equal Button State Red. This means that both integers will be carrying each other’s value, which allows our if statement for the serial data to work, since we’ll be checking to see if the state of the button has changed. If the state changes, then it will trigger that if statement to send data to the serial monitor.

We’re going to write an identical line for Last Button State Green to equal Button State Green.

After this, we can compile and upload the code. Then open the serial monitor by going to Tools, Serial Monitor.

Once it’s open, try pressing the buttons. You should see it print that the button is on followed by it being off after you release it, represented by ones and zeros. This is a great way to ensure that your physical input is working as expected with your code and circuit.