Electrical – Using limit switches to reverse circuit polarity

arduinodesignpolarityrelay

I am designing a linear translation stage that will be driven with a DC motor. When the circuit is powered the DC motor will start turning in either direction, which will move the linear stage towards one end. At that end the stage will activate limit switch #1 and the motor will turn the opposite direction until the stage activates limit switch #2. When limit switch #2 is activated the motor will turn in the original direction and send the stage back towards limit switch #1. This process will repeat until the circuit is powered off.

Does anyone have any ideas for how to build a circuit that reverses polarity to the motor using only a momentary pulse from a limit switch? I have built this circuit: Polarity Switching Circuit

The circuit works great for its intended purpose, but it does not work for my needs because I need to be able to use momentary switches (limit switches). The circuit shown here will stop traveling as soon as the switch is released.

I have also tried to wire the circuit above through an Arduino. I used the momentary limit switches as inputs for interrupts. I had the same issue of the motor only moving while a switch was being actively closed. Here is the sketch that I came up with:

const int interruptPin = 2;

const int interruptPin2 = 3;

const int cwPin = 10;

const int ccwPin = 11;



void setup()
{

  pinMode(cwPin, OUTPUT);

  pinMode(ccwPin, OUTPUT);

  pinMode(interruptPin, INPUT);

  pinMode(interruptPin2, INPUT);

  attachInterrupt(0, goCw, RISING);

  attachInterrupt(1, goCcw, RISING);
}


void loop() 
{

digitalWrite(cwPin, HIGH);
}


void goCw()
{
 digitalWrite(cwPin, HIGH);
 digitalWrite(ccwPin, LOW);
}


void goCcw()
{
digitalWrite(cwPin, LOW);
digitalWrite(ccwPin, HIGH);
}

I would appreciate any advice that anyone has on this issue. I am open to using any method to achieve the desired outcome. I attempted to use the Arduino because I thought that it would solve my problem, but I am not against eliminating it if there is a better way.

Best Answer

As for the arduino you can create a variable, say 'directionstatus' (yes long name abbreviate as required)

This is not code but rather the logic of operation - Create an IF statement in the loop to do the following:

If switch pressed
    If directionstatus = 1
    direction status =0
    ***Insert code to initiate one direction here***
    Else 
    directionstatus = 1
    ***Insert code to initiate other direction here*** 
    EndIf
EndIf

Then you can use either relays or mosfets or otherwise to operate the motor, and initiate the drive. If you want further elaboration please say.

It is possible to do without an arduino but you seem to have one already there and useful and potentially ready to go.

Added benefit of using a uC is if you know the speed and implement some timings etc you could control travel, and if you move to stepper motors later on (not sure if your application requires) you can have very precise control quite easily - but for now I would imagine an If in void_loop that operates when button is pressed would suffice.

... P.S.

Just thought - please have a look at this as this might be upset your code later on as each time the switch is initiated it may bounce, plus you might find it triggers when it actuates and triggers when you leave (in which case update code to ignore every second instruction) https://www.arduino.cc/en/Tutorial/Debounce