dn153
Posts: 2
Joined: Wed Feb 13, 2013 9:21 pm

pwm control from python

Wed Feb 13, 2013 9:29 pm

Hi all

Any help would be greatly appreciated, I have been playing around with my pi and would like to start to use the gpio #18 to control pwm output to a servo. I have looked around on google but can't find any real info on how to achieve control through python. I am running raspian wheezy os and can wire the hardware fine, it's the software I'm having a problem with. Many thanks!

User avatar
bgreat
Posts: 235
Joined: Mon Jan 23, 2012 2:09 pm

Re: pwm control from python

Thu Feb 14, 2013 12:53 am

WiringPi has everything you need for PWM control via Python. I use it with my Nokia SPI display to control the back light brightness. The thread on the forums has the code.

Here is a quick excerpt for using the PWM:

Code: Select all

#!/usr/bin/env python

import wiringpi

#gpio's :
LED  = 1 # gpio pin 12 = wiringpi no. 1 (BCM 18)

wiringpi.wiringPiSetup()

wiringpi.pinMode(LED, 2)
wiringpi.pwmWrite(LED, 256)
Enjoy!
Bill

dn153
Posts: 2
Joined: Wed Feb 13, 2013 9:21 pm

Re: pwm control from python

Thu Feb 14, 2013 6:54 am

That's great thanks I'll get on with it, such a fast reply!

simplesi
Posts: 2327
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
Contact: Website

Re: pwm control from python

Thu Feb 14, 2013 7:50 am

Just a couple of points
1 - I've never personally got wiringpi to work in python - but that's just me :)

2. PWM and Servo are different and you'd have to be very lucky that the PWM output would control a servo.

AFAIK PWM just needs to switch a pin high for x secs, then off for y secs. It doesn't matter about the absolute values of x and y - its just the ratio that counts when using it to control LED brightness or a motor speed.

But a servo requires pulses of certain durations and repetition rate and therefore any PWM output from any device is quite unlikely to be able to control one.

Although not impossible, (as things rarely are with computers :) ) its quite a bit of trouble to get servos to work on the RPi - search for servoblaster :)

regards
Simon
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter

User avatar
bgreat
Posts: 235
Joined: Mon Jan 23, 2012 2:09 pm

Re: pwm control from python

Thu Feb 14, 2013 1:26 pm

simplesi wrote:Just a couple of points
1 - I've never personally got wiringpi to work in python - but that's just me :)
I have found WiringPi to work well for general GPIO and PWM from Python. My only problem was getting my head around the different pin numbering schemes (which is why my code has the detailed comments on the pin assignment lines). If you have specific problems, I would like to be aware so I can possibly avoid future issues.
simplesi wrote:2. PWM and Servo are different and you'd have to be very lucky that the PWM output would control a servo.
I come from an industry where variable speed DC motors with encoders in device control are often referred to as "servo" motors to distinguish them from AC motors and stepper motors. ;) So, I just answered the question about PWM and did not consider the whether it was for DC motor control or positional servo motor control.

If there is an interest in controlling positional servo motors, I would recommend looking at the ServoBlaster project which allows control of up to 8 positional servos through a simple device interface.

Enjoy!
Bill

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

Re: pwm control from python

Thu Feb 14, 2013 1:37 pm

For C

pigpio provides servo pulses on all the user gpios.

http://abyz.co.uk/rpi/pigpio/pigpio.tar

It also provides general pwm on all the user gpios.

EDITED to change link to latest version of pigpio.
Last edited by joan on Sun May 26, 2013 10:21 pm, edited 1 time in total.

simplesi
Posts: 2327
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
Contact: Website

Re: pwm control from python

Thu Feb 14, 2013 1:45 pm

If you have specific problems, I would like to be aware so I can possibly avoid future issues.
Just tried to install it a couple of times and failed :(
(last attempt was about 3 months ago so maybe its less problematical to get going now :) )
So, I just answered the question about PWM and did not consider the whether it was for DC motor control or positional servo motor control.
Whereas I did the opposite and assumed positional servo :)

Simon
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter

Return to “Python”