Tom Boy
Tom Boy

Reputation: 11

The led doesn't switch on when button pressed and doesn't switch off when I let go off my button, what is wrong with it?

This is my code I wrote for when I press the button the Led turns on , when I let go of the button then it switches off. It's always on when I press the button and always on when I let go off the button. Why is it always on?

    int Input_Pin = 13;
    int Out_Pin = 2;
    int Button_read;



    void setup()
    {
     pinMode(Input_Pin, INPUT);
     pinMode(Out_Pin, OUTPUT);
     Serial.begin(9600);
    }

    void loop()
    {
     Button_read = digitalRead(Input_Pin);
     if (Button_read == 1) {
     delay(1000);
     digitalWrite(Out_Pin, LOW);
     Serial.println("off");
     } 
     if (Button_read == 0) {
     delay(1000);
     digitalWrite(Out_Pin, HIGH);
     Serial.println("On");  
     }
    }

enter image description here

Upvotes: 1

Views: 158

Answers (1)

Anirudha Saraf
Anirudha Saraf

Reputation: 41

I think I found an issue in your diagram... Basically, your pin 13 is always gonna be HIGH because the 5V has a direct connection through it. There is nothing wrong with your code Allow me to demonstrate

How the electricity flows

The fixed circuit for this would be (The red is wrong and the blue is the fixed connection): enter image description here

Upvotes: 2

Related Questions