1

I've got an Arduino Uno.

I was building a simple circuit with only one switch and it doesn't seem to work (I followed the Arduino project book from the Arduino starter kit).

This is my circuit : Photo of circuit

Then I try again to move the switch and then it works (but I don't understand why).

Photo of circuit with active LED

So I tried to understand where the error is and I try to read digital input from the simple circuit with only one LED :

Simple LED circuit

And this is my code :

int SWITCH = 13;

void setup() {
    Serial.begin(9600);
    pinMode(SWITCH, INPUT);
}

// the loop routine runs over and over again forever:
void loop() {
    int val;
    val = digitalRead(SWITCH);
    if(val == HIGH) {
        Serial.println("OKAY");
    }
}

I expect to see on the Serial Monitor "OKAY OKAY" and so on... but nothing appears on the Serial monitor.

Then I try to change the condition :

if(val == LOW)

And now I see "OKAY OKAY" and so on...

I think that there is something wrong with this Arduino... Can someone tell me something about what is happening? Am I stupid or there is really something wrong with this?

2
  • Could you try to turn the led around. And maybe even take out the switch. If that doesn't work, try another LED and turn it around when it won't work. LED's have polarity, which means they only work one way around. Commented Jan 30, 2016 at 11:39
  • I tryied with a lot of different leds and switches. In the lastest image (link) the led is on so this works right but if a read digital input from that I see that there is no voltage. Why? Commented Jan 30, 2016 at 11:43

1 Answer 1

2

It looks to me like your arduino is working just fine.

In the first two images you use the arduino only to provide 5v power to the circuit on the breadboard. In the first image you haven't connected your button to properly function as a button. I don't know the exact pin layout of the button (which should be explained in a datasheet of that button), but it is also good to know exactly how to holes in a breadboard are connected. This picture shows why your button works on your second image but not your first:

enter image description here

This picture also shows how the breadboard holes are connected:

enter image description here

In your third image you have practically connected input pin 13 to GND, so it makes sense that digitalRead(13) in your code always returns LOW. Thus, the variable var is always LOW (or 0). Thus, the comparison (var == HIGH) is FALSE, which won't send "OKAY" to the serial monitor. And, the comparison (var == LOW) is TRUE, which will* send "OKAY" to the serial monitor.

I hope you can follow the logical steps and it is clear to you what is happening.

1
  • Thanks so much for your answer. I've build now the same circuit and it works right. If i push my switch the led turn on. Here is the picture of my circuit : imgur.com/6DWyWr6 . If I use the same code than above using val == LOW than print "OKAY" what I have to expect in output if my switch is open, and what if my switch is closed? (i tryied to print Serial.println(digitalRead(SWITCH)) and also if my switch is open or close i always have 0) Why? Thanks. Commented Jan 30, 2016 at 14:08

Your Answer

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.