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

Trying to turn on motor nothing happens.

Tue Jan 23, 2018 2:05 pm

Code: Select all

try:
	GPIO.setmode(GPIO.BOARD)
	GPIO.setup(12, GPIO.OUT)
	p = GPIO.PWM(12, 100)
	print("motor on")
	p.start(100)
	
except KeyboardInterrupt:
	p.stop()
	GPIO.cleanup()
	sys.exit()
except:
	print("other error occurred")
The python code prints "motor on" but the motor doesn't come on. Anyone able to help me out with this? Thanks.
Last edited by KingOfTheJungle on Tue Jan 23, 2018 3:07 pm, edited 1 time in total.

mattmiller
Posts: 1953
Joined: Thu Feb 05, 2015 11:25 pm

Re: Trying to turn on motor nothing happens.

Tue Jan 23, 2018 2:39 pm

You've not given speed a value
OP altered post
Last edited by mattmiller on Tue Jan 23, 2018 3:33 pm, edited 1 time in total.

User avatar
rpiMike
Posts: 488
Joined: Fri Aug 10, 2012 12:38 pm
Location: Cumbria, UK

Re: Trying to turn on motor nothing happens.

Tue Jan 23, 2018 2:46 pm

Have you got the correct pin ?

https://pinout.xyz/pinout/pin12_gpio18

Post a picture of your wiring.

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

Re: Trying to turn on motor nothing happens.

Tue Jan 23, 2018 3:08 pm

mattmiller wrote:
Tue Jan 23, 2018 2:39 pm
You've not given speed a value
I have, it was declared earlier in the script. Even if i give it a value nothing happens.

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

Re: Trying to turn on motor nothing happens.

Tue Jan 23, 2018 3:11 pm

rpiMike wrote:
Tue Jan 23, 2018 2:46 pm
Have you got the correct pin ?

https://pinout.xyz/pinout/pin12_gpio18

Post a picture of your wiring.
setup like this. http://javatutorial.net/wp-content/uplo ... ematic.png


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

Re: Trying to turn on motor nothing happens.

Tue Jan 23, 2018 3:38 pm

So looking at your code

your program just runs through the code once and then stops but it runs through and stops before it can actually complete the p.start(100) command.

so adding a simple sleep (0.5) allows time for p.start (100) to be setup.

Code: Select all

import RPi.GPIO as GPIO 
import time  
import sys     
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)

try:
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(12, GPIO.OUT)
    p = GPIO.PWM(12, 100)
    print("motor on")
    p.start(100)
    time.sleep(0.5)
        
except KeyboardInterrupt:
    p.stop()
    GPIO.cleanup()
    sys.exit()

except:
    print("other error occurred")
    

secondly you will never get to use the keyboard interrupt as your program is not looping and will end before you ever get to press the required keys so the stop and clean up will never be run.
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: Trying to turn on motor nothing happens.

Tue Jan 23, 2018 4:18 pm

pcmanbob wrote:
Tue Jan 23, 2018 3:38 pm
So looking at your code

your program just runs through the code once and then stops but it runs through and stops before it can actually complete the p.start(100) command.

so adding a simple sleep (0.5) allows time for p.start (100) to be setup.

Code: Select all

import RPi.GPIO as GPIO 
import time  
import sys     
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)

try:
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(12, GPIO.OUT)
    p = GPIO.PWM(12, 100)
    print("motor on")
    p.start(100)
    time.sleep(0.5)
        
except KeyboardInterrupt:
    p.stop()
    GPIO.cleanup()
    sys.exit()

except:
    print("other error occurred")
    

secondly you will never get to use the keyboard interrupt as your program is not looping and will end before you ever get to press the required keys so the stop and clean up will never be run.
Thanks for your response! How do I keep the program looping? Also I need the motor running continuously until an interrupt arrives. So won't the time.sleep(0.5) line, make it stop and not run continuously?

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

Re: Trying to turn on motor nothing happens.

Tue Jan 23, 2018 4:26 pm

Also when I run the script from using Thonny IDE it runs fine, but when I run it from the command line, it prints "motor on" but the motor doesn't come on.

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

Re: Trying to turn on motor nothing happens.

Tue Jan 23, 2018 4:33 pm

As your program stands with the time.sleep inserted, the motor will just keep running when the program stops because no pwm.stop was ever run, so you would need another program to run to stop the PWM.

if you want to loop your program you need to use a while loop, it can then check for you condition to be true and so exit.

https://www.tutorialspoint.com/python/p ... e_loop.htm
https://wiki.python.org/moin/WhileLoop
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: Hollerer and 18 guests