Switch PIN, Also Trigger Other Pin in Arduino

SOLVE

The problem is the wire.

My wire for switch is jumper cable, and each other closer (touch) to each other cable. I try to separate, and works. There is no other pin will be trigger.


I would like to know.

I have Pin 2 and 3 as INPUT_PULLUP. I have switch to each other pin. Why when I press switch at PIN 2. SOMETIMES PIN 3 also getting input. Why?

How to fix this? Is it common problem with arduino?

My program is basic. It should be nothing wrong with program.

UPDATE

int led1 = 1;
int led2 = 2;
int led3 = 3;
int led4 = 5;

int switch1 = 6;
int switch2 = 7;
int switch3 = 8;
int switch4 = 9;

int led = 13;

//int counter = 0;

void setup()
{
  // put your setup code here, to run once
      Serial.begin(9600);
  pinMode(led1, OUTPUT);
      pinMode(led2, OUTPUT);
      pinMode(led3, OUTPUT);
      pinMode(led4, OUTPUT);
      pinMode(switch1, INPUT_PULLUP);
      pinMode(switch2, INPUT_PULLUP);
      pinMode(switch3, INPUT_PULLUP);
      pinMode(switch4, INPUT_PULLUP);
      pinMode(led, OUTPUT);
}  
void loop()
{

if (digitalRead(switch1) == 0){
  digitalWrite(led1, HIGH);
  }

 if (digitalRead(switch1) == 1){
  digitalWrite(led1, LOW);
  } 
  
if (digitalRead(switch2) == 0){
  digitalWrite(led2, HIGH);
  }

 if (digitalRead(switch2) == 1){
  digitalWrite(led2, LOW);
  } 

  
if (digitalRead(switch3) == 0){
  digitalWrite(led3, HIGH);
  }

 if (digitalRead(switch3) == 1){
  digitalWrite(led3, LOW);
  } 

  
if (digitalRead(switch4) == 0){
  digitalWrite(led4, HIGH);
  }

 if (digitalRead(switch4) == 1){
  digitalWrite(led4, LOW);
  } 

digitalWrite(led, HIGH);

delay(1000);

digitalWrite(led, LOW);

}

