Quantcast
Channel: Vcc to Ground » Tutorials
Viewing all articles
Browse latest Browse all 15

Controlling an LED on Arduino using a push button

$
0
0

We have already seen how to blink an LED using the delay() function on the Arduino. In that project, the switching ON and OFF of the LED was automated. It was in the control of the CPU, however, you had the liberty of changing the interval of the delay() function. But sometimes, you would want to have control over the hardware. Sometimes, you would want to decide when to switch ON an LED and when to switch it OFF. For this, we use buttons, switches, and other types of inputs. In this project, we are going to be working with push buttons.

Push buttons come in different shapes, sizes, and colors. You may buy the ones which look like the buttons shown in the image. These come with four leads. One of them connects to the ground, one to the Vcc through a resistor, and you take the output from the other lead. The fourth lead can be left unattended. It is a good idea to keep the button pulled up. You can Google “pull up circuits” for more info on that. For now, you can continue with the circuit as shown in the figure below.

Button Circuit

I have connected the button to pin 7 on the Arduino for this example. But you can connect to any other pin and make the corresponding changes in the code.

Now, we need an LED. We can use the same circuit which we used in the last project. I have reproduced the same circuit diagram here. We can connect the LED to pin 13 on the Arduino, or to another pin. Just remember to change the pin declarations in the code as well.

LED Circuit

Now that we are done with the circuit, let have a look at the code.

int led=13; //declaring pin 13 on the Arduino board as the LED pin
int button=7; //declaring pin 7 on the Arduino board as the push button pin

void setup() //function which sets up the Arduino board
{
  pinMode(led, OUTPUT); //telling the controller that the LED pin is an OUTPUT pin
  digitalWrite(led, HIGH); //writing a HIGH to the LED pin, which will turn ON the LED

  pinMode(button,INPUT); //telling the controller that the button pin is an OUTPUT pin
}

void loop() //function which will be executed continuously on the controller
{
  int buttonValue = digitalRead(button); //reading the value of the button.
  //This will tell us if the button is pressed or released.

  if(!buttonValue) //if button is presssed
    digitalWrite(led,HIGH); //switch ON the LED
  else
    digitalWrite(led,LOW); //or else switch OFF the LED
}

As you can see from the code, we are reading the value of the pin to which the button is connected. If the button is not pressed, the value of the pin 7 will be 1, or HIGH, because of the pull up resistor. And when the button is pushed, the connection between the output pin of the switch and the ground is made, and hence, we get a 0, or LOW, at the button pin.

When we detect a LOW, we are switching ON the LED, and as soon as we detect a HIGH, we are switching OFF the LED again. You can experiment with this as much as you want. Try changing the code and see what happens. And if you have any doubts, please leave a comment.

You can check the output of the code in the video below.


Viewing all articles
Browse latest Browse all 15

Trending Articles