Address
304 North Cardinal
St. Dorchester Center, MA 02124

Work Hours
Monday to Friday: 7AM - 7PM
Weekend: 10AM - 5PM

Tutorial How to Control Led by a Button Arduino

In this lesson, we will learn Control Led by a Button, how to detect the state of a button, and then toggle the state of the LED based on the state of the button

Hardware Required

Principle

Button
Layer 1a 1

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 in this experiment will be a 12 mm button, as shown below.

The button we used is a normally open type one. The two contacts of a button are in the off state under the normal conditions; only when the button is pressed, they are closed.
The schematic diagram is as follows:

Layer 2 2

The button jitter must happen in the process of using. The jitter waveform is as the flowing picture:

Control Led by a Button

Each time you press the button, the Arduino will regard you have pressed the button many times due to the jitter of the button. You should deal with the jitter of buttons before using. You can eliminate the jitter through software programming. Besides, you can use a capacitor to solve the issue. Take the software method for example. First, detect whether the level of button interface is low level or high level. If it is low level, 5~10ms delay is needed.
Then detect whether the level of button interface is low or high. If the signal is low, you can infer that the button is pressed once. You can also use a 0.1uFcapacitor to avoid the jitter of buttons. The schematic diagram is as shown below:

How to Control Led by a Button
Interrupt

Hardware interrupts were introduced as a way to reduce wasting the processor’s valuable time in polling loops, waiting for external events. They may be implemented in hardware as a distinct system with control lines, or
they may be integrated into the memory subsystem

Key functions:

●attachInterrupt(interrupt, ISR, mode)

Specifies a named Interrupt Service Routine (ISR) to call when an interrupt
occurs. Replaces any previous function that was attached to the interrupt.
Most Arduino boards have two external interrupts: numbers 0 (on digital pin 2)and 1 (on digital pin 3).
Generally, an ISR should be as short and fast as possible. If your sketch uses multiple ISRs, only one can run at a time, other interrupts will be ignored(turned off) until the current one is finished. as delay() and mills() both rely on interrupts, they will not work while an ISR is running. delayMicroseconds(),
which does not rely on interrupts, will work as expected.
Syntax:
attachInterrupt(pin, ISR, mode)
Parameters:
pin: the pin number
ISR: the ISR will be called when the interrupt occurs; this function must take no parameters and return nothing. This function is sometimes referred to as an interrupt service routine.
Mode: defines when the interrupt should be triggered. Four constants are predefined as valid values:
-LOW to trigger the interrupt whenever the pin is low,
-CHANGE to trigger the interrupt whenever the pin changes value
-RISING to trigger when the pin goes from low to high,
-FALLING for when the pin goes from high to low.

●digitalRead()

Reads the value from a specified digital pin, either HIGH or LOW.
Syntax:
digitalRead(pin)
Parameters:
pin: the number of the digital pin you want to read (int)
Returns:
HIGH or LOW

●delayMicroseconds(us)

Pauses the program for the amount of time (in microseconds) specified as parameter. There are a thousand microseconds in a millisecond, and a million microseconds in a second.
Currently, the largest value that will produce an accurate delay is 16383. This could change in future Arduino releases. For delays longer than a few thousand microseconds, you should use delay() instead.
Syntax:
delayMicroseconds(us)
Parameters:
us: the number of microseconds to pause (unsigned int)
Returns:
None

Circuit Diagram

How to Control Led by a Button 1

Attach the Push Button and LED on the Breadboard.

  • Connect the Pin 5V of the Arduino UNO with the Breadboard for further
    5V power supply connections.
  • Connect GND of the Arduino UNO with the Breadboard for further GND
    connections.
  • Connect the first leg of Push Button with 5V rail of Breadboard.
  • Connect 10KΩ resistor on the second leg of the Push Button.
    Pg. 54
  • Connect Digital Pin 7 of Arduino UNO with the second leg of Push
    Button via 10KΩ resistor (one leg).
  • Connect the other leg of resistor with GND rail of the Breadboard.
  • Connect positive terminal of LED with Digital Pin 11 of Arduino UNO.
  • Connect negative terminal of LED with the GND rail of Breadboard via
    220Ω resistor.

Arduino Code’s Control Led by a Button

/****************************************************************************************************
Description: Tutorial How to Control Led by a Button Arduino
Website: www.Iduino.co.in
E-mail: support@Iduino.co.in
Modified BY: IDUINO
****************************************************************************************************/

int ledpin=11;// initialize pin 11

int inpin=7;// initialize pin 7

int val;// define val

 

/* The setup() function is called when a sketch starts. It is used to initialize variables, pin modes, start using libraries, etc. This function will only run once, after each power up or reset of the Arduino board. */

 

void setup()

{

pinMode(ledpin,OUTPUT);// set LED pin as “output”

pinMode(inpin,INPUT);// set button pin as “input”

}

 

/* The loop() function executes the program repeatedly until Specified. */

 

void loop()

{

val=digitalRead(inpin);// read the level value of pin 7 and assign if to val

if(val==LOW)// check if the button is pressed, if yes, turn on the LED

{

digitalWrite(ledpin,LOW);

}

else

{

digitalWrite(ledpin,HIGH);

}
}

Working and Output

Welcome to the Arduino Based Project which consists of Push Button and
LED’s. When the button is pressed, the LED will be on. In this program, we
add a statement of judgment. Here, we have used if () statement. When
we press the button, pin 7 will output high level.
We can program pin 11 to output high level and turn on the LED. When
pin 7 outputs low level, pin 11 also outputs low level and the LED remains
off. When the button is pressed, the LED is on, otherwise, the LED remains off. After
the above process, the button-controlled LED experiment, is completed.
The simple principle of this experiment is widely used in a variety of circuit
and electric appliances. You can easily come across it in your everyday
life. One typical example is when you press a certain key of your phone,
the backlight will be on.

How to Control Led by a Button Arduino 1

Code Explanation

Initializing variables and Pin for input and values for the program

int ledpin=11;// initialize pin "11" as "Ledpin" for output for led 

int inpin=7;// initialize pin "7" as "inpin" For Data input for Switch 

int val;// define "val" as integer type for saving data for program 

The setup() function is called when a sketch starts. It is used to initialize variables, pin modes, start using libraries, etc. This function will only run once, after each power up or reset of the Arduino board.

void setup()

{

pinMode(ledpin,OUTPUT);// set LED pin as “output”

pinMode(inpin,INPUT);// set button pin as “input”

}

The loop() function executes the program repeatedly until specified.

void loop()
{
val=digitalRead(inpin);// read the level value of pin 7 and assign if to val

if(val==LOW)// check if the button is pressed, if yes, turn on the LED

{
digitalWrite(ledpin,LOW);
}

else
{
digitalWrite(ledpin,HIGH);
}}

2 Comments

Leave a Reply

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