LESSON 1: CONTROLLING LED BY BUTTON

0

#Arduino #ArduinoLED #ControlLEDByButton

Introduction
In this experiment, you will learn how to turn on/off an LED by using an I/O port and a button. The “I/O port” refers to the INPUT and OUTPUT port. Here the INPUT port of the Arduino Uno board is used to read the output of an external device. Since the board itself has an LED (connected to Pin 13), so you can use this LED to do this experiment for convenience.

Components

  • 1 * Arduino Uno board
  • 1 * USB cable
  • 1 * Button
  • 1 * Resistor (10kΩ)
  • Jumper wires
  • 1 * Breadboard

Principle
Buttons are a common component used to control electronic devices. They are usually used as switches to connect or disconnect circuits. Although buttons come in a variety of sizes and shapes, the one used here is a 12mm Tactile button as shown in the following pictures. Pins pointed out by the arrows of same color are meant to be connected.

When the button is pressed, the pins pointed by the blue arrows will connect to the pins pointed by the red arrows. Generally, the button is directly connected in an LED circuit in order to turn on or off the LED. This connection is relatively simple. However, sometimes the LED will light up automatically without pressing the button, which is caused by various interferences. In order to avoid these external interferences, a pull-down resistor is used, that is, to connect a 1K–10KΩ resistor between the button port and GND. It is used to consume external interferences while connected to GND for as long as the button switch is turned off. This circuit connection is widely used in numerous circuits and electronic devices. For example, if you press any button on your mobile phone, the backlight will light up.

Step 2: Program (Please refer to the example code)

Step 3: Compile the code

Step 4: Upload the sketch to the Arduino Uno board Now, press the button, and the LED on the Arduino Uno board will light up.

THE CODE

/**********************************/
const int keyPin = 12; //the number of the key pin
const int ledPin = 13;//the number of the led pin
/**********************************/
void setup()
{
  pinMode(keyPin,INPUT);//initialize the key pin as input 
  pinMode(ledPin,OUTPUT);//initialize the led pin as output
}
/**********************************/
void loop()
{
  //read the state of the key value
  //and check if the kye is pressed
  //if it is,the state is HIGH 
  if(digitalRead(keyPin) ==HIGH )
  {
    digitalWrite(ledPin,HIGH);//turn on the led
  }
  else
  {
    digitalWrite(ledPin,LOW);//turn off the led
  }
}
/************************************/
THE TUTORIAL VIDEO IN MY YOUTUBE CHANNEL

Related searches: controlling led by button controlling led by button arduino control led by button arduino button tutorial arduino button led stays on arduino button led arduino button switch mode arduino button code arduino button box arduino button on off switch arduino button long press

Join this channel to get access to perks:

https://www.youtube.com/channel/UCEoNaDH6R_Iu8ee58-hsGxQ/join

Follow our social media accounts:

Facebook: https://www.facebook.com/kringgidz

Facebook: https://www.facebook.com/gideongasulasbuniel

Website: https://e-boombots.com/

Instagram: https://www.instagram.com/gideongbuniel/

Twitter: https://twitter.com/KringxG

Tumbler: https://www.tumblr.com/blog/kringxgidz

Pinterest: https://www.pinterest.ph/KringxGidz

Linkedin: https://www.linkedin.com/in/gideon-buniel-118bb35a/

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Follow us on our social media accounts.