streetlegend
Posts: 8
Joined: Mon Dec 24, 2018 8:40 pm

Raspberry Pi 3 & L298N - Motor not turning

Mon Dec 24, 2018 8:47 pm

I bought two DROK L298 H-Bridges to control my DC motor (one for backup). I've connected it up to my Raspberry Pi 3 with BCM 12 as my PWM and 16 and 19 as my IN1 and IN2. Below is my code to test if the motor works - but it doesn't turn.

I tested the code on an L293D chip which turns the motor and tested both L298 H-bridges with no success. I can turn the motor on the L298 by connecting the IN1 / IN2 pins to the GND. Looking for thoughts on how to get the motor working on the L298 H-bridge.

Code: Select all

import RPi.GPIO as GPIO
import time

MotorPin1   = 16    # pin36
MotorPin2   = 19    # pin35
MotorEnable = 12    # pin32


GPIO.setmode(GPIO.BCM)          # Numbers GPIOs by BCM
GPIO.setup(MotorPin1, GPIO.OUT)   # mode --- output
GPIO.setup(MotorPin2, GPIO.OUT)
GPIO.setup(MotorEnable, GPIO.OUT)
GPIO.output(MotorEnable, GPIO.LOW) # motor stop
pwm = GPIO.PWM(MotorEnable, 1) # configuring Enable pin (MotorEnable) for PWM)

pwm.start(50) #starting pwm with 50% duty cycle

print 'Press Ctrl+C to end the program...'
print 'Raising...'        
GPIO.output(MotorPin1, GPIO.HIGH)  # clockwise
GPIO.output(MotorPin2, GPIO.LOW)
GPIO.output(MotorEnable, GPIO.HIGH) # motor driver enable
time.sleep(2.5)

GPIO.output(MotorEnable, GPIO.LOW) # motor stop
time.sleep(0.5)

print 'Dropping...'
pwm.ChangeDutyCycle(20) #decreasing dutycycle to 20
GPIO.output(MotorPin1, GPIO.LOW)   # counter-clockwise
GPIO.output(MotorPin2, GPIO.HIGH)
GPIO.output(MotorEnable, GPIO.HIGH) # motor driver enable
time.sleep(1.5)

GPIO.output(MotorEnable, GPIO.LOW) # motor stop
time.sleep(0.5)

pwm.stop()
GPIO.cleanup()                     # Release resource
Pics of connections and spec sheet:

Image

Image

Image

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

Re: Raspberry Pi 3 & L298N - Motor not turning

Tue Dec 25, 2018 10:16 am

Hi.

First disconnect the 5v (red ) cable between the pi and the motor driver board , the motor driver board has its own on board 5v supply.

Then look at you connections again, motor enable you have set on gpio 12 in your code but you appear to have it connected to a ground pin

so counting from the end ( pin 40 ) gpio 16 ( pin 36) is 3 pins from the end which you have correct , But gpio 12 ( pin 32 ) 5 pins up were as you have your connection 4 pins up which is ground
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

streetlegend
Posts: 8
Joined: Mon Dec 24, 2018 8:40 pm

Re: Raspberry Pi 3 & L298N - Motor not turning

Tue Dec 25, 2018 4:24 pm

Thanks for the advice! Definitely had the wrong pin connection for the motor enable - changed it to GPIO 12 (5th pin up).

As for the power - the motor drive board needs an external 5v power supply (there's a light indicator for when there's power).

Making the change, I am still unable to turn the motor with the code. Any other suggestions?

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

Re: Raspberry Pi 3 & L298N - Motor not turning

Tue Dec 25, 2018 8:46 pm

Ok try this code

Code: Select all


import RPi.GPIO as GPIO
import time

MotorPin1   = 16    # pin36
MotorPin2   = 19    # pin35
MotorEnable = 12    # pin32


GPIO.setmode(GPIO.BCM)          # Numbers GPIOs by BCM
GPIO.setup(MotorPin1, GPIO.OUT)   # mode --- output
GPIO.setup(MotorPin2, GPIO.OUT)
GPIO.setup(MotorEnable, GPIO.OUT)
GPIO.output(MotorEnable, GPIO.LOW) # motor stop

print 'Press Ctrl+C to end the program...'
print 'Raising...'        
GPIO.output(MotorPin1, GPIO.HIGH)  # clockwise
GPIO.output(MotorPin2, GPIO.LOW)
GPIO.output(MotorEnable, GPIO.HIGH) # motor driver enable
time.sleep(2.5)

