1

enter image description hereenter image description hereI 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.....")

enter image description here

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)

4
  • 1
    please do not post pictures of text ... post the text instead
    – jsotola
    Commented Nov 16, 2022 at 23:19
  • What is not working? I'd expect the motors to switch on and off but PWM (speed control) will likely not work.
    – joan
    Commented Nov 17, 2022 at 9:29
  • Post an annotated schematic of how you have it wired, showing all power, ground, and power supply connections, not a frizzy picture. Also post a link to "Technical information" on the hardware devices.
    – Gil
    Commented Nov 18, 2022 at 17:44
  • thanks Gil. I was having problems with schematic view of the L298N object I imported into Fritzing. Thats why I only shared the wiring diagram view. I will find another L298N import and get a schematic together.
    – rozzers
    Commented Nov 20, 2022 at 15:09

1 Answer 1

0

I believe your code is OK. The L298 was never intended to operate with a Pi, it is 5V logic, about 4.5V minimum. If this is wrong please post an annotated schematic and a link to the "Technical Information" on the L298 module. I cannot tell how it is wired but 4 batteries will give you a little over 6V but as the motors run that voltage will drop because of the battery resistance and partially discharged.

You may also damage your Pi, it is my understanding that it should only be connected to an external power supply rated at 5V/2.5 A DC. A higher amperage rating of the power supply will be fine.

1
  • thanks Gil. I checked the spec again and added a schematic from the spec for the Dual H BRidge L298N. In the image screen-print for the board shows 5V printed over the ENA & ENB inputs. Thanks for the guidance. I will look for a motor controller that can operate with 3v3 control lines.
    – rozzers
    Commented Nov 24, 2022 at 20:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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