I've been trying to detect a push button based on this tutorial, but due to my lack of skills/knowledge I haven't been able to make it work, could you guys point out what I'm doing wrong?
Any help is appreciated
My project on 123d circuits
My code:
void setup() {
pinMode(2, INPUT);
pinMode(3, OUTPUT);
digitalWrite(3, HIGH);
Serial.begin(9600);
}
void loop() {
trigger();
}
void trigger(){
if (digitalRead(2) == HIGH) {
Serial.println("Hey");
//turn();
}
}
1 Answer 1
You seem to be suffering from three problems here:
- A lack of understanding how the button's pins are configured
- A lack of understanding of how breadboard works
- A strange notion (that I have seen around a lot recently for some reason) that a button needs to be driven from an output.
For the button's pins you should know that the pins are paired on the long sides. That is, if you hold the button so it is in "landscape" orientation (wider than it is high) the two upper pins are joined together, and the two lower pins are joined together.
Breadboard consists of strips of connections which, in your diagram, would be running horizontal. Pins plugged into holes horizontally in line with each other on the same side of the board (the gap in the middle is a gap in the connections too) will be connected together.
Finally for the wiring of the button. Nowhere in the tutorial you link to does it ever mention connecting the button to two IO pins. The button should only be connected to one IO pin. It also connects to +5V and, through the resistor, to ground. Wire it up exactly as it shows you in the tutorial and make it work like that before you try fiddling with it.
-
Thank you so much for helping me out and for having patience to explain to me what I was doing wrongKyle– Kyle2016年04月13日 11:36:09 +00:00Commented Apr 13, 2016 at 11:36
-
so electrons from the black jumper go to red one and if I push the button they will get to pin 2? a weird question, will the electrons go through pin 2 and get out of the 5V pin? if not, where will they go?Kyle– Kyle2016年04月13日 11:58:10 +00:00Commented Apr 13, 2016 at 11:58
-
At your stage of development you shouldn't be concerning yourself with electrons. For input pins you are only concerned about the voltage at the pin. With the button released the voltage is 0V. With the button pressed it is 5V.Majenko– Majenko2016年04月13日 14:38:28 +00:00Commented Apr 13, 2016 at 14:38