Multiple Limit Switches Problem

Hi everyone,
I am using an Arduino MEGA 2560 for a project that involve the need for 6 limit switches.
Unfortunately I can’t get it to work.
This is the code I’m using to check that they work properly, but for some reason only Limit switch 4-6 responding correctly. Limits 1-3 kind of doing what they want. They sometimes write ‘1’ even when not pressed. Other times they go between 0 and 1.

int limit1 = 22;
int limit2 = 23;
int limit3 = 24;
int limit4 = 25;
int limit5 = 26;
int limit6 = 33;


void setup() {
  pinMode(limit1, INPUT);
  pinMode(limit2, INPUT);
  pinMode(limit3, INPUT);
  pinMode(limit4, INPUT);
  pinMode(limit5, INPUT);
  pinMode(limit6, INPUT);
  Serial.begin(9600);
}

void loop() {
  Serial.println(digitalRead(limit1));
  Serial.println(",");
  Serial.println(digitalRead(limit2));
  Serial.println(",");
  Serial.println(digitalRead(limit3));
  Serial.println(",");
  Serial.println(digitalRead(limit4));
  Serial.println(",");
  Serial.println(digitalRead(limit5));
  Serial.println(",");
  Serial.println(digitalRead(limit6));
  Serial.print(".");
  delay(1000);
}

I have attached a diagram of the connections I made.
Hope you can help me,
Thanks!

what happens if you exchange the position of the weird behaving limit switches? same pins still presenting the pb or is the pb moving with the switch?

They sometimes write '1' even when not pressed

Use INPUT_PULLUP instead of INPUT in the pinMode()s and change the wiring to take the input pins LOW when pressed..

Does it work any better ?

J-M-L:
what happens if you exchange the position of the weird behaving limit switches? same pins still presenting the pb or is the pb moving with the switch?

Hi!
Thanks for the quick answer!! I have checked every single switch by itself and they all work fine alone. Also tried to connect them to other ports to see maybe that was the problem but it does the same... Very weird!!
When I plug the Arduino and start running the program it prints the following:
1,1,1,0,0,0.
Without switches 1-3 even pressed..
Hope you can help

you have NC and NO output it seems - have you really check you wired them the right way?

UKHeliBob:
Use INPUT_PULLUP instead of INPUT in the pinMode()s and change the wiring to take the input pins LOW when pressed..

Does it work any better ?

Can you explain what to change the wiring to? And what does the INPUT_PULLUP mode does? Im not familiar with that one...

J-M-L:
you have NC and NO output it seems - have you really check you wired them the right way?

Yes, all connected properly, like the sketch..

Could it be that your breadboard has a divided power rail, that you did not connect?

1 Like

Whandall:
Could it be that your breadboard has a divided power rail, that you did not connect?

It's a regular breadboard.. no divided power rail or anything else...

what happens when you exchange the switches positions ?

J-M-L:
what happens when you exchange the switches positions ?

Does the same, even tried to use other limit switches. Tried also using an Uno board, works fine with 3, does the same with 6.

NurSharlin:
Hi!
Thanks for the quick answer!! I have checked every single switch by itself and they all work fine alone. Also tried to connect them to other ports to see maybe that was the problem but it does the same... Very weird!!
When I plug the Arduino and start running the program it prints the following:
1,1,1,0,0,0.
Without switches 1-3 even pressed..
Hope you can help

Your switches are operated in pinMode INPUT instead using INPUT_PULLUP.
How did you make the connections? Wiring plan or schematics?

When using pinMode INPUT all your button circuits will need either pull-up or pull.down resistors to provide other than "random (floating) signal".

What do you have: pull-up or pull-down resistors?

jurs:
Your switches are operated in pinMode INPUT instead using INPUT_PULLUP.
How did you make the connections? Wiring plan or schematics?

When using pinMode INPUT all your button circuits will need either pull-up or pull.down resistors to provide other than "random (floating) signal".

What do you have: pull-up or pull-down resistors?

Im not sure what the difference between the two is.. And about the connections, I have attached a wiring plan to the original message

(deleted)

spycatcher2k:
Why not just follow the advice given? When others try to help, it's courtesy to follow their example and report the findings.

I can see his point intellectually - if he wired the way he said, he is doing a pulldown instead of a pull-up that would not change anything.

BUT — that being said as we are looking for a weird bug, using the INPUT_PULLUP would be indeed a good idea to ensure that the resistors are not faulty (and that OP used are actually 4.7kΩ and not 4.7Ω or something low...)

(deleted)

NurSharlin:
Im not sure what the difference between the two is.. And about the connections, I have attached a wiring plan to the original message

I've had a look. Connections seem to be OK for pull-down resistor circuits.

But I'm wondering about the resistor values.
While I see the value should be 4.7K = 4700 Ohm, the resistors color code look like red-red-brown, which would be 220 Ohm, not 4700 Ohms.

What's the actual resistor value used? 220 Ohms or 4700 Ohms?
Values in the range 330 Ohm up to 10K (10000) should be OK.

One other thing to check... If you got a voltmeter in hand, try to read the voltage from input pin to ground.
That could guide you to know of it is hardware or software related...

jurs:
I've had a look. Connections seem to be OK for pull-down resistor circuits.

But I'm wondering about the resistor values.
While I see the value should be 4.7K = 4700 Ohm, the resistors color code look like red-red-brown, which would be 220 Ohm, not 4700 Ohms.

What's the actual resistor value used? 220 Ohms or 4700 Ohms?
Values in the range 330 Ohm up to 10K (10000) should be OK.

Hi, the sketch was made with Fritzing. The resistor module in the software looks like a 220 ohm, maybe there is a way to change that but I dont know.
On the real circuits I have tried both 10k ohm and 4.7k ohm, both acted the same.

nitrof:
One other thing to check... If you got a voltmeter in hand, try to read the voltage from input pin to ground.
That could guide you to know of it is hardware or software related...

Sounds like a good idea, I'll check that when I be near the circuit.

And for the guy who suggested the INPUT_PULLUP method and said why dont I do what said, as I already wrote I am not familiar with this function and how the wiring should go, it will be great if you can help...

Thx

If you use

pinMode(pinNumber, INPUT_PULLUP);

The pin will be held HIGH by the internal pullup resistor when the switch is not closed. This eliminates an external resistor for each pin, thus simplifying the wiring, and it also removes any doubt about the value of the resistor.

With the pin held HIGH by the resistor you need to wire the switch so that the pin is taken to GND when the switch is closed. The test for whether the switch is closed thus becomes

if (digitalRead(pinNumber) == LOW)
{
  //the switch is closed so execute the code here.
}