1

So, according to this video which I had followed, my motors should be constantly running as per the program and should stop running after the program is executed. Instead, when I first ran the script, the motors did run, as per the directions and stopped. But, later, on the second try, one of my wires connecting the motor, disconnected. I immediately reattached it back after stopping the program. But, the motor kept constantly running and was running regardless of the program(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(PS I am just a newbie in this), but using a voltmeter I found out that it still was nearly full. Then, I tried running it again, and this time, it did not even run. So, just to try out, I restarted my Pi, and it worked again and slowly decreased the speed as the previous incident. So, if there is any solution to this problem, it would be very much appreciated.

This is the circuit layout: https://ibb.co/dk49rVV (Stack Exchange wouldn't let me add a picture due to its size)

P.S. I had made a code error on the first try, which I had later realised 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)


Also, just to your notice, I'm very new to this. So, if you feel I'm too dumb or silly, sorry. 

Thank you.

6
  • 1
    Add the code to the question (not a link). Add a photo and schematic of the wiring to the question (not a link).
    – joan
    May 20 at 19:41
  • if a wire disconnects, then power down before reattaching wire
    – jsotola
    May 21 at 0:44
  • @joan Done. Thank you. May 21 at 6:08
  • @jsotola I had tried that already. Did not work. May 21 at 6:09
  • The code is clearly wrong for forward(). Can you edit the post and show the code you are actually using.
    – joan
    May 21 at 8:57
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. May 24 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
    May 24 at 7:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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