SCHEMATIC (Example only one input, the rest (other 3 input) is similar schematic.

My input only like that, simple schematic.

For output only goes to led, resistor, and ground.

My program is basic.

And invisible. (it should be C/C++. not BASIC)
As is your schematic.

AWOL:
And invisible. (it should be C/C++. not BASIC)
As is your schematic.

I mean C/C++ as Arduino IDE.

My schematic is just ground->switch->Arduino PIN. Since this is INPUT_PULLUP.

At least, do you have any experience with this? I don't say often, but sometimes the other pin also trigger.

I found the same situation http://www.instructables.com/id/Arduino-Push-Switch-Debouncing-Interrupts/

On the website, He said, "The problem with this setup was when the button was pressed the interrupt was being called multiple times and even toggling other buttons. Why was this happening? It is caused from a switch bouncing feedback."

Is there any solution without modifying the hardware?

But your code says you have a led connected to pin2...

And some tips, the IDE tries to make your code more readable by indenting it. I still don't know how people get it to break this bad but press chtr+t in the IDE and see how it looks wayyyyyy better.

Read up on arrays :wink:

septillion:
But your code says you have a led connected to pin2...

And some tips, the IDE tries to make your code more readable by indenting it. I still don't know how people get it to break this bad but press chtr+t in the IDE and see how it looks wayyyyyy better.

Read up on arrays :wink:

Thanks for Ctrl+T.

Have you heard this behavior before? I just switch pin 2. Everything is ok, yet sometimes other pin also trigger as pin 2. Did you have this weird behavior?

Personally, I would never use the serial Tx pin as a LED output, if I could avoid it.

if (digitalRead(switch1) == 0){
  digitalWrite(led1, HIGH);
  }

 if (digitalRead(switch1) == 1){
  digitalWrite(led1, LOW);
  }

aka

digitalWrite (led1, 1 - digitalRead (switch1));

And arrays. Oh yes, arrays.

Problem is, you tell us something that's not back up by your code at all. You talk about a switch at pin 2 and pin 3 but you declare them as outputs... Thus so far as I know the program that causes the problem isn't the program you posted... So let's start over, post the correct code and a correct description of the problem.

Btw, things like

if (digitalRead(switch1) == 0)
...
 if (digitalRead(switch1) == 1)

Are redundant. If you know it's not 0 you already know it's a 1. So you can just use a else there :wink:

Or simpler

digitalWrite(led1, !digitalRead(switch1));

uconalpukat:
At least, do you have any experience with this? I don't say often, but sometimes the other pin also trigger.

I found the same situation http://www.instructables.com/id/Arduino-Push-Switch-Debouncing-Interrupts/

I have great difficulty believing that there is a relation between your problem and bouncing. A bouncing switch will not suddenly cause the input on another pin to change.

You're not using interrupts either as far as I can see.

I would check the wiring; you might have a bad contact somewhere.

uconalpukat:
I would like to know.

I have Pin 2 and 3 as INPUT_PULLUP. I have switch to each other pin. Why when I press switch at PIN 2. SOMETIMES PIN 3 also getting input. Why?

Because the layout is poor? The internal pull-ups are weak and not necessarily sufficient to prevent
noise pickup if you run long cables or have big messy loops of hook-up wire everywhere. If the switches
are on the other end of a cable try 10k or 4k7 physical pull-up resistors instead. The noise is most likely
coming from the LEDs which carry the more current and thus induce more noise into nearby wires.

AWOL:
Personally, I would never use the serial Tx pin as a LED output, if I could avoid it.

if (digitalRead(switch1) == 0){

digitalWrite(led1, HIGH);
 }

if (digitalRead(switch1) == 1){
 digitalWrite(led1, LOW);
 }




aka 


digitalWrite (led1, 1 - digitalRead (switch1));




And arrays. Oh yes, arrays.

septillion:
Problem is, you tell us something that's not back up by your code at all. You talk about a switch at pin 2 and pin 3 but you declare them as outputs... Thus so far as I know the program that causes the problem isn't the program you posted... So let's start over, post the correct code and a correct description of the problem.

Btw, things like

if (digitalRead(switch1) == 0)

...
if (digitalRead(switch1) == 1)



Are redundant. If you know it's not 0 you already know it's a 1. So you can just use a else there ;)

Or simpler


digitalWrite(led1, !digitalRead(switch1));

Hi, thanks for the comment and syntax for making a good program. I am really appreciate and I am so sorry if my code is different. And yes, when we use serial.begin, pin 0 and 1 cannot be used. The program that I gave to here is not correct.

sterretje:
I have great difficulty believing that there is a relation between your problem and bouncing. A bouncing switch will not suddenly cause the input on another pin to change.

You're not using interrupts either as far as I can see.

I would check the wiring; you might have a bad contact somewhere.

MarkT:
Because the layout is poor? The internal pull-ups are weak and not necessarily sufficient to prevent
noise pickup if you run long cables or have big messy loops of hook-up wire everywhere. If the switches
are on the other end of a cable try 10k or 4k7 physical pull-up resistors instead. The noise is most likely
coming from the LEDs which carry the more current and thus induce more noise into nearby wires.

My arduino is like this:

Switch Pin 6 -> Out Led Pin 1
Switch Pin 7 -> Out Led Pin 2
Switch Pin 8 -> Out Led Pin 3
Switch Pin 9 -> Out Led Pin 5

Switching pin 6 on/off 10 times OK. When I switch after that, pin 7 as output also get trigger. Another case, only 3-4 time on/off other pin 8 also trigger. I just want to say this is RANDOM.

What else make this situation happen?

  1. Bad contact ?
  2. Long cable ?

Guys, Do you have any experience with Arduino with on/off without having this issue?

Thank you so much.

Switching pin 2 on/off 10 times OK.

Pin 2 is an output.

This has already been mentioned.

You use Serial which uses pins 0 and 1 so don't use pin 1 for anything else. Clear?

uconalpukat:
What else make this situation happen?

  1. Bad contact ?
  2. Long cable ?

Guys, Do you have any experience with Arduino with on/off without having this issue?

Thank you so much.

This is where many would get out their meter and start checking wiring continuity from pin to pin.

Are you using jumpers more than 20 cm (8 inch) long?

AWOL:
Pin 2 is an output.

This has already been mentioned.

I changed my post. :slight_smile:

GoForSmoke:
You use Serial which uses pins 0 and 1 so don't use pin 1 for anything else. Clear?

