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?
-
1Where 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).aaa– aaa2016年08月16日 11:54:00 +00:00Commented Aug 16, 2016 at 11:54
-
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 ;)aaa– aaa2016年08月16日 12:12:35 +00:00Commented Aug 16, 2016 at 12:12
-
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 :)Code Gorilla– Code Gorilla2016年08月16日 12:22:22 +00:00Commented Aug 16, 2016 at 12:22
-
I have a book which has this circuit...user15851– user158512016年08月16日 13:47:45 +00:00Commented Aug 16, 2016 at 13:47
4 Answers 4
Your code and schematic both seem to be wrong.
Try out this schematic:
And then code:
const int kPinLed = 6; // LED - Pin 6
const int kPinBtn = 7; // Push Button - Pin 7
void setup()
{
pinMode(kPinLed, OUTPUT); // LED as OUTPUT
pinMode(kPinBtn, INPUT_PULLUP); // Push Button as INPUT wiith pullup
}
void loop()
{
if(digitalRead(kPinBtn) == LOW) // When Push Button is pressed
{
digitalWrite(kPinLed, HIGH); // LED ON
}
else digitalWrite(kPinLed, LOW); // LED OFF
}
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...
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! :) )
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.