1

I know this is ridiculously simple task but I spent the last two hours trying to get this to work and it is not working. I don't have an Arduino but I have an ESP8266 Huzzah Feather which is programmable via the Arduino IDE. So, I don't think I can provide a Fritz diagram but in any case, I'm just trying to hook up a pushbutton and be able to read a HIGH or LOW (via my Serial monitor). Currently, everything is 0.

As best I can describe it is:

5V from Feather into resistor into one terminal of the pushbutton. Out the other terminal of the pushbutton is a wire connecting to pin 12 of the Feather, and also a wire leading to ground. Feather is also grounded.

My code is:

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

void loop()
{
  int button_val = digitalRead(12);
  Serial.println(button_val);
}

It shouldn't be this hard for me. Whenever I open and run the serial monitor, it reads 0s. What am I missing?

Oh, an issue that I faced is determining which 2 out of the 4 terminals of the pushbutton to use. Some resources I find say that it is the same side as the terminal the voltage is flowing into; other resources say it is the terminal opposite to the side the voltage is flowing into. I currently have it opposite.

  • If you have a multimeter you can measure how to connect your switch up, measure the resistance between the pins. Two sets of pins will measure 0 Ohms, you want to use a pair which doesn't measure 0 Ohms – Doodle Apr 4 '16 at 7:34
  • If you don't have a multimeter, you can connect an LED (with current limiting resistor) between your Arduino and button. It should go on when the button is pressed. – Paul Apr 4 '16 at 10:37
  • And before doing everything else read @Majenko's answer, because reading what you wrote you wired the button in the wrong way (this is the correct way) – frarugi87 Apr 4 '16 at 16:27
3

Out the other terminal of the pushbutton is a wire connecting to pin 12 of the Feather, and also a wire leading to ground.

That sounds to me like you have connected the IO pin direct to ground.

The IO pin should connect to the same pin as the resistor that connects to +5v. The other side of the button should connect to ground only.

For instance using @HansNeve's button picture you could connect pin A to the IO pin and via a resistor to +5V, and pin B to ground.

  • Oh, my. This worked wonderfully for me. Thank you! Both answers were helpful but I had to pick this one because it told me that my IO pin was connected incorrectly as well as which terminals of the button to use. Now, a follow-up question: my button reads 1 without a press and 0 with a press. Is that right or should it be the other way around? I tried swapping terminals but that didn't seem to correct the issue. – noblerare Apr 4 '16 at 17:43
  • That is completely correct. It is known as "active low" because it gives a "low" signal when you activate it. To make it "active high" you reverse the entire circuit by connecting the wire that is connected to ground to 5V instead, and the resistor that is connected to 5V to ground instead. – Majenko Apr 4 '16 at 17:45
  • @noblerare be aware, though, that the active low configuration is the most common, because you can exploit the internal pull-up (no need for resistor, just enable the pullup) – frarugi87 Apr 5 '16 at 12:28
  • 1
    @frarugi87 thanks, I kept it as active low and modified my code accordingly. – noblerare Apr 6 '16 at 1:56
4

The pins on each side of the button are shorted, the "button" is between the sides of the button. Else your code looks alright.

Edit: Here's an image which shows how a push button is connected internally, make sure you're using the correct pins.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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