Yeah, I know this.

My concern is everything is ok with on/off switch. But, sometimes (random) other pin also trigger. Does Arduino need additional hardware (capacitor, etc) to do this simple on/off? My circuit only like this

GoForSmoke:
This is where many would get out their meter and start checking wiring continuity from pin to pin.

Are you using jumpers more than 20 cm (8 inch) long?

Already checking pin by pin and is not connected each other. Yeah, I am using cable jumper more than 20 cm. Is it a problem?

I have lost track of what you have changed in the code. Can you please post the latest version that you are testing in a new reply. You keep mentioning using pins 2 and 3 as inputs but that does not match the code in your original post.

UKHeliBob:
I have lost track of what you have changed in the code. Can you please post the latest version that you are testing in a new reply. You keep mentioning using pins 2 and 3 as inputs but that does not match the code in your original post.

int led1 = 1;
int led2 = 2;
int led3 = 3;
int led4 = 5;

int switch1 = 6;
int switch2 = 7;
int switch3 = 8;
int switch4 = 9;

int led = 13;

//int counter = 0;

void setup()
{
	  pinMode(led1, OUTPUT);
      pinMode(led2, OUTPUT);
      pinMode(led3, OUTPUT);
      pinMode(led4, OUTPUT);
      pinMode(switch1, INPUT_PULLUP);
      pinMode(switch2, INPUT_PULLUP);
      pinMode(switch3, INPUT_PULLUP);
      pinMode(switch4, INPUT_PULLUP);
      pinMode(led, OUTPUT);
} 
void loop()
{

if (digitalRead(switch1) == 0){
  digitalWrite(led1, HIGH);
  }

 if (digitalRead(switch1) == 1){
  digitalWrite(led1, LOW);
  }
 
if (digitalRead(switch2) == 0){
  digitalWrite(led2, HIGH);
  }

 if (digitalRead(switch2) == 1){
  digitalWrite(led2, LOW);
  }

 
if (digitalRead(switch3) == 0){
  digitalWrite(led3, HIGH);
  }

 if (digitalRead(switch3) == 1){
  digitalWrite(led3, LOW);
  }

 
if (digitalRead(switch4) == 0){
  digitalWrite(led4, HIGH);
  }

 if (digitalRead(switch4) == 1){
  digitalWrite(led4, LOW);
  }

digitalWrite(led, HIGH);

delay(1000);

digitalWrite(led, LOW);

}

Everything is work. But, sometimes when I push switch 1 for example other switch also get trigger. Do you have any exp for this one? I think this is hardware problem not the code.

If you indeed have long cables then yeay, the long cables. They can pick up everything, also switching of another pin. To fix that, use a beefier pullup then the build in pull up, like 1K.

Also, you can filter the input so a bad timed short pulse will not turn on the output (because of the delay it will be turned on wayyyy longer then the pulse might is). A simple button debounce library like Bounce2 does it for you.

You can also fix it in hardware by using a RC filter for each input but why go to the hassle of hardware if you can fix it in software :wink:

Can you please clarify how the switches are wired because your previous examples showed you using pin 2 and mentioned pin 3 but as has been pointed out nether of these are inputs in your code.

septillion:
If you indeed have long cables then yeay, the long cables. They can pick up everything, also switching of another pin. To fix that, use a beefier pullup then the build in pull up, like 1K.

Also, you can filter the input so a bad timed short pulse will not turn on the output (because of the delay it will be turned on wayyyy longer then the pulse might is). A simple button debounce library like Bounce2 does it for you.

You can also fix it in hardware by using a RC filter for each input but why go to the hassle of hardware if you can fix it in software :wink:

yeah, that is what i think. Use some delay to make sure the switch is pressed. Also, it will turn on way longer than before.

UKHeliBob:
Can you please clarify how the switches are wired because your previous examples showed you using pin 2 and mentioned pin 3 but as has been pointed out nether of these are inputs in your code.

This wiring only for one switch pin. and the rest pin is similar.
For output, each output going to led, resistor and ground.

PIN 2 is mistake. It should be PIN 6,7,8, and 9.

And if you're a good programmer, don't add that delay with delay() :wink:

If you want a easy fix, go with a library.
If you want to learn why, read up on it, exam a library, make tests and then use a library to fix it.