DC Motor break

Hello guys,

I am starting a new project and the first step is to get my DC Motor to stop at exactly 360 degrees, I have read that its quite difficult to do this, however I have almost achieved this, the problem is that it does stop for a few micro seconds but then tries to re-adjust…

Basically the code is doing the following:
1- Move shaft 360 degrees (should of been 3240 pulses) however when I did this manually, its clearly not right and ended up setting it up to 3775 pulses which makes my 360 turn on the shaft.
This is the Motor

2- Because it is a DC Motor, with no brakes, I found a code and modified it a bit to try to reverse the motor when it passes the set amount of pulses…

void loop() {
if (pulses==total) {
analogWrite(pwm,0);
digitalWrite(dir,HIGH);
digitalWrite(dir2,HIGH);
}
else {
if (pulses>total) {
analogWrite(pwm,motorSpeed);
digitalWrite(dir,LOW);
digitalWrite(dir2,HIGH);

}
else if (pulses<total) {
analogWrite(pwm,motorSpeed);
digitalWrite(dir,HIGH);
digitalWrite(dir2,LOW);
}
}

However it seems like I accidentally made a vibrator… the problem is that it keeps trying to go back and fourth, even if it is right on the value, it stops at the right place, but keeps buzzing and vibrating…

Any ideas if I can simply set a range instead? if its within 2 pulses maybe it will be satisfied?

EncorderA_B_x2.ino (2.13 KB)

nizoo91:
Hello guys,

I am starting a new project and the first step is to get my DC Motor to stop at exactly 360 degrees, I have read that its quite difficult to do this, however I have almost achieved this, the problem is that it does stop for a few micro seconds but then tries to re-adjust...

Basically the code is doing the following:
1- Move shaft 360 degrees (should of been 3240 pulses) however when I did this manually, its clearly not right and ended up setting it up to 3775 pulses which makes my 360 turn on the shaft.
This is the Motor

2- Because it is a DC Motor, with no brakes, I found a code and modified it a bit to try to reverse the motor when it passes the set amount of pulses...
However it seems like I accidentally made a vibrator.. the problem is that it keeps trying to go back and fourth, even if it is right on the value, it stops at the right place, but keeps buzzing and vibrating...

Any ideas if I can simply set a range instead? if its within 2 pulses maybe it will be satisfied?

You need to use a PID loop, and tune it. Your ad-hoc feedback system is not going to do as well I suspect.

The control loop needs full linear authority over the motor drive, ie it must be able to drive it in either direction
at any drive level. The signed output value from a PID loop needs to be split into direction + strength to interface to the typical PWM-driven H-bridge.

For accurate final position the I-term in the PID is needed - however the I-term can be problematic
(read up on integral wind-up).