LED Problem

I have a push button and LED that I wired-up on my breadboard. I set the Push button to Pin 7 as a digital input and the LED to pin 12 as a digital Output with a current limiting resistor.

When I press the button the LED turns on and stays on. It does not shut off when I let go of the button. What am I doing wrong? I know I could use Pin 13 which has a built-in LED but this is not what I want to do in this project.

Here is the sketch I had written:

const int LED = 12 ; //Tied to digital pin as an OUTPUT
const int BUTTON = 7 ; //Tied to digital pin 7 as an INPUT

void setup()
{
pinMode(LED, OUTPUT) ;
pinMode(BUTTON, INPUT) ;
}

void loop()
{
if (digitalRead(BUTTON) == HIGH)
{
digitalWrite(LED, HIGH) ;
}
else
{
digitalWrite(LED, LOW) ;
}
}

Thanks in advance

Your button is connected up wrong.

Wire it between the input pin and ground. Then change that line in the setup function to read:-

pinMode(BUTTON, INPUT_PULLUP) ;

Hi Mike

Thanks for the quick reply. I am not sure what you mean when you say my button is connected wrong. Regarding the pins on the buttons, this is what I have:

1,4 goes to vcc on Uno
2,3: goes to Pin 7 on Uno

from pin 2,3 I have a current limiting resistor that is used to protect the LED. The LED is attached to pin 12.
Also I am wondering, if I use pin 13, would I still have to use INPUT_PULLUP? If not, can u please explain the reason

Thank you

Yes what you have is wrong. That is why I said it is wrong.
Those wires to Vcc on the arduino should be to ground and you should enable the pull up resistor.

At the moment you have what is known as a floating input, that is an input not electrically connected to anything when the button is not pressed. Classic beginners mistake.

The LED wiring is pants, just connect it and the resistor to an output.

It is still not working. This time the LED does not turn on. If I am following you currently, where is the supply coming from?

Can u please provide some sort of a schematic or something that I may follow along. I have been on this issue for the last 3 days trying to figure it out.

Thanks

It is better if you post a photo of what you have then we could spot what you have wrong.

Hello Mike,

here is the photo:

Red Wire = Going from Vcc rail to 5v on the Arduino
Green Wire = first pin of tacticle switch to power rail
Blue Wire = going from second pin of tacticle switch via 10k resistor
LED (+) is connected to Blue Wire as mentioned above
Yellow Wire = LED (+) going to Pin 12 on Arduino
White Wire =LED(-) to ground on Arduino

thanks very much

DSCN0160[1].JPG

figured it out. Very Simple mistake :slight_smile:

Yaser43082:
figured it out. Very Simple mistake :slight_smile:

No, as I have been telling you thought this thread the circuit is wrong.
There is no current limiting on the output pin to the LED. This means that your arduino is being damaged every time your software puts pin 12 high. If you reach or exceed 40mA from a pin damage occurs. Without a resistor to limit the current this is what will happen. You are not doing your LED much good either.

That is a very odd circuit anyway, you seem to want the LED to be lit when the button is pressed without any software intervention.
Attached is my view of your schematic along with what you should have.

P.S. Click on that photo you posted and see how useless it is because of the size.

LED Problem.pdf (25.3 KB)

Hi Mike,

Thanks for the schematic. I will try it and let you know. I think what was confusing me was the power rail. It is already coming from the USB port if I am not mistaken.

Thanks again

I think what was confusing me was the power rail. It is already coming from the USB port if I am not mistaken.

It is but why is that confusing?

I just started with Arduino and trying to get familiar with things