Arduino Button Toggle LED (Pin State) Tutorial

In this tutorial, we’ll create two Arduino Button Toggle LED Projects and you’ll learn step-by-step how to do it yourself. We’ll also discuss the working of code examples in detail and run the projects in both real-world and simulation environments. Without further ado, let’s get right into it!

Table of Contents

  1. How To Toggle LED Using Button in Arduino?
  2. Button Toggle LED Project Wiring Diagram
  3. Arduino Code – Button Toggle LED Without Debouncing
  4. Arduino Code – Button Toggle LED With Debouncing
  5. Button Toggle LED Project Simulation
  6. Button Toggle LED Project Testing
  7. Project Wrap Up

How To Toggle LED Using Button in Arduino?

To toggle an LED using a push button in Arduino, we need the following components:

  • Arduino Board
  • Push Button
  • LED
  • Resistors (1x 10kΩ and 1x 330Ω)

The toggle action means inverting the digital state of the output pin (or LED). To create an Arduino button toggle action, we’ll follow the steps below:

  1. Define an output pin (for the LED)
  2. Define an input pin (for the push button)
  3. Read the button input pin
  4. If the button is pressed: flip the pin state

And this is simply what we need to do in order to achieve button toggle action for the LED using Arduino. We’ll create two versions of this project, one without debouncing the push button and another one with a button debouncing technique to eliminate the noise or jitter in the button input signal.


Button Toggle LED Project Wiring Diagram

Here is the wiring diagram for this example project. The LED output pin is 13, and the push button input pin is 4.

Note that the LED current limiting resistor is 330Ω, The push button here is connected in a pull-down configuration with a pull-down resistor of 10kΩ.

Arduino-LED_Button-Example-Wiring


Arduino Code – Button Toggle LED Without Debouncing

In this example project, we’ll create a button toggle action for an LED output using Arduino. The push button input pin is not denounced in this example, therefore it’s prone to jitter/noise. But for the sake of simplicity, we’ll add a small delay after detecting a button click to eliminate the effect of button bouncing noise.

Code Example

Here is the full code listing for this example.

Code Explanation

The code example simply set the Builtin-LED IO pin to be output and defines BTN_PIN to be IO pin4 and sets it as an input pin. Then we read the digital state of the Button pin and turn on the LED while the button is pressed, when the button is released, the LED will be driven low again.

This is the button pin definition (assignation)

setup()

in the setup() function, we initialize the digital IO pin ( LED_BUILTIN or pin 13) to be an OUTPUT pin. And the BTN_PIN to be an input pin.

loop()

in the loop() function, we continuously read the state of the button’s pin. While it’s held down (state=1), the LED state will be toggled by doing this.

Which reads the current state of the LED output pin, inverts it, then writes it back to the LED output pin. And that’s how we toggle the LED state.

We also add a moderate delay of 500ms (0.5s) after toggling the LED to allow the user to release the button before taking the same action again and toggling the LED for the next time and so on.

❕ Note

Leaving an input pin in a floating state can result in the digitalRead() function returning always HIGH, always LOW, or even a randomly changing value HIGH or LOW. Check the Arduino pinMode Tutorial for more information on this.

???? Also Read
Arduino pinMode Function & Arduino INPUT_PULLUP Explained

This article will give more in-depth information about the Arduino pinMode() function, INPUT_PULLUP, INPUT_PULLDOWN modes, pin floating state, and how to implement internal or external pull-up


Arduino Code – Button Toggle LED With Debouncing

In this second project example, we’ll implement the same button toggle action for an LED using Arduino. But this time, we’ll implement a simple button debouncing technique to eliminate the effect of the input button bouncing.

Code Example

Here is the full code listing for this example.

Code Explanation

The code example simply set the Builtin-LED IO pin to be output and defines BTN_PIN to be IO pin4 and sets it as an input pin. Then we read the digital state of the Button pin and turn on the LED while the button is pressed, when the button is released, the LED will be driven low again.

This is the button pin definition (assignation)

We also define some global variables to save the LED state, and button state, and perform the button debouncing logic.

setup()

in the setup() function, we initialize the digital IO pin ( LED_BUILTIN or pin 13) to be an OUTPUT pin. And the BTN_PIN to be an input pin.

loop()

in the loop() function, we continuously read the state of the button’s pin. If it has changed since the last read, we get a timestamp using millis() function.

