chetuk
Posts: 48
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: 4257
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),Aeromodelling,1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

chetuk
Posts: 48
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: 48
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: 664
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: 48
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: 4462
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: 48
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!

Return to “Automation, sensing and robotics”