I'm starting to learn how to use Arduino and I tried to make this code, which is just a simple if else statement; what I want to make is when the input is LOW, LED 1 is HIGH and LEDs 2,3 are LOW, and when the input is HIGH, the LEDs 2,3 are HIGH and LED 1 is LOW. I wrote this code but when I put the switch in LOW, instead having just like what I intended, all the outputs are HIGH.

I try changing the output and I checked all the wiring to the LEDs so my mistake is in the code.

Could you guys tell me what's wrong with the code?

int switchstate = 0;
void setup() {
  pinMode (13,OUTPUT);

void loop() {
  switchstate = digitalRead(3);
  if (switchstate == LOW) {
  } else {
  • Can you post a schematic of your circuit.
    – Aircraft
    Jul 11, 2016 at 4:27
  • do you check voltages ? When input is HIGH, does it give right answer ?
    – Anklon
    Jul 11, 2016 at 5:15
  • What type of switch, a push button or a slide switch?
    – RSM
    Jul 11, 2016 at 5:45
  • You say your circuit is correct, yet by looking at your code, the leds should never be lit at the same time.. You do not debounce the input in software, but it should not matter as if the input flickered, it should change the state of all the leds. Review the circuit again.
    – jogco
    Jul 11, 2016 at 5:52
  • Pull-up/-down in place?
    – JimmyB
    Jul 11, 2016 at 7:32

2 Answers 2


Your code doesn't seem to be the problem. Contrary to what you are saying I think your problem is most definitely in your hardware setup.

Most likely, your digitalRead doesn't register a HIGH input, so it doesn't get 5V. Instead of digitalRead, go for analogRead(A0). So change your circuit and attach it to pin A0.

Use serial.println(switchstate) to print the results to the serial monitor and then decide what your top value (HIGH) and bottom value (LOW) for detection will be. Perhaps include a pull down resistor as well on the A0 pin.

  • Thanks, I will try that as soon as I got home. One more thing thou, how the output generate all high even I put the wrong wiring. is the arduino have NC thing when there is no logic in it will just outputing high ? Jul 11, 2016 at 15:00
  • I don't know what you are using to create this HIGH input. Switchstate also is an integer so it could be possible your thing that is supposed to create a HIGH output, isn't creating a HIGH output and as such Switchstate is filled as an integer, not a boolean (booleans have two states: true or false, or HIGH or LOW). Experiment.
    – Len
    Jul 11, 2016 at 15:08

To debug this, as Len has suggested I would recommend putting in some Serial.println("Debug info") commands into your code. This will confirm that you are programming the Arduino as you expect, and that the Arduino is running.

To do this simply include a Serial.begin(9600) command in your setup block, and then a Serial.println("Some text") wherever you want to check if you are reaching a certain part of the code. You can then read these messages via the serial monitor (which you can get to from Tools->Serial Monitor in the Arduino IDE).

For hardware debugging if you have a multi-meter available I would check that you are getting 0V/5V on digital pin 3 when you expect (i.e. when you are / are not pressing the button). Also, to check your LEDs are working, and connected to the output pins you think they are, write a script which just scrolls through them with a short delay (See the Blink example under File->Examples->01.Basics->Blink for an example of how to do that).

Your Answer

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

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