Push button is triggered by electric switch

Hello,

I am not electronics professional but rather enthusiast, so do not judge if the problem is so obvious :slight_smile:

I have an arduino UNO that has has a push up button and a led, and the design is simple;
when I push the button, it will change the state of the led, meaning if the led is off is goes on, and vice versa. So far so good, I am quite happy.
To power the arduino I am using an adapter that changes the AC current of 240V to 12V DC, and the source of the power comes from the electrical switch that I use to power ON/OFF an electrical light (this is because I have no source of power nearby - no power socket, and I did not wanted to use batteries to power the arduino), but of course I am using the source of electricity before the switch, have a look at the picture below:

image

the code:

bool pressed = 0;
void setup() {
    pinMode(2, INPUT_PULLUP); //button
    pinMode(5, OUTPUT); //led
}

void loop() {
            
      int buttonValue = digitalRead(2);
      if (buttonValue == LOW){
      			pressed = !pressed;
      			digitalWrite(5, pressed);
            delay(500);   
      }
}

To conclude, my problem is that the button is triggered automatically sometimes (only sometimes - not always) when I switch the light ON/OFF, which it should not, since I am not directly depending on the electrical switch.
How come the switch triggers the button on arduino, when it is not directly linked, and I am also using the pull_up resistor macro for the button?
Can someone say something about this issue, or maybe not :slight_smile:

Your switch always produces a spark inside the switch. That spark produces a noise signal that is carried by your long wires to the Arduino switch wires. Reduce the value of your pull up resistor, by perhaps half the current value.

Thank you for your reply. But still I do not understand what do you mean by 'reduce the value of pull up resistor by half', how should I reduce it?
Do you mean I should use a resistor element (instead of pull up macro for resistor) with some concrete Ohm value, if yes what value that resistor should be?
Thank you in advance.

Yes, perhaps around 5k Ohms.

It would help if you draw a diagram of the "Button and LED project"

I can confirm that it works fine now, thank you very much

Well I do not have my original design anymore, now I am using the design from the following tutorial:

But still thank you for you effort in helping me.

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