chetuk
Posts: 49
Joined: Mon Feb 20, 2017 7:27 am

how to control speed of dc motor connected to pi & l298n ?

Sun Jan 21, 2018 11:47 am

How to control speed of dc motor connected to pi & l298n ?
Feels robo is too fast and getting out of control to sense line ..

/Chetan S.
Last edited by chetuk on Mon Feb 19, 2018 6:24 am, edited 2 times in total.

User avatar
PeterO
Posts: 4385
Joined: Sun Jul 22, 2012 4:14 pm

Re: how to control speed of dc motor connected to pi & l298n ?

Sun Jan 21, 2018 11:55 am

This might give you some ideas

https://github.com/CamJam-EduKit/EduKit ... ration.pdf

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

chetuk
Posts: 49
Joined: Mon Feb 20, 2017 7:27 am

Re: how to control speed of dc motor connected to pi & l298n ?

Sun Jan 21, 2018 12:01 pm

Thanks !! PeterO

/Chetan


chetuk
Posts: 49
Joined: Mon Feb 20, 2017 7:27 am

Re: how to control speed of dc motor connected to pi & l298n ?

Mon Jan 29, 2018 4:57 pm

this is my code the robo run too fast ...please help to add code so to slower down speed as i desire/???

-------------------------------------------------------------------------------------------------------------------------------------------------------
#!/usr/bin/python
import RPi.GPIO as GPIO #Import GPIO library
from espeak import espeak
import time
#Import time library
GPIO.setwarnings(False)
#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)
GPIO.setup(37,GPIO.OUT) # Trigger
GPIO.setup(24,GPIO.IN) # Echo
time.sleep(1)
print "Ultrasonic Measurement"
GPIO.output(37, False)

def measure():
time.sleep(0.333)
GPIO.output(37, True)
time.sleep(0.00001)
GPIO.output(37, False)
start = time.time()

while GPIO.input(24)==0:
start = time.time()

while GPIO.input(24)==1:
stop = time.time()

elapsed = stop-start
distance = (elapsed * 34300)/2
return distance
def stop():
GPIO.output(7,False)
GPIO.output(11,False)
GPIO.output(13,False)
GPIO.output(15,False)
def forward():
GPIO.output(7,True)
GPIO.output(11,False)
GPIO.output(13,True)
GPIO.output(15,False)
def back():
GPIO.output(7,False)
GPIO.output(11,True)
GPIO.output(13,False)
GPIO.output(15,True)
def left():
GPIO.output(7,True)
GPIO.output(11,False)
GPIO.output(13,False)
GPIO.output(15,True)
def right():
GPIO.output(7,False)
GPIO.output(11,True)
GPIO.output(13,True)
GPIO.output(15,False)
try:

while True:

distance = measure()
print "Distance : %.1f" % distance
time.sleep(0.0)
if distance >= 130:
forward()
else:
stop()
except KeyboardInterrupt:
#cleanup the GPIO pins before ending
GPIO.cleanup()

User avatar
OutoftheBOTS
Posts: 702
Joined: Tue Aug 01, 2017 10:06 am

Re: how to control speed of dc motor connected to pi & l298n ?

Mon Jan 29, 2018 8:14 pm

at the moment your just turning on your gpio pin to activate the l298n to make the motor spin at full speed. instead you need to use PWM duty cycle on that pin to adjust the speed needed

google "raspbery pi pwm duty cycle"

chetuk
Posts: 49
Joined: Mon Feb 20, 2017 7:27 am

Re: how to control speed of dc motor connected to pi & l298n ?

Mon Feb 12, 2018 6:10 am

Any suggestion in code i tried search in google but confused .

/Chetan S.


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

Re: how to control speed of dc motor connected to pi & l298n ?

Mon Feb 12, 2018 11:16 am

to control the motor speed on your L298N you simply need to appy a PWM signal to the ena & enb inputs ( you may need to remove the jumps from these pins )

this is an example of PWM controlling a fan speed but it will work exactly the same for your motor speed control.

Code: Select all

#!/usr/bin/env python2.7  

import RPi.GPIO as GPIO 
from time import sleep  
      
GPIO.setmode(GPIO.BCM)  
      
GPIO.setup(25, GPIO.OUT)

fan = GPIO.PWM(25, 100)  
print "start"  
fan.start(0) 
             
sleep (5)  
print "Run 100%"   
fan.ChangeDutyCycle(100)

sleep (5)
print "Run 50%" 
fan.ChangeDutyCycle(50)

sleep (5)
print "Run 25%" 
fan.ChangeDutyCycle(25)

sleep(5)
print "Stop"
fan.stop()     
GPIO.cleanup()
and please when posting code use code tags

Add [code] at the top

code goes here

and [/code] at the bottom.
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

chetuk
Posts: 49
Joined: Mon Feb 20, 2017 7:27 am

Re: how to control speed of dc motor connected to pi & l298n ?

Wed Feb 14, 2018 7:44 am

Thanks! pcmanbob

Manage to achieve speed control

Cheers!

arindamsingha
Posts: 1
Joined: Thu Jan 03, 2019 2:34 pm

Re: how to control speed of dc motor connected to pi & l298n ?

Thu Jan 03, 2019 2:40 pm

Dear Chetuk sir, could you please send me your demo code, I have tried all the processes available on the internet for using PWM signal to control the speed of the motor, but none of this is worked. I will be really grateful if you kindly give me your demo code, just to control the speed through PWM using a raspberry pi. Just give me a reply, I'll give you my email id.

Thanx in advance.

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

Re: how to control speed of dc motor connected to pi & l298n ?

Fri Jan 04, 2019 1:18 pm

arindamsingha wrote:
Thu Jan 03, 2019 2:40 pm
Dear Chetuk sir, could you please send me your demo code, I have tried all the processes available on the internet for using PWM signal to control the speed of the motor, but none of this is worked. I will be really grateful if you kindly give me your demo code, just to control the speed through PWM using a raspberry pi. Just give me a reply, I'll give you my email id.

Thanx in advance.
It might help if you show us your code and tell us how you tried to enable speed control , on most L298N motor controllers you only have to apply a PWM signal to the ENA & ENB pins on the motor control board to get speed control.

Image

on this example the pins are marked " A enable " & " B enable " you would need to remove the existing jumpers if fitted as on this example board.

please when you post code use code tags

Add [code] at the top

code goes here

and [/code] at the bottom.
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

Erklei
Posts: 4
Joined: Fri Apr 13, 2018 3:39 pm

Re: how to control speed of dc motor connected to pi & l298n ?

Sun Jan 06, 2019 9:30 am

If you apply PWM to enable pin you get the motor run-coast-run-coast-run-coast.
If you apply PWM to input pin you get the motor run-brake-run-brake-run-brake

Be also aware that L298 is only good for motors with running current less than lets say 1A. Its big energy waster and tends to overheat and shut down.

Return to “Automation, sensing and robotics”