Take the 2-minute tour ×
Raspberry Pi Stack Exchange is a question and answer site for users and developers of hardware and software for Raspberry Pi. It's 100% free, no registration required.

Can I use the GPIO as a Pulse Width Modulation output? If so, how would I go about doing it and how many concurrent, distinct PWM outputs can I have?

share|improve this question

3 Answers 3

up vote 19 down vote accepted

As suggested by Alex Chamberlain, the WiringPi library appears to support both hardware PWM output on one GPIO pin and software PWM on any of the other GPIO pins. Meanwhile the RPIO.PWM library does PWM by DMA on any GPIO pin. Effectively this is a halfway house between hardware and software PWM, providing a 1us timing resolution compared to 100us with WiringPi's Software PWM.Thanks dm76

Which of these is suitable for your applications depends on how many PWM outputs you need and what performance you want out of those outputs.

If you application is tolerant of low timing resolution and high jitter then you could use a software or DMA assisted timing loop. If you want higher precision / lower jitter PWM then you may need hardware assistance.

When might Software PWM be suitable?

If you want to flash a bunch of LEDs with different human visible cadences (10's of hertz) with soft real-time response requirements then the software loop could handle as many PWM's as you have GPIO pins.

When might Hardware PWM be suitable?

If you want to control a servo motor with hard real-time response requirements then you will need to use the Hardware PWM. Even then you may have problems ensuring a real-time response for the servo loop which ties encoder input to PWM output.

A stable servo loop need to read encoders at a regular rate (low jitter), write out revised PWM output values at a regular rate and the latency between these should be fixed (low jitter overall) or you will have to undertune your motor to prevent it becoming unstable under load. This is hard to do with a multi-tasking operating system without low level support.

What if I need multiple hardware PWM outputs?

If you need to run multiple servo loops, then you are probably going to need to offload them to another device to ensure hard real-time performance, relegating your Raspberry Pi to being a soft real-time supervisor.

One option, would be something like the Adafruit 16-Channel 12-bit PWM/Servo Driver - I2C interface - PCA9685 which would allow you to control 16 pwm outputs with just a few pins of GPIO for the I2C bus. For an example of it's use, check out the I2C 16 Channel PWM/Servo Breakout - Working post on the Raspberry Pi forums.

share|improve this answer
    
Can I use the software PWM approach to run DC motors? –  gideon Jun 23 '13 at 18:12
    
@gideon - Yes, the motor power amplifiers that I have used have all taken PWM as their input. –  Mark Booth Jun 24 '13 at 8:39
    
FYI, the RPI library (pythonhosted.org/RPIO/pwm_py.html) seem to have a much better resolution (1us) compared to WiringPi with 100us resolution –  dm76 Jan 9 at 18:18
    
@MarkBooth - No probs. The library is really well written and can be used as drop-in replacement for RPi.GPIO which is very handy if you started a project with the latter and later realised PWM signals were needed... –  dm76 Jan 21 at 10:20

Hardware PWM

Yes, there is one hardware PWM output on the RPi, connected to P1-12 (GPIO18). Further, PWM outputs could be added using an I2C or SPI interface; some people have had success with this (forum post).

Example Code

You can use WiringPi to control the PWM pin; you could look at the code to avoid including the entire library.

Software PWM

The RPi is not suitable for any serious software PWM as Linux is not a real-time operating system.

share|improve this answer
    
Question, what is the definition or an example of serious software PWM? And what are "real time operating systems" and is there ever any chance of getting one on a Pi –  AnthonyBlake Aug 17 '12 at 23:37
    
@AnthonyBlake Well, you can probably control the brightness of a light using software PWM, but I suspect a motor will stall. There's no need to do software PWM though, hardware is simpler and more effective. Real time operating systems will be better explained by Google; they guarentee certain things about how long and often software is run. –  Alex Chamberlain Aug 18 '12 at 7:13
    
@AnthonyBlake A "Real-Time OS" (RTOS) is an operating system that gives you a guarantee on the upper time limit of execution. Like saying to the program "Yes, you will have some execution time in 33ms (give or take 2ms tolerance) to flip that GPIO pin bit to give your step motor a signal in the exact time window when he needs it. And you can rely on that!" There's a RT Linux out there. Don't know if it's been ported to the RPi (yet). –  orithena Dec 26 '12 at 20:48
    
Sorry Alex, I didn't intentionally steal another part of your answer, but I've just noticed that we came to the same forum post via different routes. –  Mark Booth Jun 25 '13 at 21:50

Not quite a real-time os but RISCOS for RPI is cooperative multitasking, so you can easily run an app that has 100% CPU so you can manage your timings much better. Just dont expect to do anything else but your own code.

share|improve this answer
    
I read somewhere that there is a hardware limit as to the switching frequency of an output pin, too. I think it was around 20 MHz. So don't expect to be able to pull of 300 MHz PWM or anything of that sort, even with 100% CPU usage. –  Wallacoloo Jun 18 at 7:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.