Using limit switch

Hi everyone, I am working on a project which is using limit switches on a robot car to detect collision. I was wondering how should i connect the limit switched to arduino uno board without causing any short circuit which will fried the board. The limit switches must be able to send signal to the board when there are collision and the DC motors will move to the opposite direction of the collision. Which pin is more suitable for this case NO or NC pin ?

It does not matter. One will move from low to high upon collision and the other from high to low.

What matters is knowing how you wired it and doing the right thing in code.

The easiest way is to have it NO and wire to ground on one side and the pin defined with pinMode() as INPUT_PULLUP on the other side. This way you don’t need an extra resistor.

Then you manage that just like a button (which goes LOW upon being pressed)

2 Likes

Thanks for your reply. I tried to connect the limit switch COM to ground and NO to 5V and A4. Below shows a simple code i made to try the function :

#include <AFMotor.h>

AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
AF_DCMotor motor3(3);
AF_DCMotor motor4(4);

byte limitSwitch = A4;
void setup(){
pinMode (limitSwitch, INPUT_PULLUP);
motor1.setSpeed(112);
motor2.setSpeed(112);
motor3.setSpeed(112);
motor4.setSpeed(112);

}
void loop(){
if (digitalRead(limitSwitch) == LOW){
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);

}
else {
motor1.run(BACKWARD);
motor4.run(BACKWARD);
motor2.run(BACKWARD);
motor3.run(BACKWARD);
}
}

The motor runs forward while the switch isn't press but when the switch was pressed it became a short circuit and the motor isn't moving backwards which isn't the result expected.

Hi,
Welcome to the forum.
Please read the post at the start of any forum , entitled "How to use this Forum".

That will short your power supply out when the NO becomes closed.
You need;

A4 to COM
GND to NO
no connection to NC.

The pull_ups will provide the 5V.
Your A4 will be at 5V and go to GND when the limit switch is closed.

Tom.... :grinning: :+1: :coffee: :australia:

1 Like

I tried by following your suggestions, it worked. Thanks for the explanation :grin:.