Led Working Even If Button Not Pressed

I Am New To Arduino. I Was Tryıng To Make A Basic Program That If Button Is Pressed Then Turn The Led On else Turn the led off. I Am Using Arduino Uno: Here Is Code

const int led = 13;
const int btn = 12;

void setup() 
{
 pinMode(led, OUTPUT);
 pinMode(btn, INPUT);
}

void loop() 
{
 if(digitalRead(btn) == HIGH)
 {
   digitalWrite(led, HIGH);
 }
 else
 {
   digitalWrite(led, LOW);
 }
}

I Connected 5v to "+" on breadboard And Connected GND to "-" On breadboard. I Connected Led's Little pin to gnd and long pin to 13rd pin with 220ohm resistor on arduino via breadboard.
I Connected button's pin to gnd via 10k ohm resistor. second pin to 5v and third pin to 12nd pin on arduino via breadboard. I Uploaded code without any errors. Led Turned On Itself even if i did not pressed button. Then I pressed Button And Nothing happened. Led Is Still On. I tested led without button before like a code: turn led on delay 2 seconds and then turn off led on void loop . And it worked but the code I wrote with button does not Work. Thanks For Your Helps Now. ~Mard

if you want to use this external pull down resistor, you should connect GND --10k - PIN12.
The button just needs to be connected between Pin12 and 5v

when you close the button, the 5V goes to PIN12 and the minor GND will have no effect as there is the 10K resistor.

I tried It. But did not work. Still same led turns on itself

draw a schematic and upload it to the forum.
make real pictures of your setup and post it so we can see what you really have. Each element - each wire must be visible.

SHOW A REAL PICTURE.

you might have the button connected the wrong way:
Push Button/Tactile Switch Pinout Connections, Uses, Dimensions & Datasheet.
it never gets high.

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