Terabitio
Posts: 1
Joined: Mon Mar 11, 2019 1:22 am

GPIO controls and L298N

Mon Mar 11, 2019 1:36 am

Hello
I have been trying to use python 3 to control a L298N motor module, however this has not been working. the tutorial I followed is likely out dated as the print command for instance no longer work. I am not very good with python and would appreciate any help. I am fairly certain that the issue lies in the code and not the components the code I uploaded is not mine and is the original. Thank you all for your help

Code: Select all

# import curses and GPIO
import curses
import RPi.GPIO as GPIO

#set GPIO numbering mode and define output pins
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7,GPIO.OUT)
GPIO.setup(11,GPIO.OUT)
GPIO.setup(13,GPIO.OUT)
GPIO.setup(15,GPIO.OUT)

# Get the curses window, turn off echoing of keyboard to screen, turn on
# instant (no waiting) key response, and use special values for cursor keys
screen = curses.initscr()
curses.noecho() 
curses.cbreak()
screen.keypad(True)

try:
        while True:   
            char = screen.getch()
            if char == ord('q'):
                break
            elif char == curses.KEY_UP:
                GPIO.output(7,False)
                GPIO.output(11,True)
                GPIO.output(13,False)
                GPIO.output(15,True)
            elif char == curses.KEY_DOWN:
                GPIO.output(7,True)
                GPIO.output(11,False)
                GPIO.output(13,True)
                GPIO.output(15,False)
            elif char == curses.KEY_RIGHT:
                GPIO.output(7,True)
                GPIO.output(11,False)
                GPIO.output(13,False)
                GPIO.output(15,True)
            elif char == curses.KEY_LEFT:
                GPIO.output(7,False)
                GPIO.output(11,True)
                GPIO.output(13,True)
                GPIO.output(15,False)
            elif char == 10:
                GPIO.output(7,False)
                GPIO.output(11,False)
                GPIO.output(13,False)
                GPIO.output(15,False)
             
finally:
    #Close down curses properly, inc turn echo back on!
    curses.nocbreak(); screen.keypad(0); curses.echo()
    curses.endwin()
    GPIO.cleanup()
    

User avatar
joan
Posts: 13869
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: GPIO controls and L298N

Mon Mar 11, 2019 8:49 am

Either correct the code for Python 3 or use Python 2 to run the script.

E.g. python or python2 script.py rather than python3 script.py.

A common error is to be using the wrong GPIO/pins.

The script is using pin numbering. Make sure you are using pins 7/11/13/15.

Clear photos of the connections between the Pi and L298N will help.

pcmanbob
Posts: 5650
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: GPIO controls and L298N

Mon Mar 11, 2019 9:33 am

Hi.

Seeing as you are using board numbering in your python code you should be connecting the IN1-IN4 pins on your L298N to these pins on your pi gpio

Image

You also need a ground connection between the L298N and the pi gpio you can just connect the screw terminal marked GND on the L298N to any of the gpio ground pins shown above.

You should also have jumpers on the ENA & ENB pins on the L298N if they are not present then you will need to connect them to the pi gpio as well.

for ref.

Image
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

B.Goode
Posts: 7571
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: GPIO controls and L298N

Mon Mar 11, 2019 10:43 am

For an up to date tutorial, note that the gpiozero Python library module installed by default with the Raspberry Pi Foundation recommended and supported Raspbian Stretch Operating System has built-in support for Little Yellow Motors controlled by an H-bridge chip such as the one you mention.

Documentation here:
https://gpiozero.readthedocs.io/en/stab ... html#motor See section 14.1.6

simple 'Recipe' here:
https://gpiozero.readthedocs.io/en/stab ... tml#motors See Section 2.20

Return to “General programming discussion”