Can't get output signal from Self Reset Switch

Hi everyone,
I'm a beginner on arduino and I can't get any output from a "Momentary Self Reset Switch" (that's how it's called on aliexpress).
I made a schematic to let you know my wiring.
What Im' trying to do is :

  • Push switch once
  • make built-in led blink let's say 1 or 2 seconds max
  • No action needed when you push switch 2nd time
    -> Basically just start a loop if switch is pressed

As you can see I wired a resistor (4.7k) to try pull-up (or pull down I'm not really sure...).
I made a pastebin to show you the code I'm using :

(I copied it from the web not mine)
Thanks to anyone that can help !

Hi @valenz68

Your resistor is in series, instead of a pull-up or pull-down.
Remove that resistor.

Instead, you can put a resistor from D2 to GND (for pull-down) or from D2 to +5V (for pull-up). Also, the Arduino Uno has builtin pull-ups, so in your code, you would do pinMode(switch, INPUT_PULLUP); instead of pinMode(switch, INPUT);, that way you wouldn't need a pull-up at all.

Hope this helps!

@valenz68
Please post your code here in code tags </> as per the forum instructions: How to get the best out of this forum

Here is my take on how to use buttons correctly, there are many other examples if you read through the forum questions and tutorials: Buttons and other electro-mechanical inputs (introduction)

Thanks for the help !
I already tried with (switch, INPUT_PULLUP); but it didn't change anything....

Edit : tried with both pull up and pull down, still nothing :frowning:

Try this code:

#define switch 2

int Switch;

boolean flag;

void setup(){
  Serial.begin(9600); Serial.println("");
  pinMode(switch, INPUT_PULLUP);
}

void loop(){
  Switch = digitalRead(switch);
  if (Switch == LOW){
    flag = !flag;
    Serial.print("Flag value: ");
    Serial.println(flag);
    while (Switch == LOW);
    delay(100);
  }
}

Also, make sure the common terminal on the switch is connected to GND, rather than +5V.

So I switched common to gnd.
And my Serial monitor shows "Flag value: 1" when I press the switch !
But when I press it again nothing happens... (it only shows "Flag value: 1" once in the monitor)

EDIT:
I commented this :

And it seems to have fixed it, I can push multiple times and it shows output everytime I push the button.

You tried to but....
It's a picture, not a schematic. Most of the surface is empty, white surface, not used for useful information. It's completely uninteresting to see the physical shape of the UNO, the breadboard and an unknown third device.
It's very interesting too see pin designations, pin names etc.
The pictured stuff will not show any life. There's no powering of the kit. Insufficient powering is very often the reason for failure, therefore my "attack".

Yeah I tried, was my first time using Fritzing and I just wanted to make it easy to understand (and I didn't find the same component as the switch that I have that's why I edited it in Photoshop)

Powering is done via USB, but I have a DC Adapter if needed.

I added that line to prevent the flag being changed multiple times for one button press. Try pressing the button for ~2 sec and I'm pretty sure that the flag will toggle several times.

And I make it easy for me as I don't spend time on members easyness, to step aside. Spending time in helping members create the basic question has limits.

You can use the "L" light as a logic probe to tell if you are getting a signal--put one end of a temporary jumper on your pin 13 and the other on pin 2 or the switch and see if the LED turns off and on with the switch. If you get a bright/dark led "L" when 13 is connected to 2 and yoou toggle the switch, then you know you have good wiring and logic levels to 2.

Yes it is toggling several times, but at least it catches the press on the button !
To improve it I would need to store the button state and check if button state is not equal to last button state or something like this no ?

The code from my post already checks with built in led, but thanks !
Always cool to "see" what is happening

You could, but I like to go as simple as possible :wink: .

Try replacing the while code line with this:

while (Switch == LOW){ digitalRead(switch); delay(1); }

Oops-- I didn't click through to the code. If LED_BUILTIN is left in default, then the pin is tri-state, and the LED is driven by an op-amp and makes a good logic probe that works without code.