If debounce time (defined as 50ms) has passed, we then check if the button state is still the same or if it has changed. If it’s valid, we’ll be sure that it’s not noise and the button is actually pressed by the user. Therefore, we’ll carry on the LED toggle action.


Button Toggle LED Project Simulation

We can test this project’s code examples using any available Arduino simulator environment. Here I’ll show you the simulation results for this project on both TinkerCAD and Proteus (ISIS).

TinkerCAD Simulation

Here is the simulation result for this project on the TinkerCAD simulator. You can run it as is, or make a copy and add your own code and start running the simulation to see how it’s going to behave.

You can check this simulation project on TinkerCAD using this link.

Proteus (ISIS) Simulation

Another extremely powerful simulation environment for Arduino is the Proteus (ISIS) with Arduino add-on library. It can definitely run our test projects for this tutorial. But you won’t feel its power until you need some virtual test equipment like an oscilloscope, function generator, power supply, and advanced SPICE simulation for auxiliary electronic circuitry that you may build around an Arduino microcontroller.

Here is the Arduino project simulation result from the Proteus environment.

Check out the tutorial below to help you get started with simulating your Arduino projects in the Proteus simulation environment.

???? Also Read
Arduino Proteus Library Simulation Guide

This article will give more in-depth information about using proteus ISIS for Arduino projects simulation.


Button Toggle LED Project Testing

Here is a demo video for testing this project on my Arduino UNO board.


Parts List

Here is the full components list for all parts that you’d need in order to perform the practical LABs mentioned here in this article and for the whole Arduino Programming series of tutorials found here on DeepBlueMbedded. Please, note that those are affiliate links and we’ll receive a small commission on your purchase at no additional cost to you, and it’d definitely support our work.

Download Attachments

You can download all attachment files for this Article/Tutorial (project files, schematics, code, etc..) using the link below. Please consider supporting my work through the various support options listed in the link down below. Every small donation helps to keep this website up and running and ultimately supports our community.


Project Wrap Up

To conclude this project tutorial, we can say that the button toggle action is pretty much easy to implement and can be used as a software/hardware block in different Arduino projects to control other devices, not only an LED. It’s just the beginning of an endless list of possibilities for what you can do with this knowledge.

If you’re just getting started with Arduino, you need to check out the Arduino Getting Started [Ultimate Guide] here.

And follow this Arduino Series of Tutorials to learn more about Arduino Programming.

For more projects & ideas, check this Arduino Projects resource page.

???? Also Read
Getting Started With Arduino Programming For Beginners

This is the ultimate guide for getting started with Arduino for beginners. It’ll help you learn the Arduino fundamentals for Hardware & Software and understand the basics required to accelerate your learning journey with Arduino Programming.


FAQ & Answers

How to make a button toggle in Arduino?

The toggle action means inverting the digital state of the output pin (or LED). To create an Arduino button toggle action, we’ll follow the steps below:
1. Define an output pin (for the LED)
2. Define an input pin (for the push button)
3. Read the button input pin
4. If the button is pressed: flip the pin state

How to toggle LED in Arduino?

The simplest way to implement an Arduino LED toggle is to do the following: (Read the LED pin state, invert it, and write it back to the pin’s output)
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));

What is toggle state in Arduino?

Toggle state in Arduino is the act of inverting the digital state of the output from 0 to 1 or from 1 to 0.

How to use a button with Arduino without a resistor?

To eliminate the need for an external pull-up resistor, you can use the Arduino’s internal pull-up resistor with the button instead. In this way, you won’t have to connect an external resistor to the Arduino input pin connected to the button.

Share This Page With Your Network!
Join Our +25,000 Newsletter Subscribers!

Stay Updated With All New Content Releases. You Also Get Occasional FREE Coupon Codes For Courses & Other Stuff!

Photo of author
Author
Khaled Magdy
Embedded systems engineer with several years of experience in embedded software and hardware design. I work as an embedded SW engineer in the Automotive & e-Mobility industry. However, I still do Hardware design and SW development for DSP, Control Systems, Robotics, AI/ML, and other fields I'm passionate about.
I love reading, writing, creating projects, and teaching. A reader by day and a writer by night, it's my lifestyle. I believe that the combination of brilliant minds, bold ideas, and a complete disregard for what is possible, can and will change the world! I will be there when it happens, will you?

Leave a Comment