DC motor doesn't return at the same position

hello everyone, i need help with my code , i want to run my motor (12v 30rpm) for a few seconds and return to the same position , i use l298n motor drive, this is my code

int motor1=3;
int motor11=4;

void setup() {
pinMode(motor1,OUTPUT);
pinMode(motor11,OUTPUT);

}

void loop() {
  digitalWrite(motor11,HIGH);
digitalWrite(motor1,LOW);
delay(2500);
  digitalWrite(motor1,LOW);
  digitalWrite(motor11,LOW);
delay(2000);
  digitalWrite(motor11,LOW);
digitalWrite(motor1,HIGH);
delay(2500);
  digitalWrite(motor1,LOW);
  digitalWrite(motor11,LOW);
delay(2000);

}

Inertia, mass, momentum…

  • plus some form of position feedback.

If you want to control the position of your motor you should use a servo or stepper motor. You can't control the position of a normal DC motor without an encoder feedback.

I bought a lot of dc motor for my project so i can not exchange the type of the motor ):

I must found a solution for a dc motor

What do you mean about encoder feedback?

Encoder feedback is some additional bit of equipment that provides information as to the number of revolutions the motor has made in a particular direction.

It might be helpful if you describe your greater project so that a suitable feedback mechanism could be recommended. If, for example, the motor opens and closes a door, the feedback might simply be a switch ("limit switch") that makes contact when the door is fully open or closed.

This happens a lot when you try to build before doing a schematic and a project statement. Your problem is how to know when you are in the home position. That can be easy to impossible. There is no positioning with DC motors, you must use external feedback. You can time but as components wear etc that will change. At the current point all you have given us is a motor with a shaft, more details such as what you are building, a schematic, etc.

Try Limit switches or Hall Effect sensors

1 Like

Hi, @aishaenezi

You would be best to use a limit swatch, positioned at the HOME or start position of your motor.

What is the application?
What is your project?
What does the motor shaft drive?

Thanks.. Tom... :+1: :coffee: :australia:

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.