user81083: 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;
  }
}


user81094: 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.

81083: <p>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..</p>
<pre><code>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 &amp;&amp; sw == 0) state=LOW;
  else if (reading == HIGH) state=LOW;
  else state=HIGH;

  digitalWrite(outPin, state);
}

void stop() {
  if (sw == 0 &amp;&amp; reading == LOW) {
    state=LOW;
    previous=1;
  }
}
</code></pre>
 81094: <p>You can stop a BDC faster if you short-circuit it. This is possible with an H-Bridge and the appropriate components.</p>
<p>It would be easier to use a <a href="https://www.ti.com/product/L293D" rel="nofollow noreferrer">L293D</a>, which already provides this mode.</p>
<p>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.</p>
<p>For up to 2A you can use a L298N.</p>
