1

Following this video, my motors should be constantly running as per the program and should stop running after the program is executed.

When I first ran the script, the motors did run, as per the directions and stopped. Later on my second try, one of my wires connecting the motor, disconnected. I immediately reattached it after stopping the program. The motor kept running regardless of the status of the program. By this, I mean that it did not change the directions or stop as per the time.

I restarted my Pi and it worked fine for 2 tries and then slowly, the speed of the motors decreased and then at one point stopped. I thought that my batteries were dead as the motors might have consumed a lot of power, but using a voltmeter I found out that it still was nearly full.

I tried running it again, and this time it did not even run. As another test I restarted my Pi, and it worked again and slowly decreased the speed as the previous incident.

Does anyone have a solution to my problem?

This is the circuit layout: https://ibb.co/dk49rVV

I had made a code error on the first try, which I had later realized when the motors spun only in 1 direction. For the forward function, I had accidentally copy-pasted the same line that is, the gpio.output(17, True) instead of assigning the values for different GPIOS.

import RPi.GPIO as gpio
import time

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(steps):
    init()
    gpio.output(17, False)
    gpio.output(22, True)
    gpio.output(23, False)
    gpio.output(24, True)
    time.sleep(steps)
    gpio.cleanup()

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

print("GOING FORWARD")
foward(4)
print("GOING BACKWARD")
reverse(2)
4
  • if a wire disconnects, then power down before reattaching wire
    – jsotola
    Commented May 21, 2021 at 0:44
  • @jsotola I had tried that already. Did not work. Commented May 21, 2021 at 6:09
  • The code is clearly wrong for forward(). Can you edit the post and show the code you are actually using.
    – joan
    Commented May 21, 2021 at 8:57
  • @joan Done. I messed up in assigning the GPIO values. Commented May 21, 2021 at 12:08

2 Answers 2

0

Try the following.

import RPi.GPIO as gpio
import time

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(steps):
    gpio.output(17, False)
    gpio.output(22, True)
    gpio.output(23, False)
    gpio.output(24, True)
    time.sleep(steps)

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

init()

print("GOING FORWARD")
foward(4)
print("GOING BACKWARD")
reverse(2)

gpio.cleanup()
2
  • Nope, didn't work. Commented May 24, 2021 at 7:20
  • In that case you need to start from scratch. Completely rewire and get one motor working before moving on to the second.
    – joan
    Commented May 24, 2021 at 7:42
0

Part of your problem is powering this with batteries. 2 9V batteries do not have the capacity to run the Pi and/or the motor for any period of time. Use a different power source that will supply the needed current. Your broken connection could have destroyed the Pi and or the driver. Start by testing the Pi with LEDs on the pins you are using and determine they operate properly. If not you will need to either repair or replace it. An alternative would be to use other pins that operate properly. If it is ok, you probably fried the driver, try a new one.

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