drekthrall
Posts: 44
Joined: Sat Oct 17, 2015 12:43 pm

permanently running PWM

Mon Oct 26, 2015 4:47 pm

Hello,

my simple python code :

Code: Select all

#!/usr/bin/env python
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setup(25, GPIO.OUT)
p = GPIO.PWM(25,200)
p.start(50)
If I execute this script 25GPIO will turn on (on 50%) and immediately turn off. My LED just once blinked.

If I execute script like this :

Code: Select all

import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setup(25, GPIO.OUT)
GPIO.output(25, True)
The LED will be turned on "forever".

My question is how to run PWM "forever" without any loops like while ... ? Thanks for any response !

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 14230
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: permanently running PWM

Mon Oct 26, 2015 5:41 pm

You just turn PWM on, and it will run "forever" until you give a new PWM control, that turns it off.

User avatar
B.Goode
Posts: 13220
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: permanently running PWM

Mon Oct 26, 2015 5:44 pm

mahjongg wrote:You just tun PWM on, and it will run "forever" until you give a new PWM control, that turns it off.
What happens when the controlling Python script terminates? Is that the end of 'forever'?

drekthrall
Posts: 44
Joined: Sat Oct 17, 2015 12:43 pm

Re: permanently running PWM

Mon Oct 26, 2015 5:58 pm

mahjongg wrote:You just tun PWM on, and it will run "forever" until you give a new PWM control, that turns it off.
That is not working for me , i need to add time.sleep(x) to let PWM running for x seconds , after time.sleep ends PWM ends too :/

User avatar
rpdom
Posts: 19287
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: permanently running PWM

Mon Oct 26, 2015 6:08 pm

The software PWM is controlled by the python RPi.GPIOlibrary. As soon as your program terminates it will stop working. You can't do anything about that.

The hardware PWM would keep running independently of any program, but there is only one of those normally available.

drekthrall
Posts: 44
Joined: Sat Oct 17, 2015 12:43 pm

Re: permanently running PWM

Mon Oct 26, 2015 7:02 pm

rpdom wrote:The software PWM is controlled by the python RPi.GPIOlibrary. As soon as your program terminates it will stop working. You can't do anything about that.

The hardware PWM would keep running independently of any program, but there is only one of those normally available.
Thanks ! That was very helpful for me ! Thanks for help :)

drekthrall
Posts: 44
Joined: Sat Oct 17, 2015 12:43 pm

Re: permanently running PWM

Mon Oct 26, 2015 7:07 pm

rpdom wrote:The software PWM is controlled by the python RPi.GPIOlibrary. As soon as your program terminates it will stop working. You can't do anything about that.

The hardware PWM would keep running independently of any program, but there is only one of those normally available.
One more question please :D ... Is there any Raspberry pi ( 2 A,B ,B+ , ...) that has 2 (or more) HW PWM ? If no, is there any HW PWM module ?

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

Re: permanently running PWM

Mon Oct 26, 2015 7:55 pm

pigpio will let you use the two hardware PWM channels available on the A+/B+/Pi2/compute module. It well also let you run hardware timed PWM on the other GPIOs even when the Python script is terminated (as long as the pigpio daemon is left running).

drekthrall
Posts: 44
Joined: Sat Oct 17, 2015 12:43 pm

Re: permanently running PWM

Mon Oct 26, 2015 9:38 pm

joan wrote:pigpio will let you use the two hardware PWM channels available on the A+/B+/Pi2/compute module. It well also let you run hardware timed PWM on the other GPIOs even when the Python script is terminated (as long as the pigpio daemon is left running).

Thanks !
Only for sure Raspberry Pi 2 B has two hardware PWMs ?

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

Re: permanently running PWM

Mon Oct 26, 2015 9:42 pm

drekthrall wrote: ...
Only for sure Raspberry Pi 2 B has two hardware PWMs ?
http://abyz.co.uk/rpi/pigpio/python.html#hardware_PWM

Return to “General discussion”