Problem : Trigger limit switch to stop DC motor

Good Afternoon,
I want to make a project about automatic gate and garage door via Android app and connected by HC-05 bluetooth module.
Each of the gate and garage door have two limit switches(located at each end of travel) to trigger the DC motor to stop.
I have made some code and do researches, but the DC motor will only stop when I pressed it, if I release it again it will start automatically. I want to make it stop until the bluetooth module send the code again.
I have input the pull-down resistor into the limit switch.

Can anybody please help me?
Here's my current code, thank you

// Motor for Gate (MotorA)
int IN1 = 2;
int IN2 = 3;
int EnableMotorA = 9;

// Motor for Garage Door (MotorB)
int IN3 = 4;
int IN4 = 5;
int EnableMotorB = 10;

const int LimitSwitch1 = A1;
const int LimitSwitch2 = A2;
const int LimitSwitch3 = A3;
const int LimitSwitch4 = A4;

int state;


void setup(){
  pinMode(IN1,OUTPUT);
  pinMode(IN2,OUTPUT);
  pinMode(EnableMotorA,OUTPUT);
  
  pinMode(IN3,OUTPUT);
  pinMode(IN4,OUTPUT);
  pinMode(EnableMotorB,OUTPUT);

  pinMode(LimitSwitch1, INPUT);
  pinMode(LimitSwitch2, INPUT);
  pinMode(LimitSwitch3, INPUT);
  pinMode(LimitSwitch4, INPUT);

  digitalWrite(LimitSwitch1, LOW);
  digitalWrite(LimitSwitch2, LOW);
  digitalWrite(LimitSwitch3, LOW);
  digitalWrite(LimitSwitch4, LOW);
  
  Serial.begin(9600);
}

void loop(){
  if(Serial.available() > 0){
    state = Serial.read();
    }
    if(state == 'A') {
      OpenGate();
      if(digitalRead(LimitSwitch2) == HIGH){
      StopMotorA();
      }
    }
    else if(state == 'B') {
      CloseGate();
      if(digitalRead(LimitSwitch1) == HIGH){
      StopMotorA();
      }
    }
    else if(state == 'C') {
      OpenGarageDoor();
      if(digitalRead(LimitSwitch4) == HIGH){
      StopMotorB();
      }
    }
    else if(state == 'D') {
      CloseGarageDoor();
      if(digitalRead(LimitSwitch3) == HIGH){
      StopMotorB();
      }
    }
}

//******************   Motor A control   *******************
void OpenGate(){
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  analogWrite(EnableMotorA, 250);
  }
void CloseGate(){
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  analogWrite(EnableMotorA, 250);
}
void StopMotorA(){
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  analogWrite(EnableMotorA, 0);
 }
//******************   Motor B control   *******************
void OpenGarageDoor(){
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
  analogWrite(EnableMotorB, 250);
}
void CloseGarageDoor(){
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  analogWrite(EnableMotorB, 250);
}
void StopMotorB(){
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
  analogWrite(EnableMotorB, 0);
}
//**********************************************************

Hi,
Welcome to the forum.
How have you got your switches wired, with the controller you cannot leave a digital input that you are using, open and floating, it has to be connected to 0V (LOW) or 5V(HIGH).

If you are switching the input to 5V, you will need a 10K resistor between that input and gnd to make sure the input goes low when the switch is open.
If you are switching your input to gnd, then you need the 10K resistor to go to 5V.


Tom... :slight_smile:

Hello Tom,
I have wired my limit switch with the pull-down resistor, the common one is connected to one analog pin of Arduino and to gnd by a 10k resistor, and the normally open to 5v.
But it still doesn't work as I wish, maybe its the code?

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.. :slight_smile:

here's my circuit diagram, but I have modified the limit switch connection as I told before.
And I want to focus on the DC motors code first, so I have not input the servo and buzzer as seen in the circuit.

Hi,


Can you draw a schematic please, this fritzy tells us very little.
What are the motors?
What are the limit switches.
What is the servo.
Are you really powering everything off a 9V transistor battery?
Are you really powering the servo through the UNO?
Where are you powering the motor?

Thanks.. Tom.. :slight_smile: