Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Here is my circuit: enter image description here

and here is my code:

int LED = 13;
int BTN = 7;

void setup() {
pinMode(LED,OUTPUT);
pinMode(BTN,INPUT);
}
void loop() {
if(digitalRead(BTN) == HIGH) {
digitalWrite(LED,LOW);
}
else{
digitalWrite(LED,HIGH);
}
}

When I press the button, the LED is supposed to go low, but it doesn't. What is wrong with this circuit/code?

share|improve this question
1  
Where did you base your button connection on? To me it seems that it's always connected to GND. Pressing the button will send the electricity to GND, through the resistor, it's unlikely that it goes into the pin, since it has higher resistance (as far as I know). – Paul yesterday
    
It may be worthwhile to search up some electronics tutorials and/or Arduino basic tutorials. When following a (good) tutorial, they would explain how to set up the electronics (and why it works). Just connecting stuff in a way you think will work, is going to cause headaches or worse ;) – Paul yesterday
    
Max - A really useful site for you might be 123d.circuits.io which allows you to blow up components virtually. It lets you simulate your circuits (and code) and means you don't fry real Unos, which you may well do. I've never yet managed to fry every component in a circuit on 123d, but I keep trying :) – Matt yesterday
    
I have a book which has this circuit... – Max yesterday

The Input port is going to always be at Gnd, because you have connected it to ground. Sorry to put it in such basic terms but this is the way I understand it. It needs to be more difficult for the output of the button to get to ground than to get to the BTN pin of your UNO. So put a bigger resistor (1K-10K) in place of your yellow wire between the button and ground.

And your LED is the wrong way round in the diagram.

(And Majenko is a quicker typist than me! :) )

share|improve this answer

Paul is correct. Your button is wired wrong. The GPIO pin should be connected to the same side as the resistor. Also the button needs rotating through 90 degrees.

Also check how your LED is wired up. At the moment it looks like it's set up so that the GPIO sinks the current (LOW turns the LED on) yet the LED looks like it's connected the wrong way around for that.

In fact, I can't actually find anything right about that circuit...

share|improve this answer
    
And button seems to be shorted in breadboard too – KIIV yesterday
    
Well spotted. Updated answer. – Majenko yesterday

In addition to the answers from Matt and Majenko, you code uses the wrong pin for the LED.

You have int LED = 13; whereas your LED is connected to pin 6.

NB: Pin 13 is connected to an LED soldered on to the Arduino board - as seen in the image below.

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.