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;
}
Output


Online Exam Quiz for One day Exam Online Typing Test CCC Online Test 2026 Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Best Website and Software Company in Allahabad