Go Down

Topic: How to toggle led (Read 20157 times) previous topic - next topic


I want to toggle 1 led. When button is pressed led comes on and when pressed again goes off.


That will be a state change then. Look in the IDE under File-> Examples -> 02 Digital -> StateChangeDetection


is there a way to do it without buttonpushcounter


Did your try?  StateChangeDetection
No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.


Here you go (connect button to PIN2 and ground):
Code: [Select]

const int buttonPin = 2;    //  button is connected to pin

bool buttonState = 0;       // variable to hold the button state
bool Mode = 0;              // What mode is the light in?

void setup()
  pinMode(buttonPin, INPUT_PULLUP);    // Set the switch pin as input

void loop()
  if (!digitalRead(buttonPin)) // cheks if button is pressed (cheks if it's LOW, becouse PULLUP inverts signal: LOW = button pressed)
    if (!buttonState) // same as: (buttonState == false) ---I made it a bit compact, but it may be confusinf to you
      buttonState = true;
      Mode = !Mode; // this inverts button mode: If Mode was True - it will make it False and viseversa
  else buttonState = false;

  // blink LED
  digitalWrite(LED_BUILTIN, Mode); // remember: TRUE = HIGH = 1 and FALSE = LOW = 0 (You can use whatever You like)


Feb 14, 2018, 04:05 am Last Edit: Feb 14, 2018, 04:07 am by Wawa
Shorter version.
Code: [Select]
const byte buttonPin = 2; // button between pin2 and ground

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);

void loop() {
  if (!digitalRead(buttonPin)) {
    digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));


is there a way to do it without buttonpushcounter
You need to know what the last state produced by the button to know what the next state should be. So how ever you dress it up you have to count the presesses up to a maximum of two.

What does that question really mean? Does it mean you can't understand the example and you think we deliberately want to make things complex?


Feb 24, 2020, 01:34 am Last Edit: Feb 24, 2020, 01:35 am by IoT_hobbyist
I am sorry for answering an old post. But I hope it is useful for some googling this issue.  You can see how to toggle LED by button in this tutorial. The tutorial includes code WITH and WITHOUT debouncing
- How to get started with Arduino: https://arduinogetstarted.com
- Arduino Tutorials For Beginners: https://arduinogetstarted.com/arduino-tutorials
- Arduino FAQ: https://arduinogetstarted.com/arduino-faq


I am sorry for answering an old post.
I don't like, when old posts are getting pushed.

The 3 answers from 2018 were ok and are still ok.
The "solutions" in your linked tutorial ain't no better than the provided solutions. Using "int" for each and any variable is bad coding behavior!
how to react on postings:
- post helped: provide your final sketch, say thank you & give karma.
- post not understood: Ask as long as you understand the post
- post is off topic (or you think it is): Stay to your topic. Ask again.
- else: Ask again.

Go Up