2

I have used the raspberry pi for almost 5 months now, so I could say I am new.

Everything was fine at the start but when I built a car my motors will not spin. I am using DC motors (approximately 9V) and an L298N as well as a 9v battery and of course the Raspberry Pi.

What should I do?

Info below:

I connected each DC motors to each of the boxes at the side of the L298N board. I connected the 9V battery to the +12V and the GND in the L298N and then the LED on the L298N lit up.

I also connected the ground in the L298N to the Raspberry Pi pin 6. Then I connected the four pins for controlling the motors to pins 37, 35, 33, 31 in the Raspberry Pi.

My code to spin the motor:

import RPi.GPIO as GPIO

import time

GPIO.setmode(GPIO.BOARD)

GPIO.setup(37, GPIO.OUT)

GPIO.output(37, True)

time.sleep(2)

GPIO.output(37, False)

GPIO.cleanup()
4
  • The code is wrong, it needs to set both GPIO to different values to spin the motor. The connections may also be wrong and have you enabled the enable lines? Clear photos of the connections may be useful.
    – joan
    Commented Mar 31, 2019 at 14:29
  • The l298n board uses 5V logic and the PI will be outputting 3.3V signals. In the datasheet it states the minimum logical voltage as 4.5V. Are you sure that the output for the motor driver is actually going high? Have you measured the output voltage with a Voltmeter? Also have you connected the GND of the Raspberry Pi to the GND of the motor driver? The logic signals need a common ground reference. Commented Mar 31, 2019 at 14:30
  • You might like to first check if all wiring are correct by manually, by hand, using jumper wires to select +5V and Ground signals to input to the 3 pin control connector, to move the motor forward and backward. If everything OK, then you can start writing the Rpi Python program. You can google L298 tutorials to get familiar with the wiring.
    – tlfong01
    Commented Apr 1, 2019 at 4:05
  • @NathanielJS - I think you have misinterpreted the data sheet. The L298 minimum logic supply voltage is 4.5V. The minimum logic input high voltage is 2.3V. The Raspberry Pi, or other 3.3V device, can control the L298 without problems. Commented Mar 10 at 18:30

1 Answer 1

2

I would suggest to do the testing in 2 steps:

(1) Check by hand if jumper wire 5V, Gnd signals can move motor forward and backward.

(2) If motor can turn manually, then start writing the python program.

I googled and found almost all the L298N modules have the similar input and output terminal connectors. The following is my quick and dirty hardware wiring check. If you find this test OK, I can show you a very simple python program.

Wiring Notes:

(1) Remove jumper connecting on board 5V 7805 voltage regulator, If you are using external 5V power for both motor and control logic.

(2) Don't forget to short sensor jumper to ground, otherwise motor won't turn.

enter image description here

Now I have written a python program to move motor forward and backward.

Youtube showing motor moving L298N Motor

# *** l298n_test04_ tlfong01 2019apr01hkt1627 ***
import RPi.GPIO as GPIO
from   time import sleep

# *** Description ***
# Move motor one direction, then reverse direction.

# *** Notes ***
# Only motor 1 tested

# *** Config ***

enablePin1 =  11 # (SPI SCL)
inPin11     = 10 # (SPI MOSI)
inPin12     =  9 # (SPI MISO)

motorPinList1 = [enablePin1, inPin11, inPin12]
motorPinList2 = [enablePin1, inPin11, inPin12] 

motorPinListList  = [motorPinList1, motorPinList1]

# *** GPIO Setup / Cleanup ***

def setGpioMode():
    GPIO.setwarnings(False) 
    GPIO.setmode(GPIO.BCM)
    return

def cleanupGpio(): 
    GPIO.cleanup()
    return

# *** Gpio Pin SetupOutput / SetHighLow ***

def setupGpioPinOutLow(gpioPin):
    GPIO.setup(gpioPin, GPIO.OUT)
    GPIO.output(gpioPin, GPIO.LOW)  
    return

def setGpioPinHigh(gpioPin):  
    GPIO.output(gpioPin, GPIO.HIGH)
    return

def setGpioPinLow(gpioPin): 
    GPIO.output(gpioPin, GPIO.LOW)
    return

# *** Toggle GpioPin (for debugging/troubleshooting only) ***

def toggleGpioPin(gpioPin, highTime, lowTime, toggleCount):
    for i in range(toggleCount):
        setGpioPinHigh(gpioPin)
        sleep(highTime)
        setGpioPinLow(gpioPin)
        sleep(lowTime)
    return

# *** Setup/Cleanup GPIO ***

def setup(gpioPinList):
    setGpioMode()
    for gpioPin in gpioPinList:
        setupGpioPinOutLow(gpioPin)
    return    

def cleanup():
    cleanupGpio()
    return

# *** Motor Functions ***

def moveMotor(motorNum, direction, holdTime):
    enablePin = motorPinListList[motorNum][0]
    inPin1    = motorPinListList[motorNum][1]
    inPin2    = motorPinListList[motorNum][2]

    setGpioPinHigh(enablePin)

    if direction == 'CW':
       setGpioPinLow(inPin1)
       setGpioPinHigh(inPin2)
    else: 
       setGpioPinHigh(inPin1)
       setGpioPinLow(inPin2)
    sleep(holdTime)

    setGpioPinLow(enablePin)    
    return

# *** Test Functions ***

def testToggleMotorPinList(motorNum):
    print('  Begin toggle gpioPin ...')
    motorPinList = motorPinListList[motorNum]
    testCount   = 10000
    highTime    = 1
    lowTime     = 1
    toggleCount = 4    
    for test in range(testCount):
        for gpioPin in motorPinList:    
            toggleGpioPin(gpioPin, highTime, lowTime, toggleCount)
    print('  End   toggleGpio0Pin.')
    return
  
def test(motorNum):
    print('Begin test(), ...')
    setup(motorPinListList[motorNum])

    sleep(4)

    moveMotor(motorNum = 0, direction = 'CW',  holdTime = 2)
    moveMotor(motorNum = 0, direction = 'CCW', holdTime = 4)

    sleep(2)
    
    moveMotor(motorNum = 1, direction = 'CCW',  holdTime = 6) # Just debugging, still motor 0!
    moveMotor(motorNum = 1, direction = 'CW',   holdTime = 3) # Just debugging, still Motor 0!
    
    cleanup()
    print('End   test().')
    return

# *** Main ***

test(motorNum = 0)

# *** End ***

 

Final wiring diagram

Newbie reminder - connect Rpi Power Ground to L298N module power ground, otherwise motor won't move!

enter image description here

Update 2019apr04hkt1628 - All step motors jumpers off

All Stepping Motor Jumpers Removed

Good luck!

Recommending A Motor Driver Better Than L298N

After answering the OP's question, I began to worry that other newbies might think I like this L298N driver and encourage others to use it. Actually don't like this old chip at all, because new generation chips such as TB6612FNG are much smaller, more efficient, and not that harder to use. The picture below shows how smaller is the TB6612FNG module.

enter image description here

The Review of DC Motor Drivers - L298N, TB6612FNG and LV8406T

.END

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.