KingOfTheJungle
Posts: 16
Joined: Wed Nov 22, 2017 7:09 pm

How to keep motor running until interrupt arrives?

Sat Jan 20, 2018 8:53 pm

I'm trying to control a motor attached to my pi through a webserver. The following code works but only when i include the sleep(2) line.

Code: Select all

GPIO.setmode(GPIO.BOARD)
GPIO.setup(12, GPIO.OUT)
p = GPIO.PWM(12, 100)
sleep(2)
GPIO.output(12, GPIO.HIGH)
p.stop()
GPIO.cleanup()


I want to know how to keep the motor running until an interrupt arrives so I have control when the motor starts and stops. When i remove the sleep(2) line, the motor doesn't turn on. Anyone know why and how I can get around this?

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

Re: How to keep motor running until interrupt arrives?

Sat Jan 20, 2018 9:14 pm

First why are you driving gpio 12 high if you are using it for pwm ?

The reason your motor stops is that you have the line p.stop() in your code which stops the pwm which in turn stops you motor.

You need to replace the sleep with what ever code you are going to use to await the interupt or await the command to stop the motor.
Please only ask questions in the forum I will not answer questions sent to me directly via PM
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

KingOfTheJungle
Posts: 16
Joined: Wed Nov 22, 2017 7:09 pm

Re: How to keep motor running until interrupt arrives?

Sat Jan 20, 2018 9:30 pm

Thanks. I feel like an idiot.
I set it to high because that's how you turn the motor on?

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

Re: How to keep motor running until interrupt arrives?

Sun Jan 21, 2018 11:49 am

What motor driver are you using to run this motor as you can connect it directly to the gpio on the pi.

most H bridge motor drivers use 2 or 3 control wires per motor.

2 for direction which you drive high or low and one for enable this is normally the one you apply the pwm signal to that controls the speed.

if you are just using a simple single transistor then you apply the pwm signal to the base of the transistor and this turn on the motor and controls its speed.
Please only ask questions in the forum I will not answer questions sent to me directly via PM
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

KingOfTheJungle
Posts: 16
Joined: Wed Nov 22, 2017 7:09 pm

Re: How to keep motor running until interrupt arrives?

Tue Jan 23, 2018 1:48 pm

I've set it up like this: https://javatutorial.net/raspberry-pi-c ... otor-speed
I just want to control the speed not direction.

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

Re: How to keep motor running until interrupt arrives?

Tue Jan 23, 2018 2:12 pm

In that case all you need to do is apply the PWM signal to the gpio that you are using to turn on the ULN2003 output for your motor.

Heres an example program I wrote to demonstrate a fan speed control but it should change your motor speed just the same.

Code: Select all

#  python2.7  

import RPi.GPIO as GPIO 
from time import sleep  
      
GPIO.setmode(GPIO.BOARD)  
      
GPIO.setup(12, GPIO.OUT)

fan = GPIO.PWM(12, 100)  
print "start"  
fan.start(0)              
sleep (5)  
print "Run 100%"   
fan.ChangeDutyCycle(100)
sleep(0.5)
speed = [15,25,50,75,100]
for sp in speed:
    print "Run", sp,"%"
    fan.ChangeDutyCycle(sp)      
    sleep(10)
fan.ChangeDutyCycle(0) 
print "Stop"
sleep (5)  
print "Run 100%"   
fan.ChangeDutyCycle(100)
sleep(0.5)  
for sp in range(15,101,5):
    print "Run", sp,"%"
    fan.ChangeDutyCycle(sp)      
    sleep(2)
sleep(5)
print "Stop"
fan.stop()     
GPIO.cleanup()

from this example you should be able to work out how to control your motor.
Please only ask questions in the forum I will not answer questions sent to me directly via PM
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

Return to “Python”

Who is online

Users browsing this forum: No registered users and 6 guests