I'm trying to have a common light switch turn a LED on and off.

When I connect the light switch directly to a LED, it works like it should - the LED turns on and off with the switch.

When I connect the light switch back to the board, changing the switch does nothing - I get the exact same signals regardless of whether the switch is on or off.

Why? How do I make the board recognize whether the switch is on or off?


The diagram I followed: https://i.stack.imgur.com/pIoMd.png

const int  buttonPin = 2;

const int ledUt = 13;
const int ledTest = 12;
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;

int tilstand = 0;

int buttonStateForrigeForrige = 0;

boolean light = false;

void setup() {
  Serial.println("Hello World");
  pinMode(buttonPin, INPUT);
  pinMode(ledUt, OUTPUT);
  pinMode(ledTest, OUTPUT);


void loop() {

  digitalWrite(ledTest, HIGH);
  buttonState = digitalRead(buttonPin);

  tilstand = LOW;
  if ( buttonState == HIGH ) {
    Serial.println("buttonState HIGH");
    if ( lastButtonState == HIGH ) {
      Serial.println("lastButtonState HIGH");
      tilstand = HIGH;
    } else {
      Serial.println("lastButtonState LOW");
  } else {
    Serial.println("buttonState LOW");

  if (buttonState != lastButtonState) {

    if (buttonState == HIGH) {
    } else {
  buttonStateForrigeForrige = lastButtonState;
  lastButtonState = buttonState;

  if (tilstand == HIGH) {
    digitalWrite(ledUt, HIGH);
  } else {
    digitalWrite(ledUt, LOW);
  • Is this switch connected to positive voltage or negative voltage?
    – user27783
    Oct 29 '16 at 20:25
  • At present this is not clearly stated enough to be answerable. Your code appears to look for input on pin 2, but your second diagram, perhaps from Nick rather than your actual circuit, provides it on pin 8. Your original diagram does not provide any useful information to indicate if you have a workable circuit or not. Oct 30 '16 at 15:52
  • You can draw a schematic in pen and scan it if you have to. Do you have a resistor in series with the LED?
    – Nick Gammon
    Oct 30 '16 at 20:51

See my page about switches. Judging by your very-hard-to-read diagram, you have just connected the switch to an input pin like this:

Switch with floating input

That will not give a positive reading either way because it is "floating". You should connect a pull-up or pull-down resistor, like this:

Pull-up resistor

Now if the switch is open, it is pulled-up to 5V by the resistor and the input will read HIGH. When the switch is closed the switch conducts and "overpowers" the resistor, and it would read LOW.

An alternative is to use a pull-down resistor like this:

Pull-down resistor

That reverses the sense of the switch. When open it will read LOW and when closed it will read HIGH.

I hope you have a resistor in series with the LED.

  • Sorry, I have no experience in making electronics diagrams. Your diagram shows just what I did, with a 12k resistor. Oct 30 '16 at 12:25

You can also use pinMode(buttonPin, INPUT_PULLUP); instead of pinMode(buttonPin, INPUT); in that case the arduino "activates" an internal pull-up resistor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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