One "2 PIN Button" on 2 GPIO Pins

Hey Arduino Friends,

i have a question about the digital Pins. Because i am using Arduino Due with enough digital Pins, i want to connect some Buttons (until WIFI and App is finished to takes control) and i dont want to use my breadboard.
So i used an 2x pin stripe to connect the Button / Wires. Now i want to check the Button State programmatically. It works but i am not sure if it is a good solution (electronics).

Without a diagram or code we can't tell you... And what's "2x pin strip" or a 2 PIN Button"?

I believe it's a push button you are connecting. Normally I check the button state using digitalRead and I dont find any issue with that.

Sorry, the image can not be uploaded. Trying again.

I am using some normal Push Button with two wires and soldered with 2 Pins to connect it directly to the Ar. Due.

My Code looks like this:

Setup:

  pinMode(PIN_BUTTON_2_OUT, OUTPUT);
  digitalWrite(PIN_BUTTON_2_OUT, LOW);
  pinMode(PIN_BUTTON_2_IN, INPUT);
  digitalWrite(PIN_BUTTON_2_IN, HIGH);

Loop:

  if(digitalRead(PIN_BUTTON_2_IN)==LOW){ Serial.print("Button 2 is pressed!\r\n"); }

If it's just a normal push button than this is a bit wasteful. Just connect one side to GND and the other to a pin. Then you don't need the:

pinMode(PIN_BUTTON_2_OUT, OUTPUT);
digitalWrite(PIN_BUTTON_2_OUT, LOW);

A normal button doesn't need a output.

Then you're left with on input on which you turn on the internal pull up. You can do that with

pinMode(PIN_BUTTON_2_IN, INPUT);
digitalWrite(PIN_BUTTON_2_IN, HIGH);

But you can also do it in one line, which I find more clear

pinMode(PIN_BUTTON_2_IN, INPUT_PULLUP);

Little code tip, a line is done after a { or a ;. So:

if(digitalRead(PIN_BUTTON_2_IN) == LOW){ 
  Serial.println("Button 2 is pressed!"); 
}

Makes the code clearer.

And by using println() you can skip the whole line end stuff :wink:

A photo of the connection is very difficult to understand, a schema drawn in Fritzing or any other tool is easily readable.

I normally use the code approach you mentioned in the post.

To connect switches I used the Internal pull-up resistor approach mentioned in post Gammon Forum : Electronics : Microprocessors : Switches tutorial

Thank u both for explanation and the Link.

pinMode(PIN_BUTTON_2_IN, INPUT_PULLUP);

...is better to understand.

I used GND and the internal pullup before. Works well. the Problem is, that i need to use some more Buttons and Sliders and the Wires between Breadboard, Arduino, Buttons, ... are driving me crazy. :o)

So while development i would like to connect the Buttons directly to 2 GPIO Pins. But im still not sure if the Code is 'electrically' ok?!

  pinMode(PIN_BUTTON_2_OUT, OUTPUT);
  digitalWrite(PIN_BUTTON_2_OUT, LOW);
  pinMode(PIN_BUTTON_2_IN, INPUT);
  digitalWrite(PIN_BUTTON_2_IN, HIGH);

Yes it is. But adding all the LOW outputs just to create GND's for switches doesn't help the code being readable.

If you need multiple buttons in a test situation and a breadboard is driving you crazy (tip, small solid core wire like this already keeps it more manageable) then just get out your soldering iron and make a small PCB :wink:

Small PCB :), that's what i do normally after a prototyping in breadboard. Wires drives me crazy :), takes me an hr or two to etch a simple home made pcb and drill holes.

But you don't need to make it that fancy... Just a prototype PCB, takes you 5 minutes. Just solder on more buttons then you ever need and you can re-use it every time you need buttons.

Thank u both. :slight_smile:

Since it is my first project and I have no experience with electrical engineering, I am pleased with all instructions. However, since almost all components will be shipped from China, I will buy next week a prototype pcb in the electronics shop in the city.

Maybe i will try an playstation controller because it seems to be connected with something like spi communication.