I have adopted Electronics hub tutorial to run 2 motors from a L298N controlled by Rpi3B using GPIO.
Why is my version of the code not working?
I do understand that I could ignore the "channel already in use" warning but I thought I would mention in case they really are being used elsewhere. The code is below. A pic attached is showing the warnings and the results of "raspi-gpio get". Also included is a very rough and ready wiring diagram and io table between Rpi3B & L298N MC.
import RPi.GPIO as GPIO
from time import sleep
# LHS enable
enA=12
# L_FWD
in1=23
# L_REV
in2=24
# RHS enable
enB=13
# R_FWD
in3=17
# R_REV
in4=27
temp1=1
GPIO.setmode(GPIO.BCM) # Broadcom pin-numbering scheme
GPIO.setup(enA,GPIO.OUT)
GPIO.setup(in1,GPIO.OUT)
GPIO.setup(in2,GPIO.OUT)
GPIO.setup(enB,GPIO.OUT)
GPIO.setup(in3,GPIO.OUT)
GPIO.setup(in4,GPIO.OUT)
p1=GPIO.PWM(enA,1000)
p2=GPIO.PWM(enB,1000)
p1.start(25)
p2.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(1):
x=input()
if x=='r':
print("run")
if(temp1==1):
GPIO.output(enA,GPIO.HIGH)
GPIO.output(in1,GPIO.HIGH)
GPIO.output(in2,GPIO.LOW)
GPIO.output(enB,GPIO.HIGH)
GPIO.output(in3,GPIO.HIGH)
GPIO.output(in4,GPIO.LOW)
print("forward")
x='z'
elif x=='s':
print("stop")
GPIO.output(enA,GPIO.LOW)
GPIO.output(in1,GPIO.LOW)
GPIO.output(in2,GPIO.LOW)
GPIO.output(enB,GPIO.LOW)
GPIO.output(in3,GPIO.LOW)
GPIO.output(in4,GPIO.LOW)
x='z'
elif x=='f':
print("forward")
GPIO.output(enA,GPIO.HIGH)
GPIO.output(in1,GPIO.HIGH)
GPIO.output(in2,GPIO.LOW)
GPIO.output(enB,GPIO.HIGH)
GPIO.output(in3,GPIO.HIGH)
GPIO.output(in4,GPIO.LOW)
temp1=1
x='z'
elif x=='b':
print("backward")
GPIO.output(enA,GPIO.HIGH)
GPIO.output(in1,GPIO.LOW)
GPIO.output(in2,GPIO.HIGH)
GPIO.output(enB,GPIO.HIGH)
GPIO.output(in3,GPIO.LOW)
GPIO.output(in4,GPIO.HIGH)
temp1=0
x='z'
elif x=='l':
print("low")
p1.ChangeDutyCycle(25)
p2.ChangeDutyCycle(25)
x='z'
elif x=='m':
print("medium")
p1.ChangeDutyCycle(50)
p2.ChangeDutyCycle(50)
x='z'
elif x=='h':
print("high")
p1.ChangeDutyCycle(75)
p2.ChangeDutyCycle(75)
x='z'
elif x=='e':
GPIO.cleanup()
print("GPIO Clean up")
else:
print("<<< wrong data >>>")
print("please enter the defined data to continue.....")
here is the GPIO Get response
pi@RozPi:~ $ raspi-gpio get BANK0 (GPIO 0 to 27):
GPIO 0: level=1 fsel=0 func=INPUT
GPIO 1: level=1 fsel=0 func=INPUT
GPIO 2: level=1 fsel=4 alt=0 func=SDA1
GPIO 3: level=1 fsel=4 alt=0 func=SCL1
GPIO 4: level=1 fsel=0 func=INPUT
GPIO 5: level=1 fsel=0 func=INPUT
GPIO 6: level=1 fsel=0 func=INPUT
GPIO 7: level=1 fsel=1 func=OUTPUT
GPIO 8: level=1 fsel=1 func=OUTPUT
GPIO 9: level=0 fsel=4 alt=0 func=SPI0_MISO
GPIO 10: level=0 fsel=4 alt=0 func=SPI0_MOSI
GPIO 11: level=0 fsel=4 alt=0 func=SPI0_SCLK
GPIO 12: level=1 fsel=1 func=OUTPUT
GPIO 13: level=1 fsel=1 func=OUTPUT
GPIO 14: level=1 fsel=2 alt=5 func=TXD1
GPIO 15: level=1 fsel=2 alt=5 func=RXD1
GPIO 16: level=0 fsel=0 func=INPUT
GPIO 17: level=0 fsel=1 func=OUTPUT
GPIO 18: level=0 fsel=0 func=INPUT
GPIO 19: level=0 fsel=0 func=INPUT
GPIO 20: level=0 fsel=0 func=INPUT
GPIO 21: level=0 fsel=0 func=INPUT
GPIO 22: level=0 fsel=0 func=INPUT
GPIO 23: level=0 fsel=1 func=OUTPUT
GPIO 24: level=0 fsel=1 func=OUTPUT
GPIO 25: level=0 fsel=0 func=INPUT
GPIO 26: level=0 fsel=0 func=INPUT
GPIO 27: level=0 fsel=1 func=OUTPUT
here are the warnings corresponding to the GPIO setup calls in the code above /home/pi/L298N_Dual.py:22: RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings. GPIO.setup(enA,GPIO.OUT) /home/pi/L298N_Dual.py:23: RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings. GPIO.setup(in1,GPIO.OUT) /home/pi/L298N_Dual.py:24: RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings. GPIO.setup(in2,GPIO.OUT) /home/pi/L298N_Dual.py:25: RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings. GPIO.setup(enB,GPIO.OUT) /home/pi/L298N_Dual.py:26: RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings. GPIO.setup(in3,GPIO.OUT) /home/pi/L298N_Dual.py:27: RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings. GPIO.setup(in4,GPIO.OUT)