pwm with wiringPi Python?


14 posts
by andig2 » Fri Dec 14, 2012 4:13 pm
I'm trying to get pwm working by "dimming" an LED up and down:

Code: Select all
def pwm_dimm(io):
    pin = 1 # only supported on this pin
    io.pinMode(pin,io.OUTPUT)

    while 1:
        print "Up"
        for i in range(0,1024,16):
            io.pwmWrite(pin, i)
            time.sleep(DELAY)
        print "Down"
        for i in range(1024,0,-16):
            io.pwmWrite(pin, i)
            time.sleep(DELAY)


This does very much not work- the LED just stays on. The pin is correctly wired as using digitalWrite I can flash the LED.

Any ideas?
Posts: 23
Joined: Wed Oct 31, 2012 9:34 pm
by gordon@drogon.net » Fri Dec 14, 2012 7:02 pm
andig2 wrote:I'm trying to get pwm working by "dimming" an LED up and down:

Code: Select all
def pwm_dimm(io):
    pin = 1 # only supported on this pin
    io.pinMode(pin,io.OUTPUT)

    while 1:
        print "Up"
        for i in range(0,1024,16):
            io.pwmWrite(pin, i)
            time.sleep(DELAY)
        print "Down"
        for i in range(1024,0,-16):
            io.pwmWrite(pin, i)
            time.sleep(DELAY)


This does very much not work- the LED just stays on. The pin is correctly wired as using digitalWrite I can flash the LED.

Any ideas?


I'm not a Python programmer, but I did write wiringPi ;-)

Before you use the single PWM pin on your Pi, you need to set it into PWM mode - and I don't see that in your code above - I do see you setting it to an output though. This won't work. Do whatever you need to do via the Python wrappers to set it into PWM mode. Maybe:

io.pinMode(pin,io.PWM)

?

You also need to call some sort of wiringPi setup code too, but maybe you're doing this elsewhere.

And remember that there are 2 ways to initialise wiringPi - the wiring native mode uses pin 1 (which you're using here), or the hardware bcm-gpio mode when it would be pin 18.

(and I wish I've never bothered listening to people who wanted the bcm_pin numbering mode and stuck to wiringPi numbers, but that's another story!)

-Gordon
--
Gordons projects: https://projects.drogon.net/
User avatar
Posts: 1421
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
by andig2 » Fri Dec 14, 2012 7:33 pm
Hi Gordon,
This is excellent, thanks a lot! I did do the setup but forgot PWM mode.
Thanks for your excellent piece of software!
Kind regards, Andreas
Posts: 23
Joined: Wed Oct 31, 2012 9:34 pm
by cool » Tue Dec 18, 2012 6:36 am
hi..

can you write down exact program to run this project...

because i can understand..
i want run this project...

thank you
Posts: 6
Joined: Tue Dec 18, 2012 6:34 am
by andig2 » Tue Dec 18, 2012 8:53 am
Sure, check this out:

Code: Select all
import wiringpi, time

DELAY_PWM = 0.005

def reset_pins(io):
    pins = [0,1,2,3,4,5,6,7, ]
    for pin in pins:
   io.pinMode(pin,io.OUTPUT)
   io.digitalWrite(pin, io.LOW)

def pwm_dimm(io):
    STEP = 8
    pin = 1 # only supported on this pin
    io.pinMode(pin,io.PWM_OUTPUT)

    while 1:
   #print "Up"
   for i in range(0,1024,STEP):
       io.pwmWrite(pin, i)
       time.sleep(DELAY_PWM)
   #print "Down"
   for i in range(1023,-1,-STEP):
       io.pwmWrite(pin, i)
       time.sleep(DELAY_PWM)


# direct
#io = wiringpi.GPIO(wiringpi.GPIO.WPI_MODE_SYS)
io = wiringpi.GPIO(wiringpi.GPIO.WPI_MODE_PINS)

try:
    reset_pins(io)   
    pwm_dimm(io)

except (KeyboardInterrupt, SystemExit):
    reset_pins(io)
Posts: 23
Joined: Wed Oct 31, 2012 9:34 pm
by cool » Tue Dec 18, 2012 9:02 am
thanks,


and which pin is used here?
Posts: 6
Joined: Tue Dec 18, 2012 6:34 am
by andig2 » Tue Dec 18, 2012 9:21 am
Well- the code says pin=1, right? Check the WiringPi docs which one that is.
Posts: 23
Joined: Wed Oct 31, 2012 9:34 pm
by gordon@drogon.net » Tue Dec 18, 2012 9:25 am
andig2 wrote:Well- the code says pin=1, right? Check the WiringPi docs which one that is.


wiringPi pin 1 is the chip's GPIO pin 18.

(that's the only hardware PWM pin avalable - easilly)

My diagram is here:

https://projects.drogon.net/raspberry-pi/wiringpi/pins/

-Gordon
--
Gordons projects: https://projects.drogon.net/
User avatar
Posts: 1421
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
by cool » Tue Dec 18, 2012 9:33 am
thanks
but still running error..

please list all step ,not about program but how can i excess it in terminal window,
from 1sr step to last
Posts: 6
Joined: Tue Dec 18, 2012 6:34 am
by cool » Tue Dec 18, 2012 10:01 am
i am new for this wiringpi so, when i have run this code it says that wiringPi modual nit found

so what can i do??
Posts: 6
Joined: Tue Dec 18, 2012 6:34 am
by gordon@drogon.net » Tue Dec 18, 2012 10:10 am
cool wrote:i am new for this wiringpi so, when i have run this code it says that wiringPi modual nit found

so what can i do??


I'm not sure - this is really a thread about with Python wrappers for wiringPi - which I didn't create (I just did the wiringPi part - which is in C).

Maybe you need to install the wiringPi/Python package first?

This is the location for that: https://github.com/wiringPi

-Gordon
--
Gordons projects: https://projects.drogon.net/
User avatar
Posts: 1421
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
by cool » Wed Dec 19, 2012 4:35 am
this program didnot work
Posts: 6
Joined: Tue Dec 18, 2012 6:34 am
by texy » Thu Dec 20, 2012 8:37 am
You need to install 'wiringpi for python', not wiringpi native as that is for C only.
See here :
viewtopic.php?f=32&t=10010&start=100#p162668

Texy
"2.8inch TFT LCD + Touch screen" add-on boards for sale here :
http://www.raspberrypi.org/phpBB3/viewtopic.php?f=93&t=65566
50p goes to the Foundation ;-)
Moderator
Posts: 2202
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England
by andig2 » Thu Dec 20, 2012 9:24 am
cool,
you'll need to show some interest and engagement in resolving your problems. If you expect people to help you you'll need to explain what you've done and in which way it has failed. A simple cry for "doesn't work, tell me exactly how" most often doesn't work.

Cheers,
Andi
Posts: 23
Joined: Wed Oct 31, 2012 9:34 pm