Programming Examples

Arduino program to turns on LED when the button is pressed once and remains on until the button is pressed again to turn it off


ON OFF

Arduino program to turns on LED when the button is pressed once and remains on until the button is pressed again to turn it off.

Solution

int buttonState = 0;        
int lastButtonState = 0;   
int ledState = LOW;       

void setup() {
  pinMode(13, OUTPUT);
  pinMode(2, INPUT);
}

void loop() {
  buttonState = digitalRead(2);
  if (buttonState != lastButtonState) 
  {
    if (buttonState == HIGH) 
    {
      ledState = !ledState;
      digitalWrite(13, ledState);
    }
    delay(50);
  }
  lastButtonState = buttonState;
}
▶ RUN

Output/ Explanation:


Latest Current Affairs 2025 Online Exam Quiz for One day Exam Online Typing Test CCC Online Test Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Online MBA 2 years Online MCA Online BCA Best Website and Software Company in Allahabad