Problem with Motor DC and limit switch

Hi everyone, I'm new to this forum, in recent days I have had to deal with a cart driven by a normal motor DC 5V, to stop the trolley in an exact position I used a limit switch or mechanical contact such as those found in CD players, as the software trolley stops regularly but when I go to give the command to start it remains stationary perpetually, 'cause the switch is pressed. someone can 'help me? :slightly_frowning_face:
thank you and good day. :wink:

Does this trolley simply travel back and forth between two locations? - Scotty

You can either have a limit switch that works through software or you can control the motor with two relays. If one the relays is blocked by a limit switch the other one can still operate and reverse the motor.

motor switch.JPG

hello Scotty, it actually travels between two locations when comes to the first location, that 'the home established by the limit switch stops without' share. :confused:

Sounds like an issue with your code - posting it would help.

Does the motor need to change rotation directions when it is stopped by the limit switch, or does it need to continue rotating in the same direction?

zoomkat:
Does the motor need to change rotation directions when it is stopped by the limit switch, or does it need to continue rotating in the same direction?

Reads to me like the latter.

I'm sure you can take care of that in code. Let's assume the trolley stops because you read switchA (the limit switch nudged by the trolley) as open. At start time, you have a pushbutton called switchB. In your code you read switchB: if it's pushed, read switchA... if it's open, move the trolley.

(Unless of course your limit switch disconnects the trolley's power: then you're screwed.)

You might want a state machine.

Hi,After being stopped by the switch the truck has to change direction

this is the code:

        motor[motorId] -> start(forward, power);  // starts the cart
        encoder[encoderId] -> countCycles(cycles, timeout);    // the optical encoder counts cycles
        motor[motorId] -> stop();

        if (digitalRead(switchPin) == LOW)  // the optical encoder counts ciclise the switch and 'down the Carrelo stops, unfortunately perennially
        {
          motor[motorId] -> stop();
          Serial.println("HOME");
          }
      }
      break;

You should not have the switch directly stop the motor.

The Arduino should check the switch and if the cart is moving "left" (say) when the switch is pressed the Arduino will record the fact that the switch is pressed, will stop the motor and will change the direction to "right" ready for the next move.

And, because the switch is not directly causing the motor to stop, the Arduino can easily tell the cart to move to the right.

And, next time, post a complete program, not just a snippet.

...R

I tried this sketch but the cart remains stationary:

while (digitalRead(switchPin) == LOW)
        {
          motor[motorId] -> stop(); // stop the  motor
          Serial.println("HOME");
break;
}
          
  motor[motorId] -> start(forward, power);
  encoder[encoderId] -> countCycles(cycles, timeout);
  motor[motorId] -> stop();

Robin2:
And, next time, post a complete program, not just a snippet.

...R

If you make it hard for people to help you you will not get much help

Hi,The program complete is too long,the program is 400 lines!

If it is too large, attach it.

Hi,After being stopped by the switch the truck has to change direction

Simple electrical reversing setups using relays, limit switches and diodes.

motor-1relay.jpg

reversemotor.jpg

diego-95:
I tried this sketch but the cart remains stationary:

That is still a snippet.

I will look at the problem when you post the full program.

...R

Hi zoomkat,I would not 'use the relay.
the trolley stops perpetually on limit switch, and does not start more despite the command to get it going in the other direction. Then despite the limit switch is not connected directly to the engine but rather to a digital pin arduino is as if the limit switch off the current to the motor. I have some possibilities in software out of the cycle of "if" and then be able to understand the program that the limit switch is not depressed despite both? help :slightly_frowning_face:

Perhaps if you review the logic and add in an extra condition check...?

Assuming that there is a start(reverse,power ) or similar command.

So something like this?

if (digitalRead(switchPin) == LOW)

AND

start(forward, power);

THEN

Stop

The condition for STOP will be negated once the reverse command is given.

Thanks for the help, but unfortunately the truck remains stationary no more again, I fear that the switch off the current to the motor despite being connected to a digital pin arduino and then controlled by it. :confused:

diego-95:
Thanks for the help, but unfortunately the truck remains stationary no more again,

Might be a good idea to read ALL of the posts in this Thread again, and carefully.

...R

Diego ...you've not given enough information for anyone to solve your problem.

This question has been asked previously ,but I for one am unclear as to what your answer actually was... so...once again.

IS the switch actually in the motor circuit?

If not what is it connected to? ..doing what ?...commanding / operating what ?

Post a circuit diagram / sketch /picture of the switch , motor etc.

Also.

Upload ALL your code .
Don't know how to upload a file or picture from your computer to the forum?
Go here ...How to use this forum - please read. - Installation & Troubleshooting - Arduino Forum ....read section 8

  1. You can add attachments

In fact read ALL of the How to use this forum