Reputation: 43
I have some troubles with my Raspberry Pi3B+. First of all, I want to control 2 motors using the Raspberry Pi and the L289n MotorDriver. My main problem is that the motors won't start... If I use my multimeter, it says that there is no electricity that arrives on the motors. However, it may be the code or even the circuit, I don't really know. So I decided to ask her and upload not only my code but also my circuit as a picture. Maybe you can help me.
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
#Motor 1
GPIO.setup(17, GPIO.OUT)
GPIO.setup(27,GPIO.OUT)
GPIO.output(17, 0)
GPIO.output(27, 0)
#Motor 2
GPIO.setup(23, GPIO.OUT)
GPIO.setup(24,GPIO.OUT)
GPIO.output(23, 0)
GPIO.output(24, 0)
#Control Motor 1
GPIO.output(27, 1)
GPIO.output(17, 0)
#Control Motor 2
GPIO.output(23, 0)
GPIO.output(24, 1)
Upvotes: 0
Views: 658
Reputation: 7409
You can try the following:
import RPi.GPIO as GPIO
from time import sleep
in1 = 17
in2 = 27
en = 25
temp1 = 1
GPIO.setmode(GPIO.BCM)
GPIO.setup(in1, GPIO.OUT)
GPIO.setup(in2, GPIO.OUT)
GPIO.setup(en, GPIO.OUT)
GPIO.output(in1, GPIO.LOW)
GPIO.output(in2, GPIO.LOW)
p = GPIO.PWM(en, 1000)
p.start(25)
print("\n")
print("The default speed & direction of motor is LOW & Forward.....")
print("r-run s-stop f-forward b-backward l-low m-medium h-high e-exit")
print("\n")
while True:
x = raw_input()
if x == 'r':
print("run")
if (temp1 == 1):
GPIO.output(in1, GPIO.HIGH)
GPIO.output(in2, GPIO.LOW)
print("forward")
x = 'z'
else:
GPIO.output(in1, GPIO.LOW)
GPIO.output(in2, GPIO.HIGH)
print("backward")
x = 'z'
elif x == 's':
print("stop")
GPIO.output(in1, GPIO.LOW)
GPIO.output(in2, GPIO.LOW)
x = 'z'
elif x == 'f':
print("forward")
GPIO.output(in1, GPIO.HIGH)
GPIO.output(in2, GPIO.LOW)
temp1 = 1
x = 'z'
elif x == 'b':
print("backward")
GPIO.output(in1, GPIO.LOW)
GPIO.output(in2, GPIO.HIGH)
temp1 = 0
x = 'z'
elif x == 'l':
print("low")
p.ChangeDutyCycle(25)
x = 'z'
elif x == 'm':
print("medium")
p.ChangeDutyCycle(50)
x = 'z'
elif x == 'h':
print("high")
p.ChangeDutyCycle(75)
x = 'z'
elif x == 'e':
GPIO.cleanup()
break
else:
print("<<< wrong data >>>")
print("please enter the defined data to continue.....")
If you take a look at the code carefully, you can easily understand the working. Run the script.
You will get a message regarding the default speed and direction of the Motor. This is followed by a list of commands you must use to control the motor. These commands are given below.
Source - Raspberry Pi L298N Interface Tutorial
Upvotes: 1