1

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?

Circuit

The diagram I followed: https://i.sstatic.net/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");
 Serial.begin(9600);
 pinMode(buttonPin, INPUT);
 pinMode(ledUt, OUTPUT);
 pinMode(ledTest, OUTPUT);
}
void loop() {
 digitalWrite(ledTest, HIGH);
 buttonState = digitalRead(buttonPin);
 Serial.println("Sjekk");
 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) {
 buttonPushCounter++;
 Serial.println("on");
 Serial.println(buttonPushCounter);
 } else {
 Serial.println("off");
 }
 }
 buttonStateForrigeForrige = lastButtonState;
 lastButtonState = buttonState;
 if (tilstand == HIGH) {
 digitalWrite(ledUt, HIGH);
 } else {
 digitalWrite(ledUt, LOW);
 }
 }
asked Oct 29, 2016 at 18:58
3
  • Is this switch connected to positive voltage or negative voltage? Commented Oct 29, 2016 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. Commented Oct 30, 2016 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? Commented Oct 30, 2016 at 20:51

2 Answers 2

1

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.

answered Oct 29, 2016 at 21:35
1
  • Sorry, I have no experience in making electronics diagrams. Your diagram shows just what I did, with a 12k resistor. Commented Oct 30, 2016 at 12:25
1

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

answered Oct 30, 2016 at 9:58

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.