1

I have a problem with stopping motor at same spot when magnet pass.. I tried to add interrupt and to make code as clean as possible but no joy so far. Any suggestions? sw will add later, just ignore..

byte outPin = 12;        // the number of the output pin
byte inPin = 2;          // the number of the input pin

byte hallPin = 3;        // hall sensor

byte sw;
byte previous;
int state = LOW;         // the current state of the output pin
int reading;             // the current reading from the input pin

void setup()
{
  pinMode(inPin, INPUT_PULLUP);
  pinMode(outPin, OUTPUT);

  pinMode(hallPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(hallPin), stop, FALLING);   //hall read low when magnet pass
}

void loop()
{
  sw=0;
  
  reading = digitalRead(inPin);       //read(LOW when press!)
  if (reading == HIGH) previous=0;
  
  if (previous == 1 && sw == 0) state=LOW;
  else if (reading == HIGH) state=LOW;
  else state=HIGH;

  digitalWrite(outPin, state);
}

void stop() {
  if (sw == 0 && reading == LOW) {
    state=LOW;
    previous=1;
  }
}
10
  • What is the problem you are facing exactly?
    – Coder9390
    Oct 31, 2021 at 13:45
  • well code works, but I'm having delays, and motor does not stop instantly... any way to enhance code further?
    – Milos
    Oct 31, 2021 at 15:05
  • 1
    You can enhance it by switching changing state's declaration to volatile bool state ...
    – timemage
    Oct 31, 2021 at 15:48
  • 1
    @timemage will try that, thanks
    – Milos
    Oct 31, 2021 at 16:43
  • 1
    @Coder9390 arduino Micro
    – Milos
    Nov 1, 2021 at 15:47

2 Answers 2

3

You can stop a BDC faster if you short-circuit it. This is possible with an H-Bridge and the appropriate components.

It would be easier to use a L293D, which already provides this mode.

If you want to try it with this component, of course you can also build the h-bridge yourself, pay attention to the D on the component. With the D-version the protection diodes are already built in. You can power two motors from 4.5V to 36V with a maximum current of 600mA.

For up to 2A you can use a L298N.

2
  • He may be able to stop it still faster by powering it in reverse for a very short time. The current draw can be very large though. Nov 1, 2021 at 11:49
  • 1
    @Georg Thanks for recommendation, will consider if no code give acceptable result
    – Milos
    Nov 1, 2021 at 17:54
-1

The motor can't stop instantly because it has inertia, plus the inertia of whatever stuff the motor is moving.

So if you want it to stop at a set position, you have to slow it down before it reaches the desired position, so it will come to a stop where you want. It's not possible to do that with only one sensor.

A complicated solution would be to use a position encoder, like a rotary encoder, to know the position at all times, so you could use a control algo like PID on the motor. This offers the most features, as you can then set the desired stop position in software, without needing to move the sensor.

If the stop position never moves then a simpler solution is to use multiple sensors. That's often how it's done with elevators: there is a sensor to accurately align the elevator with the floor it has to stop on, but there are also several others slightly before that on the track to tell the controller that it's time to start slowing down, then slow down to a crawl, then stop.

To keep it simple you can use just two sensors: when it goes past the first, slow down the motor to a crawl. When it activates the second sensor, since it is going slow, it will stop quickly and end up where you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.