Push button switch sometimes not working

Hi Everyone!

I am new to Arduino and have a question. I would like to seek your advice and appreciate it if you can help me to find out the problem.

The problem that I encountered is, sometimes the push button switch is not working, not very often. But I have to press twice to make the relay switching.

Replaced with a new push button switch, still the same.
This is the push button that I'm using right now.

I suspect it has nothing to do with the hardware but the coding?

Based on the code below, should I use " i = 1" or "i += 1"?

What I want to test is, by using the push button switch, no matter how long I press, The relay will only switch once.

int i=0;

void setup() {
  pinMode(6, INPUT_PULLUP);   // Button pin
  pinMode(5, OUTPUT);         // Relay Pin
  digitalWrite(5, HIGH);      // Relay On

void loop() {
  int ButtonPin=digitalRead(6);
  if (ButtonPin==HIGH) {
  if (ButtonPin==LOW && i==0) {
    i = 1;
    digitalWrite(5, LOW);
    digitalWrite(5, HIGH);

Thank you!

Show us a good schematic of your circuit.

Show us a good image of your ‘actual’ wiring.

Well, I think you can start here, simply.

Yes I have found that sort of button to be problematic in the past. Only last week I used one on a Pico to allow the user to power up into boot mode and it occasionally fails. You have to hold the button down and give it a bit of a wiggle with your finger to make sure you have contact.

That is the crap tutorial that tells people to wire the button the wrong way round.

You should wire a push button between input and ground and use the internal pull up resistors.

1 Like

It may be worth mentioning that those red buttons are often N/C contacts,
while the black ones are the ‘standard’ N/O contacts.

You can use either. but it helps to know which is which !

Remember to look for state-change and debouncing the switch pulse - those cheapies are very noisy..

Odd, I’ve found this to be the other way around.

Yes you can get either normally open or normally closed in any colour, including red and green.

It´s always better to start with a sketch that works and do addtional mods, as mentioned by you, to learn different possibilities of a solution.

Yes but it is always better to start with the right circuit to begin with.

Your code should work Ok I think. Certainly there is nothing in your code which would give randomly different results. So probably the problem is hardware.

Who defines "right"?

The whole world in in consensus over this one.
It is right for three reasons

  1. economic and environmental- you don’t have to have an external resistor, you can use the internal one, so you save money and don’t need to manafacature an other resistor.

  2. Safety - by not having 5V going to all the buttons you reduce the risk of accidental shorts. Yes you can get shorts on ground connections but this is less likely to cause problems.

  3. universality- a pull up can apply to all technologies TTL and MOS based where as pull down on TTL systems require different, much lower resistor values and you shouldn’t connect inputs directly to 5V.

1 Like

Hello! Thanks everyone! I'm in the office now, so I can only provide the picture later.

I got a bit confused with this i = 1 / i += 1.... From my code, Is it correct to use i = 1? When to use i = 1 or i +=1?

Will it cause the "error"?

It won't make any difference in this case. But you should use i = 1 in preference to i += 1 for clarity, although "i" is not a good choice for the name. You are not counting anything here. You are using "i" as a flag variable, and they normally have only 2 values like true & false, yes & no, or in your case 0 & 1.

The usual way to do this is to keep the previous result of digitalRead() in a flag variable, to compare with the next result of digitalRead(). Then your flag variable has one of the 2 values HIGH & LOW.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.