0

I connected to my Raspberry pi 3b+ two L298N H-bridge drivers and 4 motor (two for each driver) and i tried the code below:

Assumption: I purposely used this script because I wanted to test the drivers separately

#!/usr/bin/env python
#!/bin/bash
import RPi.GPIO as GPIO
from time import sleep

print("\n")
print("A-forward S-backward L-left turn R-right turn  l-low m-medium h-high e-exit")
print("\n")

a=11
b=15
c=16
d=18

def init():    
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(a, GPIO.OUT)
    GPIO.setup(b, GPIO.OUT)
    GPIO.setup(c, GPIO.OUT)
    GPIO.setup(d, GPIO.OUT)
    p=GPIO.PWM(d,1000)
    p.start(25)

def forward():
    init()
    GPIO.output(a, False)
    GPIO.output(b, True)
    GPIO.output(c, True)
    GPIO.output(d, False)  
    GPIO.cleanup()

def reverse():
    init()
    GPIO.output(a, True)
    GPIO.output(b, False)
    GPIO.output(c, False)
    GPIO.output(d, True)
    GPIO.cleanup()

def left_turn():
    init()
    GPIO.output(a, True)
    GPIO.output(b, False)
    GPIO.output(c, True)
    GPIO.output(d, False)
    GPIO.cleanup()

def right_turn():
    init()
    GPIO.output(a, False)
    GPIO.output(b, True)
    GPIO.output(c, False)
    GPIO.output(d, True)
    GPIO.cleanup()

while(1):

    x=input()
    if x=='w':
        print("forward")
        forward()
        x='z'

    elif x=='s':
        print("backward")
        reverse()
        x='z'

    elif x=='a':
        print("left")
        left_turn()
        x='z'

    elif x=='d':
        print("right")
        right_turn()
        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.....")

The L298N drivers are wired throught pin connection using this schema:

#Driver n°1

input1D1 = 23 #Pin 16

input2D1 = 24 #Pin 18

input3D1 = 25 #Pin 22

input4D1 = 16 #Pin 36

#Driver n°2

input1D2 = 17 #Pin 11

input2D2 = 27 #Pin 13

input3D2 = 22 #Pin 15

input4D2 = 26 #Pin 37

But when i try to run the script nothing is happening and i don't understand why.

I tried to change the pin code from the code with the reference of the other driver but i had the same result.

enter image description here

enter image description here

enter image description here

-------------------- EDIT 1 --------------------

This is the code after the correction:

#!/usr/bin/env python
#!/bin/bash
import RPi.GPIO as GPIO
from time import sleep

print("\n\nA: forward\nS: backward\nL: left turn\nR: right turn\nQ: stop\nL: low M: medium H:high E:exit\n")

#Function used to initialize GPIO


#Driver n°1
input1D1 = 23 #Pin 16
input2D1 = 24 #Pin 18
input3D1 = 25 #Pin 22
input4D1 = 16 #Pin 36

GPIO.setmode(GPIO.BCM)
GPIO.setup(input1D1, GPIO.OUT)
GPIO.setup(input2D1, GPIO.OUT)
GPIO.setup(input3D1, GPIO.OUT)
GPIO.setup(input4D1, GPIO.OUT)
p = GPIO.PWM(input4D1,1000)
#p.start(100)

#Driver n°2
input1D2 = 17 #Pin 11
input2D2 = 27 #Pin 13
input3D2 = 22 #Pin 15
input4D2 = 26 #Pin 37

GPIO.setup(input1D2, GPIO.OUT)
GPIO.setup(input2D2, GPIO.OUT)
GPIO.setup(input3D2, GPIO.OUT)
GPIO.setup(input4D2, GPIO.OUT)
p2 = GPIO.PWM(input4D2,1000)
#p2.start(100)


#Function used to stop the rover
def stop():
    #Driver n°1
    GPIO.output(input1D1, 0)
    GPIO.output(input2D1, 0)
    GPIO.output(input3D1, 0)
    GPIO.output(input4D1, 0)

    #Driver n°2
    GPIO.output(input1D2, 0)
    GPIO.output(input2D2, 0)
    GPIO.output(input3D2, 0)
    GPIO.output(input4D2, 0)

#Function used to move forward the rover
def forward():
    #Driver n°1
    GPIO.output(input1D1, 0)
    GPIO.output(input2D1, 1)
    GPIO.output(input3D1, 0)
    GPIO.output(input4D1, 1)

    #Driver n°2
    GPIO.output(input1D2, 0)
    GPIO.output(input2D2, 1)
    GPIO.output(input3D2, 0)
    GPIO.output(input4D2, 1)

#Function used to move backward the rover
def reverse():
    #Driver n°1
    GPIO.output(input1D1, 1)
    GPIO.output(input2D1, 0)
    GPIO.output(input3D1, 1)
    GPIO.output(input4D1, 0)

    #Driver n°2
    GPIO.output(input1D2, 1)
    GPIO.output(input2D2, 0)
    GPIO.output(input3D2, 1)
    GPIO.output(input4D2, 0)

#Function used to turn left the rover
def left_turn():
    GPIO.output(input1D1, 0)
    GPIO.output(input2D1, 1)
    GPIO.output(input3D1, 0)
    GPIO.output(input4D1, 1)

   # GPIO.output(input1D2, 0)
   # GPIO.output(input2D2, 1)
   # GPIO.output(input3D2, 0)
   # GPIO.output(input4D2, 1)

