0
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(16, GPIO.OUT) 
GPIO.setup(26, GPIO.OUT) 
GPIO.setup(5, GPIO.OUT)  
GPIO.setup(6, GPIO.OUT) 


print ("Car is moving Forward.")
GPIO.output(16, 1)
GPIO.output(26, 1)
time.sleep(4)


print("Car is moving backward.")

GPIO.output(5,1)
GPIO.output(6, 1)
time.sleep(3)

GPIO.cleanup()

Now, the shell is print 'Car is moving forward' and also, my robot tires are moving forward. then, 'Car is moving backward' print is being print but Robot tires are not moving backward. What am I doing wrong in the code?

  • How are the motors connected? Are you using a motordriver or are you driving the motors directly from the Pi? – Steve Robillard Jun 19 '17 at 19:20
  • 2
    Could you post the code you are actually using? A wrongly copied snippet is useless. – joan Jun 19 '17 at 19:22
  • I'm using motor driver l298 – spiCkyyy Jun 19 '17 at 19:26
  • The code and any answers belong in your question not the comments, and they should be properly formatted like I did. – Steve Robillard Jun 19 '17 at 19:35
  • Done brother. You can see the code now. – spiCkyyy Jun 19 '17 at 19:41
1

You should be setting both GPIO's connected to each motor input, not the single GPIO per motor you are setting. To drive the first motor you set one GPIO high and the other low, to reverse the direction you reverse the pins setting the opposite pins low and high. Assuming GPIO 16 and 26 are one motor you need to set one high and the other low, like this:

print ("Car is moving Forward.")
GPIO.output(16, 1)
GPIO.output(26, 0)
time.sleep(4)

and to change the rotation:

print ("Car is moving Backward.")
GPIO.output(16, 0)
GPIO.output(26, 1)
time.sleep(4)

and obviously the same for the other motor.

  • Kindly see my question now. I'd be grateful. :)) – spiCkyyy Jun 19 '17 at 21:00
  • You should not move or ask another, especially after getting an answer, instead you should create a new question. This keeps the queestion clear and relevant. – Steve Robillard Jun 19 '17 at 21:09
  • Okay, I'll post the question back. Sorry. I'll ask new question. – spiCkyyy Jun 19 '17 at 21:10

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.