LED won't turn off

I'm using the standard button code to test a switch on a nano. When I use the built in LED, it works. when I use an external LED, the LED is on all the time and gets brighter when I push the switch. I suspect it might be a soldering issue, but can't see where...

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 4;     // the number of the pushbutton pin
const int ledPin =  10;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}

It might be a wiring problem. Did you do something like that ? (with your own pins of course)

Show us a good schematic & image of your circuit.

Posting images:
http://forum.arduino.cc/index.php?topic=519037.0

.

Of course you did put a pull-down resistor between the button pin and ground, didn't you?

I suggest you learn at this point, the common convention - connect the button between the input pin and ground and set

pinMode(buttonPin, INPUT_PULLUP);

then simply use

if (buttonState == LOW)

thanks for all this. I think the issue was 2 pins shorting due to dodgy soldering and failing eyesight. I need more powerful lenses, sad to report.

“I need more powerful lenses,”

I know what you mean :frowning:

Santa might come :slight_smile: