Forward, Reverse, Stop Functions using Relays and Limit Switches

Thread Starter

S B

Joined Jul 3, 2018
5
Hi Community

This project uses :
- Uno
-2 Relays to Control 1 Motor with Forward, Reverse and Stop controls.
-LimitSwitch1 should stop the motor at the maximum forward position and wait for Reverse command.
-LimitSwitch2 should stop the motor at the maximum Reverse position and wait for Forward command.

i have both Limit switches wired with 10k resistors to Ground.
I cannot get this to work successfully.
Any input would be appreciated.

Code:
const byte RELAY1 = 2;
const byte RELAY2 = 3;

const byte LimitSwitch1 = A1;
const byte LimitSwitch2 = A2;

const byte State_Idle = 0;
const byte State_Forward = 1;
const byte State_Reverse = 2;
byte currentState;

void setup(){
    pinMode(RELAY1,OUTPUT);
    pinMode(RELAY2,OUTPUT);

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

    Serial.begin(9600);
    currentState = 0xFF; // Force a change to enter the idle state
}

void loop(){
    // Check if we need to change state
    byte nextState = currentState;
    switch (currentState)
    {
    case State_Idle:
        { // If we received a valid command, start the operation
            if(Serial.available() > 0)
            {
                switch (Serial.read())
                {
                case 'A':
                    nextState = State_Forward;
                    break;
                case 'B':
                    nextState = State_Reverse;
                    break;
                default:
                    break;
                }
            }
        }
        break;
    case State_Forward:
        if(digitalRead(LimitSwitch2))
            nextState = State_Idle;
        break;
    case State_Reverse:
        if(digitalRead(LimitSwitch1))
            nextState = State_Idle;
        break;
    default:
        nextState = State_Idle;
        break;
    }

    // If there was a change, switch state and trigger enter condition
    if (nextState != currentState)
    {
        currentState = nextState; // Move in the new state
        switch (currentState)
        { // Entering the new state
        case State_Idle:
            // Switch both Relays(Motor) OFF
            digitalWrite(RELAY1, LOW);
            digitalWrite(RELAY2, LOW);
            break;
        case State_Forward:
            // Move Forward
            digitalWrite(RELAY1, LOW);
            digitalWrite(RELAY2, HIGH);
            break;
        case State_Reverse:
            // Move Reverse
            digitalWrite(RELAY1, HIGH);
            digitalWrite(RELAY2, LOW);
            break;
        }
    }
}
 

shteii01

Joined Feb 19, 2010
4,644
Which part is not working?

Normal people do troubleshooting by breaking the program into "modules" and then testing each module individually, then combining the modules one at a time to make sure they work together.
 

MaxHeadRoom

Joined Jul 18, 2013
30,609
So it is not a system just using relays and limit switches as the title says, you also have a processor.
What is the reason for including the μp? When it looks like a simple relay logic circuit?
Max.
 
Your going about doing it the wrong way, Let the limit switches do their thing. Look in this thread. https://forum.allaboutcircuits.com/...es-for-greenhouse-roof-window-control.134958/ Find the file motor-limits.pdf

Basically you can tell if the relay is at a limit or if the motor is moving.
Second the relays implement dynamic braking.
You have a FWD and REV signal. Both not applied or both applied result in the motor braked.
 

Thread Starter

S B

Joined Jul 3, 2018
5
Which part is not working?

Normal people do troubleshooting by breaking the program into "modules" and then testing each module individually, then combining the modules one at a time to make sure they work together.
My apologies i should have been more descriptive. When limit switch is depressed, the Forward and Reverse functions do not work. I will update in more detail tomorrow after further tests.

I have the relays functioning with Forward and Reverse as they should without the limit switches. My difficulty is understanding and implementing State machine into the code to detect that the motor is Idle and waiting for next command.
 

Thread Starter

S B

Joined Jul 3, 2018
5
So it is not a system just using relays and limit switches as the title says, you also have a processor.
What is the reason for including the μp? When it looks like a simple relay logic circuit?
Max.
@MaxHeadRoo - Is there a way to change subject? By “μp” do you mean my Uno controller?
 

Thread Starter

S B

Joined Jul 3, 2018
5
@danadak - Thanks, These articles are helping me understand why with 1 press on switch the serial monitor showed it as 4 presses. (Note this was with a different code but may apply to this project as well)
 

Thread Starter

S B

Joined Jul 3, 2018
5
Your going about doing it the wrong way, Let the limit switches do their thing. Look in this thread. https://forum.allaboutcircuits.com/...es-for-greenhouse-roof-window-control.134958/ Find the file motor-limits.pdf

Basically you can tell if the relay is at a limit or if the motor is moving.
Second the relays implement dynamic braking.
You have a FWD and REV signal. Both not applied or both applied result in the motor braked.
@KeepItSimpleStupid - I have read mentioned motor-limits.pdf as per your advice, I am slowly trying to process info.

“Both not applied or both applied result in the motor braked. “ --- Does this mean the if both relays are on at the same time(both signals HIGH) that the motor brakes. Could both HIGH signal to motor cause damage?
 
Both not applied or both applied result in the motor braked. “ --- Does this mean the if both relays are on at the same time(both signals HIGH) that the motor brakes. Could both HIGH signal to motor cause damage?
You didn't really mention DC or the voltage, so I just guessed. The idea is that the limit switches have to be rated for the coil current only. That sometimes can be difficult. Automotive relays are about 200 mA if my memory is correct.

This is a modified version of typical automotive door locks or power window type circuits. Suffice to say, relays are on. You can change the logic.

With the relays not energized, the motor is shorted. Thus when you turn it off, the motor is acting as a generator into a short and the motor stops quickly without overrun.

When they are both on, both sides of the motor are connected to the positive supply, so still 0V across the motor. When they are both off, both sides of the motor are attached to ground and still 0V across the motor. No damage.

Warning (automotive relays): There is a suggested terminal to use for + on the coil. Some relays have suppression diodes built in. They don't always have the same pin out.

PS: You can change the subject. There are some options at the beginning of the thread you started.
 
Top