This is my first day with Arduino. I am trying to interpret an HIGH/LOW input signal in the port 2 and use this information to send HIGH/LOW output signal from port 12 in order to turn a LED on. However, nothing happens when I turn the switch the connection of the input pin 2 from ground to 5V. I checked a couple of times that everything was connected correctly, and then I realized that the input of pin 2 is read as being a value of "2"..! Isn't it supposed to be 0(LOW) or 1(HIGH)? What am I doing wrong?
Thanks a lot!
Here is the code:
/*
* Switch and LED test program
*/
int ledPin = 12; // LED is connected to pin 12
int switchPin = 2; // switch is connected to pin 2
int val; // variable for reading the pin status
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT); // Set the LED pin as output
pinMode(switchPin, INPUT); // Set the switch pin as input
}
void loop(){
Serial.print("Read switch input: ");
Serial.println((switchPin)); // Read the pin and display the value
delay(100);
val = digitalRead(switchPin); // read input value and store it in val
if (val == LOW) { // check if switch is on ground
digitalWrite(ledPin, HIGH); // turn LED on
}
if (val == HIGH) { // check if switch is on 5V
digitalWrite(ledPin, LOW); // turn LED off
}
}
Sorry I just realized that I was printing the pin connected to switchPin and not reading the value of the input..! So my question is obsolete. But the circuit still does not work.
Serial.println((switchPin)); // Read the pin and display the value
This will only print the pin number (2). You need to move this after the switch read and change the variable here to val. (You also have an unnecessary pair of parentheses here.)
Second:
In you 2 if statements, you set val low and immediately turn around and set it high (& vice-versa) with no delay in between, therefore the on/off happens too fast for the human eye to see. Put another delay between the 2 statements.
best,
Michael
edit. Just read again and saw you caught the switch read. You still need to move the call, however.
--M
Nongsai:
Sorry I just realized that I was printing the pin connected to switchPin and not reading the value of the input..! So my question is obsolete. But the circuit still does not work.
If you want help, post how it's wired, and the current code, and a description of what it does instead of working.
Why not use the built-in LED, normally on pin 13 instead of an external LED - at least to check if your issue is the LED wiring or somethign else.
Thank you so much for your overwhelming availability!
I rewired my circuit to make it more organized in order to upload a picture.
Also, I do not quite understand why I would need to add a delay. Indeed, IF there is no voltage input in pin 2, then pin 12 gives a HIGH voltage output. Until the switch (analogical) is switched to the other position in which pin 2 receives a HIGH voltage input, in which case pin 12 stops sending any voltage output. Right? This is how I understand it, in which case no delays are necessary. Please correct me if I'm wrong!
OK, here's a couple things.
It's hard to tell from the picture, but it looks like you have no gound commection--one wire goes into 5V and one into 3.3V. Maybe it's ok--hard to see. Hard to be sure the led is connected to pin 12, too.
The resistor to your switch looks wrong. I can't see the value, (most values will work, varying in how much power they waste). However, it needs to be wired differently. See attached diagram from here: www.arduino.cc/en/Tutorial/DigitalReadSerial
You appear to have a DPDT switch, when I think you only need a single-throw switch (ie, on and open connection). Again, see the picture.
I need to go offline for a while, but I'll check later today for further questions.
Dear Michael,
Thank you for your message. I should confirm that my wires go into 5V and ground, and that the led is connected to pin 12. The resistor for the switch is only 120 Ohms because it is only for protection against any potential court-circuit. I am using a DPDT switch because I am switching the input for pin 2 (through the resistance) from 5V to ground, which requires the middle wire to be connected to the resistance, 1 to 5V and 1 to ground. Can't really get what's wrong, the digitalRead now correctly recognizes the switch so it is something with the LED circuit or program.
Well, it's an atypical way to handle switching from high to ground on an input, but since you say your serial prints are showing the correct values, I'll assume it's working the way you want. Also, given your wiring, I'm thinking we can eliminate switch bounce as the main problem--even if you get bounce on throwing the switch, the value should quickly stabilize.
Ok, let's rule the led in or out:
Have you verified that you can make it light by directly applying power to it (through a resistor of course)?
Is the led wired to ground or v+? (Can't see in the picture.)
What's the value of the resistor between the output pin and the led?
Have you double-checked the polarity of the led? (the flat side goes toward ground.)
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
has the HIGH/LOW logic reversed from yours. Making sure your logic matches your wiring appears to be the avenue to explore. Eliminating the led as the problem is the first step.
Thanks you for your perseverance! The led is wired to ground on the side in which it can't be seen on the picture. The resistor between the output and the led is 330 Ohm. I had tested the LED with this resistance before, making it blink with a sketch. I tried checked the polarity of the led various times, and also tried a different led.
I don't have the Arduino with me right now, but tomorrow I will try the LED (+resistor) separately again.
Indeed the code is reversed, but that shouldn't make any difference, it just means that no input current will cause the arduino to impose current to the LED. I will inverse it tomorrow just to double-check.
justone, this seems to be it! I plugged all the power wires into the same "rectangle" within the power rails, and now it works. I uploaded a picture in case someone has a similar problem. I will do more testing now to check how these power rails are connected.
I'm curious about why you need a resistor between
the switch and the arduino?
The switch goes between 0 and 5V that should be in range
of the inputs.
Dwight
Indeed the resistor is not necessary, I 'm kinda' just over-precautious for now, and I thought it could somehow buffer any mistake such as causing a court-circuit. I opened my breadboard and realized that the powerails are each divided into two disconnected rails. Quite confusing.