Can I use a limit switch to stop and start a DC motor, then have it return to the loop and run the initial programme?
Your description is not quite clear but I'm sure you can. How do you plan to release the limit switch once the motor is stopped?
What do you mean by 'the initial program'?
By sending the motor in the opposite direction.
Basically I have a rotating platform that needs to rotate in a set sequence.
My problem is that I cannot afford to let the platform rotate outside of a 90' arc, hence the need to stop it with a limit switch or "Hall Sensor".
If the platform does hit the limit I need it to either go to a home position and start again or reverse direction and continue it's previous sequence.
I know I could use a stepper motor but I cannot get on with enough power in my timeframe.
You will need to implement the "stop going clockwise" and "stop going anticlockwise" in software
you may need to implement this as a two stage process with limit switches for "decelerate" and "stop" - just as is done with a lift (elevator). Depending on your requirements you may also need to add a brake to hold position when the motor is stopped.
None of this is difficult to program.
This might be a framework that you can base your sketch on. It uses a finite state machine to go between the states; you can add more states depending on your needs.
#define ISPRESSED LOW
const uint8_t pinBtnStart = 2;
const uint8_t pinLimitHome = 3;
const uint8_t pinLimit90 = 4;
void setup() {
Serial.begin(115200);
while (!Serial)
;
// wire switches and buttonsbetween pin and ground
pinMode(pinBtnStart, INPUT_PULLUP);
pinMode(pinLimitHome, INPUT_PULLUP);
pinMode(pinLimit90, INPUT_PULLUP);
}
enum class STATES {
WAIT4START, // wait for start button to be pressed
CW, // rotate clockwise to 90 degrees
CCW // rotate counter clockwise to home position
};
void loop() {
static STATES currentState = STATES::WAIT4START;
switch (currentState) {
case STATES::WAIT4START:
if (digitalRead(pinBtnStart) == ISPRESSED) {
Serial.println(F("Switching to CW"));
currentState = STATES::CW;
}
break;
case STATES::CW:
motorCW();
if (digitalRead(pinLimit90) == ISPRESSED) {
motorOff();
Serial.println(F("Switching to CCW"));
currentState = STATES::CCW;
}
break;
case STATES::CCW:
motorCCW();
if (digitalRead(pinLimit90) == ISPRESSED) {
motorOff();
Serial.println(F("Switching to START"));
currentState = STATES::WAIT4START;
}
break;
}
}
void motorCW() {
}
void motorCCW() {
}
void mototOff()
{
}
It’s usually worth hard wiring limit switches to break the motor supply rather than relying on software .
Google motors control with limit switches
I would have a hard wired limit switch for the absolute max and you could have another just short of this to trigger software change in direction. That way if your software fails you still cut power before things destroy themselves
If this is for a DC motor with H-bridge for reversal,
then you could use limit switches with diodes across the contacts.
The diode only allows the motor to move away from the end switch.
Limit switches with diodes is common practice for linear actuators.
Leo..