#Function used to turn right the rover
def right_turn():
    GPIO.output(input1D1, False)
    GPIO.output(input2D1, True)
    GPIO.output(input3D1, False)
    GPIO.output(input4D1, True)

while(1):

    x=input()
    if x=='w':
        print("Moving forward...")
        forward()
        x='z'

    elif x=='s':
        print("Moving backward...")
        reverse()
        x='z'

    elif x=='a':
        print("Turning left...")
        left_turn()
        x='z'

    elif x=='d':
        print("Turning right...")
        right_turn()
        x='z'

    elif x=='q':
        print("Stop")
        stop()
        x='z'

    elif x=='l':
        print("Low")
        p.ChangeDutyCycle(25)
        p2.ChangeDutyCycle(25)
        x='z'

    elif x=='m':
        print("Medium")
        p.ChangeDutyCycle(50)
        p2.ChangeDutyCycle(50)
        x='z'

    elif x=='h':
        print("High")
        p.ChangeDutyCycle(75)
        p2.ChangeDutyCycle(75)
        x='z'

    elif x=='e':
        GPIO.cleanup()
        break

    else:
        print("<<<  wrong data  >>>")
        print("please enter the defined data to continue.....")

-------------------- EDIT 2 --------------------

I tried to draw the connections between raspberry and the driver

enter image description here

-------------------- EDIT 3 --------------------

I double-checked the source code and I fixed some things, I also double checked all the connections and they are exactly as described above. Currently I am able to properly move the motors forward but currently I can't :

  • get all 4 motors to stop at the same time (in this case it happens that two motors stop and the other two continue to rotate);
  • move backward (in this case it happens that two motors stop and the other two continue to rotate);
  • turn righ / left

What did I do wrong?

-------------------- EDIT 4 --------------------

I'm not able to turn right or left, i don't understand how to set the gpio in the script to "rotate" the rover.

I have tried different configurations but none of them work, for example to turn right I think that may be enough rotate "backward" the motor to the right side, and rotate "foreward" the left side..

In the image it is possible to see the two drivers (1 above and one below) and the motors connected to them.

Each driver drives one motor on the right and one on the left

4
  • 1
    Post a clear photo of your wiring. Are you sure you have the correct GPIO's connected? Call init() once, do not call GPIO.cleanup
    – CoderMike
    May 26, 2022 at 15:28
  • 2
    I can't see a connection to GPIO 15. See pinout.xyz Check all the GPIO.
    – joan
    May 26, 2022 at 16:08
  • Sorry i make a mistake, take a look to my post again please
    – Mattia
    May 27, 2022 at 10:12
  • May I suggest a debugging step? Remove the motor drivers and put an LED on each output, well labeled. Then check if the outputs look like they are supposed to look.
    – NomadMaker
    May 30, 2022 at 21:27

2 Answers 2

2

Call GPIO.setup() ONCE, at the start of the script.

Call GPIO.cleanup() ONCE, just before the script ends.

When you call GPIO.cleanup() it resets all used GPIO to be in INPUT mode.

You need to sort out your GPIO numbering scheme.

I suggest you use Broadcom numbers (shown as GPIO x at https://pinout.xyz/).

If you use Broadcom numbers make sure you use GPIO.setmode(GPIO.BCM) and update variable a, b,c, and d to the Broadcom number.

4
  • I tried to correct my code as you suggest but something it's wrong. it is as if only one of the drivers is recognized by the raspberry. The motor run just when i press "a" or "d" but nothing happend when i press "w" or "s".
    – Mattia
    May 26, 2022 at 15:43
  • Edit your question, add that information and the requested photo.
    – joan
    May 26, 2022 at 15:46
  • Please take a look on my edited post
    – Mattia
    May 26, 2022 at 15:52
  • I added to my post a schema drawed from me, please, take a look
    – Mattia
    May 27, 2022 at 7:25
1

Since I cannot see the schematic I will take a SWAG and say it is doing what it should, not work. The L298N is an old and inefficient part. If you look at the data sheet it tells you that the Logic Supply Voltage (pin 9) is from 4.5 to 5 7 V. From what I can see you are powering it with a 3V3 which is to low. The inputs are rated at 2V3 minimum with a 4.5V supply. With a load expect the outputs to drop from 3 to 5V. You state: "The L298N drivers are wired throught pin connection using this schema:" I never found it.

3
  • I added to my post a schema drawed from me, please, take a look
    – Mattia
    May 27, 2022 at 7:25
  • 1
    Nice start on the schematic. It tells more then the last 100 frizzy pictures I looked at. Labeling the driver pins would help as I cannot read them in the pictures. There are two power connections to the L298N Vss which is the logic power that needs to be supplied with 4.5 to 5.6 volts. There is VS which is the motor power, the grounds should be common. I am not familiar with your drive can you post a link to technical information on it. It is possible it is deriving the logic power from the motor power. The only thing I have other then what you posted is the data sheet for the L298N,
    – Gil
    May 27, 2022 at 17:49
  • I edited again, code and schema, i'm not able to turn right or left, i don't understand how to set the gpio in the script to "rotate" the rover. I have tried different configurations but none of them work, for example to turn right I think that may be enough rotate "backward" the motor to the right side, and rotate "foreward" the left side.. Each driver drives one motor on the right and one on the left
    – Mattia
    May 30, 2022 at 9:39

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.