Go Down

Topic: connecting push button to arduino (Read 1 time) previous topic - next topic

_Ema_

Hello,

I have a problem connecting push button to arduino. I tried to connect the push button as shown in tutorial, and I wrote the code shown there, but I still have a problem with the push button, and I cant figure out what the problem is.

I used this tutorial, and lots of other, I made the conections as shown there, but I still have a problem.

I made the conections as show in the picture


After that I wrote the code

Code: [Select]
const byte switchPin = 8;
   
void setup ()
  {
  Serial.begin (9600);
  pinMode (switchPin, INPUT);
   }  // end of setup

void loop ()
  {
  if (digitalRead (switchPin) == HIGH)
     {
       
     Serial.println ("Push Button pressed");
     delay (1000);
     } // end if switchState is HIGH
   else
 {
    Serial.println("Push button unpressed");
 } 
  // other code here ...
   
  }  // end of loop


In this case serial monitor prints "Push Button pressed" , even I push the button, or not.


I tried connecting the button as shown in this figure

(I connected the leg to pin 11)
And even i press the button, It still prints  "Push button unpressed"

I cant figure out what should be the problem.
I tried  4 push buttons, even  a switch, but I still have same problem, like it cant read the state of pressed button. Should the push buttons been broken?
Should I tried a different push button, or i did something wrong in the code, or at connections?

I use Arduino Mega if it has any relevancy.

I used this kind of push button
http://postimg.org/image/ndvprzrx7/

Can someone help me with this issue? Thanks



couka

#1
May 29, 2016, 01:01 pm Last Edit: May 29, 2016, 01:02 pm by couka
The wiring is correct, and the code should also work, when using the first schematic.
On those buttons, there are always 2 pins directly connected. The connection between these pairs is established by pressing the button.



Maybe you have just used the wrong pins. A photo of your setup would be helpful.

lg, couka
youtube.com/DieBastler1234
Don't send me technical questions via PM. They will be deleted unanswered.

CatStrategist

Did you change the code when you were using the second schematic?

From
pinMode (switchPin, INPUT);
To
pinMode (switchPin, INPUT_PULLUP);?

_Ema_

The wiring is correct, and the code should also work, when using the first schematic.
On those buttons, there are always 2 pins directly connected. The connection between these pairs is established by pressing the button.



Maybe you have just used the wrong pins. A photo of your setup would be helpful.

lg, couka
i made the connection as show in the picture
http://postimg.org/image/46j9jssgr/
I dont know if it correctly made

couka

I can't tell exactly what the pinout of this switch is, but to me it looks like 5V and your digitalPin are directly connected.

lg, couka
youtube.com/DieBastler1234
Don't send me technical questions via PM. They will be deleted unanswered.

_Ema_

I tried lots of combinations with legs of the button, but with no results


In your opinion, should be the problem at the button?

Did you change the code when you were using the second schematic?

From
pinMode (switchPin, INPUT);
To
pinMode (switchPin, INPUT_PULLUP);?
At second conection I used this code

Code: [Select]
const byte switchPin = 11;
  
void setup ()
  {
  Serial.begin (9600);
  pinMode (switchPin, INPUT_PULLUP);
   }  // end of setup

void loop ()
  {
  if (digitalRead (switchPin) == HIGH)
     {
      
     Serial.println ("Push Button pressed");
     delay (1000);
     } // end if switchState is HIGH
   else
 {
    Serial.println("Push button unpressed");
 }  
  // other code here ...
   delay(300);
  }  // end of loop


I just forget to change the code provided by you, and i tried it now, but i still get "Push button unpressed"


couka

If using INPUT_PULLUP, HIGH means not pressed, LOW means pressed.

But I'm 99% sure the problem is your wiring. Please provide a photo.

lg, couka
youtube.com/DieBastler1234
Don't send me technical questions via PM. They will be deleted unanswered.

_Ema_

Im really messy. I dont think that you would understand something from my conections/ photo.

I tried to be as attentif as I could, regarding conection.

But if only this could be the problem then, I will try more

here is a picture about my conection

http://postimg.org/image/64ogqjt7f/

couka

#8
May 29, 2016, 02:44 pm Last Edit: May 29, 2016, 02:46 pm by couka
There's a lot of other stuff that could cause problems. If you are trying to fight a bug, remove everything  that is not necessary to reproduce it.

Try connecting Wire1 to the same pin as Wire2.
If that doesn't work move Wire3 to the unused pin next.

But please buy a DMM with a continuity tester. That would have solved your problem long before.

lg, couka
youtube.com/DieBastler1234
Don't send me technical questions via PM. They will be deleted unanswered.

cattledog

Quote
I tried lots of combinations with legs of the button, but with no results
Always wire diagonally across the switch. This will connect a pair of switched legs and never the permanently connected legs.

_Ema_

Most likley the button was broken , or I short circuit it, cause I tried all combinations possible of wiring . I changed it with a new one, smaller one, as shown in couka"s post and now its working. This kind of button is more easy to handle with.
Thanks for help.

Go Up