GPIO.output(MotorEnable, GPIO.LOW) # motor stop
time.sleep(0.5)

print 'Dropping...'
pwm.ChangeDutyCycle(20) #decreasing dutycycle to 20
GPIO.output(MotorPin1, GPIO.LOW)   # counter-clockwise
GPIO.output(MotorPin2, GPIO.HIGH)
GPIO.output(MotorEnable, GPIO.HIGH) # motor driver enable
time.sleep(1.5)

GPIO.output(MotorEnable, GPIO.LOW) # motor stop
time.sleep(0.5)


GPIO.cleanup()                     # Release resource

I have removed the pwm from the code to see if you can get the motor to run as full speed first.
I dont think you actually had the pwm set up correctlly as you were still trying to drive the pin high and low as well, if we can get the motor driver board working correctly first then we can sort out your pwm for motor speed once I am back home on my desktop.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

streetlegend
Posts: 8
Joined: Mon Dec 24, 2018 8:40 pm

Re: Raspberry Pi 3 & L298N - Motor not turning

Tue Dec 25, 2018 10:01 pm

Merry Christmas - Thanks for your help!

I tried running the code (removed the pwm reference on line 26) - and the motor still isn't turning. From the reviews for the controller, it seems like there has to be a PWM signal for the controller to work -

Under Reviews - Glen on 16/08/2018
8) The pin labeled ENA1 REQUIRES a PWM output from Arudino. You MUST NOT apply a steady voltage from something like a potentiometer or DAC output.
I'm thinking it's probably an incorrect PWM setup as well.

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

Re: Raspberry Pi 3 & L298N - Motor not turning

Tue Dec 25, 2018 11:12 pm

You could try the following to see if it's a PWM problem.

sudo pigpiod

pigs w 16 0
pigs w 19 1

pigs p 12 128 # 50% dutycycle PWM

pigs p 12 64 # 25%

pigs p 12 192 # 75%

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

Re: Raspberry Pi 3 & L298N - Motor not turning

Wed Dec 26, 2018 12:04 am

Did you try the code I posted with all the pwn lines removed ?

Just driving the pin high is the equivalent of setting the pwn signal to 100% edit .. may not work see below.

Also noted it says you must use 5 v logic to drive board pi gpio is only 3.3v so may not be able to drive this board directly.

Edit....
Reading through some of the other comments about this board it seems you have to start the board with a pwn signal of 0% and raise the pwn signal slowly to start the motor slowly or it will not work.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

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

Re: Raspberry Pi 3 & L298N - Motor not turning

Wed Dec 26, 2018 12:24 am

Looking at the images again it's not actually an L298N motor drive based board anyway, it looks to be just using transistors driven from what I hope are opto isolators.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

streetlegend
Posts: 8
Joined: Mon Dec 24, 2018 8:40 pm

Re: Raspberry Pi 3 & L298N - Motor not turning

Wed Dec 26, 2018 3:25 am

pcmanbob wrote:
Tue Dec 25, 2018 8:46 pm
Ok try this code

Code: Select all


import RPi.GPIO as GPIO
import time

MotorPin1   = 16    # pin36
MotorPin2   = 19    # pin35
MotorEnable = 12    # pin32


GPIO.setmode(GPIO.BCM)          # Numbers GPIOs by BCM
GPIO.setup(MotorPin1, GPIO.OUT)   # mode --- output
GPIO.setup(MotorPin2, GPIO.OUT)
GPIO.setup(MotorEnable, GPIO.OUT)
GPIO.output(MotorEnable, GPIO.LOW) # motor stop

print 'Press Ctrl+C to end the program...'
print 'Raising...'        
GPIO.output(MotorPin1, GPIO.HIGH)  # clockwise
GPIO.output(MotorPin2, GPIO.LOW)
GPIO.output(MotorEnable, GPIO.HIGH) # motor driver enable
time.sleep(2.5)

GPIO.output(MotorEnable, GPIO.LOW) # motor stop
time.sleep(0.5)

print 'Dropping...'
pwm.ChangeDutyCycle(20) #decreasing dutycycle to 20
GPIO.output(MotorPin1, GPIO.LOW)   # counter-clockwise
GPIO.output(MotorPin2, GPIO.HIGH)
GPIO.output(MotorEnable, GPIO.HIGH) # motor driver enable
time.sleep(1.5)

GPIO.output(MotorEnable, GPIO.LOW) # motor stop
time.sleep(0.5)


GPIO.cleanup()                     # Release resource

