# Consistent Motor control - specifically 180 degrees

I am struggling to get my motors to perform the same on each run. The first time I run the program, they will perform the same movement about 8/10 times. The second time I start the program, the movement does not perform the same as the last. It is significantly different.

How can I get my motors to perform the same exact movement, starting at Position A, turning 180 degrees forward to Position B, and then reversing 180 degrees back to Position A. Also, if there is a better method to implement this, please let me know. Need decent speed with high torque. This is the original way that was working for me with a singular motor.

I am using a 9V battery powering into my L298N Motor controller where I have 2 6-12V Mini DC Motors directed into. I then run the according control pins to my pi. I noticed when powering just one motor, I got much more consistent results in-between program starts. I am assuming it is because the power is not weaker, splitting into the two motors? Is the difference in power producing inconsistent results?

``````def init():
gpio.setmode(gpio.BCM)
gpio.setup(17, gpio.OUT)
gpio.setup(22, gpio.OUT)
gpio.setup(23, gpio.OUT)
gpio.setup(24, gpio.OUT)

def forward(tf):
init()
gpio.output(17, True)
gpio.output(22, False)
gpio.output(23, True)
gpio.output(24, False)
time.sleep(tf)

gpio.cleanup()

def reverse(tf):
init()
gpio.output(17,False)
gpio.output(22, True)
gpio.output(23, False)
gpio.output(24, True)
time.sleep(tf)
gpio.cleanup()

for i in range(0,10):
print ("forward")
forward(.015)
time.sleep(1)
print ("backward")
reverse(.015)
time.sleep(1)

``````
• @tlfong01 I can spare a couple degrees but not much room for that... I need the action to be done in about a second and as for torque I am lifting about 50 grams. I am new to this so adding proper detail is harder for me. Mar 2 at 2:46
• I would be surprised if this incomplete code did anything other than print a string of error messages. Mar 2 at 5:19
• Sounds like you need to use stepper motors. Mar 2 at 7:49
• @CoderMike: (14) Yes, so I tried it with a step motor: youtu.be/sJkf4uP4J6c. (15) More details at the end of this chat about using tmc2225 stepping motor driver: chat.stackexchange.com/rooms/134387/…. Mar 2 at 8:25
• Now I am using a toy spring balance, and a bag of nuts as load/force to calibrate the stalled torque of the stepper motor I have been testing. My testing plan is summarized below: (a) write a program to move the motor forward and backward between 90 degrees and 45 degrees. Gradually increase the loading, ie, bag of nuts, until the motor can no longer move to the 90 degrees limit. Then do the calculation to get the stalled torque. Below is the video illustrating my plan.: youtu.be/mryE3ayH27o Mar 2 at 13:33