Problem with a pushbutton

int LEDPin=6;
int buttonPin=12;
int buttonRead;
int dt=100;
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
pinMode(LEDPin, OUTPUT);
pinMode(buttonPin, INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
buttonRead=digitalRead(buttonPin);

Serial.println(buttonRead);
delay(dt);
if(buttonRead==1){
  digitalWrite(LEDPin, LOW);
}
if(buttonRead==0){
  digitalWrite(LEDPin, HIGH);
}
}

Hello guys,
I have got a problem with my pushbutton circuit.
I have got an Arduino Uno, all connected, in the right port.
I wanted to create a circuit which will turn the LED on as I push the button, but there is a problem. My serial monitor only reads 1, when I push the button it does not show 0 and the LED does not turn on. I’m pretty sure that I got all of it connected it the right way, the right pins etc. I have tried all of the cables that I used in this circuit by connecting them to the resistor to LED and 5V and they were good. I used 10k ohms resistor, which seems to be working because I attached LED to it and it worked. I also tried replacing the pushbuttons, but none of the 4 that I have worked. Please help.
I also attach the picture of my breadboard.
Have a nice day

154194042_869785987087750_3036369234771811398_n.jpg

153386664_465377271268403_2060017892632938625_n.jpg

153443628_3915848935141416_551950686492322649_n.jpg

153730508_238595157933040_3610829964796131627_n.jpg

Remove the switch.

Manually connect pin 12 to GND.

Do you get a LOW on pin 12 ?

If so, try turning the switch 90°.

Congratulations on posting good images and the sketch properly !

Change this:

pinMode(buttonPin, INPUT);

To:

pinMode(buttonPin, INPUT_PULLUP);

Remove the resistor and its associated wire between the button and ground.

Connect the button to ground on one side and the input on the other.

Read this: https://forum.arduino.cc/index.php?topic=719995.0

Nice, clear photos and properly formatted code, thank you.

Actually the OPs connections should work as is, external pull-up.

larryd: Actually the OPs connections should work as is, external pull-up.

It might be that I can't see the photos properly, but it looks to me like one side of the button is grounded and the other side has a pull DOWN resistor.

Looks like one end of the 10k goes to 5v, the other end to the switch and to pin 12.

Other lead of the switch is on GND.

Looks like 330 Ω is connected to Anode on the LED and pin 6, Cathode to GND.

They should ban these switches from kits >:( .

Hmm possibly right now I have had another look.

OP, which one of us is correct? If Larry is then you can ignore my comments.

I bought 100 of those for about £3. Have to straighten out the pins for breadboard.

Need metric protoboards? But then the DIPs wouldn't fit. These would be way more clear B3F6002BYOMZ

And just a penny more in 500-lots.

Nice


Maybe Xroads can make these up ;).

PerryBebbington: Hmm possibly right now I have had another look.

OP, which one of us is correct? If Larry is then you can ignore my comments.

I bought 100 of those for about £3. Have to straighten out the pins for breadboard.

Do the leads need to be turned 90° so as to not deform the spring contacts ?

larryd: Do the leads need to be turned 90° so as to not deform the spring contacts ?

Not had to do that. They don't go in breadboard very well though. Thinking maybe a PCB to hold 4 of them.

Hey, you have a job this weekend :) .

Vertical or horizontal PCB ?


Made some vertical LED helper boards:

|500x459

|500x276

Just use the 2 lead parts with long legs, bend to meet whatever pitch is needed.

I think it's most probably that your switch isn't making good contact into the protoboard. Those square ones are absolutely terrible for breadboard use and can screw up the internal connections if you aren't careful when trying to shove them in.

Get two-legged ones like this, they're much easier on your breadboard: https://www.amazon.com/Momentary-Tactile-Through-Breadboard-Friendly/dp/B07WF76VHT/ref=sr_1_4?dchild=1&keywords=tactile+push+button+switch+arduino&qid=1614208973&sr=8-4

PerryBebbington: Hmm possibly right now I have had another look.

OP, which one of us is correct? If Larry is then you can ignore my comments.

I bought 100 of those for about £3. Have to straighten out the pins for breadboard.

Turns out that larryd was right.

larryd: Remove the switch.

Manually connect pin 12 to GND.

Do you get a LOW on pin 12 ?

If so, try turning the switch 90°.

Congratulations on posting good images and the sketch properly !

So I have connected pin12 to GND and indeed I got 0, which I think indicates that it's LOW. Then I tried switching the buttons and most of them did not work, but it turned out that 1 out of 5 buttons from my kit works :). What's funny is that yesterday I have tried all of them and none worked. Maybe some of them will work tomorrow, who knows?

So what new pushbuttons should I order? The ones that @Jiggy-Ninja mentioned are currently unavailable.

Amazon.com: MakerSpot 6mm 2 Pin Panel PCB Momentary Tactile Tact Push Button Switch Through Hole Breadboard Friendly x 10 Pack: Computers & Accessories

would these work properly? I presume that the pushbuttons that I have right now do not make a good contact with the breadbork, as someone suggested above. These ones have longer legs, so maybe they will work better?

Also, I have got a similar problem with my potentiometer. It does not always work properly and I have to push it in order to read from it. Can anyone recommend me a good potentiometer, which would work properly?

Thanks y'all for help!

Would these pushbuttons be good and would they fit to the breadboard? They look solid. They are made by Jialong and Conntherm

pushbutton.jpg|598x360

pushbutton2.jpg|254x254

The first link you posted looks fine. It’s basically the same switches I linked to, just a different size head on them.

The other two are terrible. Especially the 3rd one. It’s the same as what you already used, just a different sized head. Avoid those. Maybe the 2nd one’s OK, but you’d have to see the pin spacing to be sure.

Amazon has lots:

1 turn potentiometer

10 turn potentiometer

10k is a reasonable value for general purpose use.


You can role your own: |500x407

|500x419

|500x360

|500x444

|500x323