I have removed the pwm from the code to see if you can get the motor to run as full speed first.
I dont think you actually had the pwm set up correctlly as you were still trying to drive the pin high and low as well, if we can get the motor driver board working correctly first then we can sort out your pwm for motor speed once I am back home on my desktop.
Yes, I've tried running the code with all PWM lines removed - but still no motor turning

streetlegend
Posts: 8
Joined: Mon Dec 24, 2018 8:40 pm

Re: Raspberry Pi 3 & L298N - Motor not turning

Wed Dec 26, 2018 3:26 am

joan wrote:
Tue Dec 25, 2018 11:12 pm
You could try the following to see if it's a PWM problem.

sudo pigpiod

pigs w 16 0
pigs w 19 1

pigs p 12 128 # 50% dutycycle PWM

pigs p 12 64 # 25%

pigs p 12 192 # 75%
Tried the following code - no motor turning.

streetlegend
Posts: 8
Joined: Mon Dec 24, 2018 8:40 pm

Re: Raspberry Pi 3 & L298N - Motor not turning

Wed Dec 26, 2018 3:29 am

pcmanbob wrote:
Wed Dec 26, 2018 12:24 am
Looking at the images again it's not actually an L298N motor drive based board anyway, it looks to be just using transistors driven from what I hope are opto isolators.
At this point I think it'll be worth it to just order an actual L298N motor board - it seems like there's a lot more support for it and less unknowns.

rhubarbdog
Posts: 64
Joined: Mon Dec 11, 2017 9:58 pm

Re: Raspberry Pi 3 & L298N - Motor not turning

Wed Dec 26, 2018 6:34 am

PWM is only available on a few pins i know BCM 18 is one of them

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

Re: Raspberry Pi 3 & L298N - Motor not turning

Wed Dec 26, 2018 10:18 am

You can use software based PWM on any pin, you just have to be aware that software based PWM can suffer interruption due other processes running on the pi , but it should be fine for testing a motor driver board.

streetlegend.
If you have a bench power supply that's capable of supplying 5v and 3.3v and a meter you could do some testing to prove how the motor driver board works.

Replace the motor with the meter then try driving the board just using 5v and ground connections to the control pins, if you can get it to work at 5v then try using 3.3v on the control pins in the same way to see if it will work.

If you can prove it works like this then you can try again with the pi to control it.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Brandon92
Posts: 773
Joined: Wed Jul 25, 2018 9:29 pm
Location: Netherlands

Re: Raspberry Pi 3 & L298N - Motor not turning

Wed Dec 26, 2018 1:42 pm

When I take a look at the different pictures. You need to connect the "5v" connection to the 3.3V of the Rpi. This is the "same" approach by a lot of the relay boards. However, I don't know if the Rpi is "strong" enough to drive the input of the optocouples.

So, if you can. Can you post the part number of the optocoupler and the value of the resistors that are below that part. The part R1 (on the bottum) and U4 (on the top). Then we know if you need some external parts to drive the input. (if you can't read it, you also post a picture of that part).

streetlegend
Posts: 8
Joined: Mon Dec 24, 2018 8:40 pm

Re: Raspberry Pi 3 & L298N - Motor not turning

Thu Dec 27, 2018 12:10 am

Brandon92 wrote:
Wed Dec 26, 2018 1:42 pm
When I take a look at the different pictures. You need to connect the "5v" connection to the 3.3V of the Rpi. This is the "same" approach by a lot of the relay boards. However, I don't know if the Rpi is "strong" enough to drive the input of the optocouples.

So, if you can. Can you post the part number of the optocoupler and the value of the resistors that are below that part. The part R1 (on the bottum) and U4 (on the top). Then we know if you need some external parts to drive the input. (if you can't read it, you also post a picture of that part).
R1 - 511
U4 - EL 357N C829

Top
Image

Bottom
Image

streetlegend
Posts: 8
Joined: Mon Dec 24, 2018 8:40 pm

Re: Raspberry Pi 3 & L298N - Motor not turning

Thu Dec 27, 2018 12:22 am

Brandon92 wrote:
Wed Dec 26, 2018 1:42 pm
When I take a look at the different pictures. You need to connect the "5v" connection to the 3.3V of the Rpi. This is the "same" approach by a lot of the relay boards. However, I don't know if the Rpi is "strong" enough to drive the input of the optocouples.
Changing the connection from 5v to 3.3V worked! Motor turns and PWM functions perfectly! Thanks Brandon92, pcmanbob, joan, and rhubarbdog for the advice!

Return to “Beginners”