Arduino UNO R4 - Button - LED
This tutorial teaches you how to use the Arduino UNO R4 and a button to control an LED. We will learn two different ways to do this:
Application 1 - The LED follows the button's state:
Application 2 - The LED changes its state each time the button is pressed:
If the Arduino UNO R4 detects that the button has been pressed (changing from HIGH to LOW), it will turn the LED on if it is off, or turn it off if it is on.
Releasing the button does not change the LED's state.
For Application 2, we need to debounce the button to make sure it works correctly. We will see why this is important by comparing how the LED behaves with and without debouncing in the Arduino code.
Or you can buy the following sensor kits:
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.
If you are unfamiliar with LED and button (including pinout, operation, and programming), the following tutorials can help:
This image is created using Fritzing. Click to enlarge image
#define BUTTON_PIN 7
#define LED_PIN 3
void setup() {
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(LED_PIN, OUTPUT);
}
void loop() {
int buttonState = digitalRead(BUTTON_PIN);
if (buttonState == LOW) {
Serial.println("The button is being pressed");
digitalWrite(LED_PIN, HIGH);
}
else
if (buttonState == HIGH) {
Serial.println("The button is unpressed");
digitalWrite(LED_PIN, LOW);
}
}
Follow these instructions step by step:
Wire the components according to the provided diagram.
Connect the Arduino Uno R4 board to your computer using a USB cable.
Launch the Arduino IDE on your computer.
Select the appropriate Arduino Uno R4 board (e.g., Arduino Uno R4 WiFi) and COM port.
Copy the code and open it in the Arduino IDE.
Click the Upload button on the Arduino IDE to compile and upload the code to the Arduino UNO R4.
You will see that the LED state is in sync with the button state.
Check out the line-by-line explanation contained in the comments of the source code!
#define BUTTON_PIN 7
#define LED_PIN 3
int led_state = LOW;
int button_state;
int prev_button_state;
void setup() {
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(LED_PIN, OUTPUT);
button_state = digitalRead(BUTTON_PIN);
}
void loop() {
prev_button_state = button_state;
button_state = digitalRead(BUTTON_PIN);
if(prev_button_state == HIGH && button_state == LOW) {
Serial.println("The button is pressed");
led_state = !led_state;
digitalWrite(LED_PIN, led_state);
}
}
You can locate the explanation in the comment lines of the Arduino UNO R4 code above.
In the code, the expression led_state = !led_state is equal to the following code:
if(led_state == LOW)
led_state = HIGH;
else
led_state = LOW;
Copy the code and open it in the Arduino IDE.
Upload the code to the Arduino UNO R4.
Press release and button several times.
Check out the change in the LED's state.
You might notice that the LED changes every time the button is pressed. However, this might not always work perfectly. Sometimes, the LED might change too many times quickly when you press the button once, or it might not change at all (it changes twice very quickly, which is hard to see).
⇒ To fix this problem, we need to debounce the button.
Debouncing a button can be challenging for beginners. Fortunately, the ezButton library makes it easy.
Why is debouncing necessary? See the Arduino UNO R4 - Button Debounce tutorial for more information.
#include <ezButton.h>
#define BUTTON_PIN 7
#define LED_PIN 3
ezButton button(BUTTON_PIN);
int led_state = LOW;
void setup() {
Serial.begin(9600);
pinMode(LED_PIN, OUTPUT);
button.setDebounceTime(50);
}
void loop() {
button.loop();
if(button.isPressed()) {
Serial.println("The button is pressed");
led_state = !led_state;
digitalWrite(LED_PIN, led_state);
}
}
Copy the code and open it with Arduino IDE.
Click the Upload button on Arduino IDE to upload the code to the Arduino UNO R4.
Press and release the button multiple times.
Check out the LED's state change.
You will see that the LED state is toggled exactly once each time the button is pressed.