Control LED Through Push Button with Arduino

In this project, you'll learn how to turn your light on and off via a Push Button and how to select different functionalities of the code using only one button

Control LED Through Push Button with Arduino

We will use the Arduino UNO to get the status of the push button and show that through LED.

Component list

  Arduino UNO x1

 Buy Now

  Breadboard x1

 Buy Now

  USB cable x1

 Buy Now

  LED x1

 Buy Now

  Resistor 220Ω x1

 Buy Now

  Resistor 1KΩ x1

 Buy Now

  Push-button x1

 Buy Now

  Jumper M/M x2

 Buy Now

Circuit

Use D12 of Arduino UNO to detect the status of push button, and D9 to drive LED.

Hardware Connection

Circuit knowledge

Connection of Push Button

In Chapter 1, we connect the push button directly to the power line of the circuit to control the LED to light on or off. In digital circuits, we need to use the push button as the input signal. The recommended connection is as follows:

In the above circuit diagram, when the button is not pressed, 5V (high level) will be detected by UNO port; and 0V (low level) when the button is pressed. The role of Resistor R2 here is to prevent the port from being set to output high level by accident, which could be connected directly to the cathode and cause a short circuit when the button is pressed.

The following diagram shows another connection, in which the level detected by UNO port is opposite to the above diagram when the button is pressed or not.

Sketch

Now, write code to detect the state of push-button, and show that through LED.

int buttonPin = 12; // the number of the push button pin int ledPin = 9;    // the number of the LED pin 

void setup() { 
  pinMode(buttonPin, INPUT); // set push button pin into input mode 
  pinMode(ledPin, OUTPUT);   // set LED pin into output mode  
} 

void loop() { 
  if (digitalRead(buttonPin) == HIGH) // if the button is not pressed 
    digitalWrite(ledPin, LOW);	// switch off LED 
  else	// if the button is pressed 
    digitalWrite(ledPin, HIGH);	// switch on LED 
} 

After the port is initialized, the LED will be switched on or off in accordance with the state of the pin connected to push button.

digitalRead(pin)

Arduino Software provides a function digitalRead(pin) to obtain the state of the port pin. The return value

is HIGH or LOW, that is, high level or low level.

Verify and upload the code, press the button, then LED lights up; loosen the button, then LED lights off.

How to Change LED State by Push Button?

Circuit knowledge

Debounce for push button

When push button is pressed, the potential won't transfer from one state to another state ideally. In fact, it will occur a continuous bounce process before it becomes final stable state.

If you detect the state of push button, there may be repeated "press" and "release" detected during one process of pressing. So it is necessary to eliminate the influence caused by the bounce. Here is the most direct and effective way: when the changed signals are received by Arduino UNO for the first time, the program does not operate immediately, just waits for a certain time to skip the bounce process and confirm the final state of push button.

Sketch

Now, write the code to detect the state of push button. Every time you pressed, the state of LED will be changed.

 int buttonPin = 12; // the number of the push button pin int ledPin = 9;    // the number of the LED pin 
boolean isLighting = false; // define a variable to save the state of LED 

void setup() { 
  pinMode(buttonPin, INPUT); // set push button pin into input mode 
  pinMode(ledPin, OUTPUT);   // set LED pin into output mode 
} 

void loop() { 
  if (digitalRead(buttonPin) == LOW) {	// if the button is pressed 
    delay(10);   // delay for a certain time to skip the bounce 
    if (digitalRead(buttonPin) == LOW) {	// confirm again if the button is pressed 
 reverseLED();	// reverse LED 
 while (digitalRead(buttonPin) == LOW); // wait for releasing 
 delay(10); // delay for a certain time to skip bounce when the button is released 
    } 
  } 
} 

void reverseLED() { 
  if (isLighting) {	// if LED is lighting, 
    digitalWrite(ledPin, LOW); // switch off LED 
    isLighting = false;	// store the state of LED 
  } 
  else {	// if LED is off, 
    digitalWrite(ledPin, HIGH); // switch LED 
    isLighting = true;	// store the state of LED 
  } 
} 

We use a function reverseLED () to change the state of the LED.

When judging the push button state, if it is detected "pressed down", wait for a certain time to detect again to eliminate the effect of bounce. When confirmed, it starts to wait for the push button to be released and waits for a certain time to eliminate the effect of bounce after it is released.

Verify and upload the code, then each time you press the button, LED changes its state accordingly.

What's Your Reaction?

like
0
dislike
0
love
0
funny
0
angry
0
sad
0
wow
0