Push Button - Led on for 5 sec after push button press

Note : Please Check the Image for Circuit

Code :
const int buttonPin=2;
const int ledPin=13;
int buttonState=LOW;
void setup()
{
pinMode(ledPin,OUTPUT);
pinMode(buttonPin,INPUT);
}
void loop()
{
buttonState=digitalRead(buttonPin);
if(buttonState==HIGH)
{
digitalWrite(ledPin,HIGH);
delay(5000);
}
else
{
digitalWrite(ledPin,LOW);
delay(50);
}
}

  • By Pramod Kumar

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Do you have a question ?

Place yours between code tags represented by the </> icon when editing your post to make it formatted and readable.

const int buttonPin = 2;
const int ledPin = 13;
int buttonState = LOW;
void setup()
{
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
}
void loop()
{
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH)
  {
    digitalWrite(ledPin, HIGH);
    delay(5000);
  }
  else
  {
    digitalWrite(ledPin, LOW);
    delay(50);
  }
}

Is your project not making your RED LED turn on? That LED should do the same as the Arduino's LED.

Maybe check that your button conducts the signals in the correct direction (vertical in your drawing).

*** and you need to ground the breadboard ground rail (for the LED path)

Do you have a question?

The other side of the LED (green wire) appears to be connected to nothing on the breadboard?

Maybe it should connected to GND?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.