<blockquote> Basically there are two ways to create PWM:
In other words: HW PWM is clearly prefered!
But unfortunately there is only one HW PWM pin (#18) available on the Rasperry Pis GPIO header (also its shared with the Audio system. That means that you can’t use PWM or Servo output and play audio through the 3.5mm jack at the same time). (http://www.jensd.de/wordpress/?p=1064)</blockquote>
<blockquote>Raspberry Pi is a pretty cool platform. You get a board not much bigger than an Arduino, it runs Linux, comes with an Ethernet board and for an extra $10 you get wifi: a perfect base for a connected object - under $50.
There is one downside however: it does not come with as many PWM pins. Only one is offered and that is not enough when you want to drive external peripherals like servos, rgb leds, or anything else that requires more than a on/off signal.
One solution that I have seen people use is to plug an Arduino to the Raspberry Pi (through USB) but it increases seriously the cost of the package and seems a little overkill. I think it only makes sense if you are just building a quick prototype.
I did not want to go down that road because I want to keep the complexity and the costs down.Another solution is to use an I2C or SPI device that will be controlled from the Raspberry Pi and will generate a PWM signal to control the LED driver. I think this would be the best option but I had none of those available and being currently in Dakar (Sénégal), I cant really walk to the nearest shop to get one or wait for Mouser to deliver.My next option was to look online for a way to do software PWM on the Raspberry PI but most of what I found came with huge warnings: software PWM in a multi-task operating system gives very bad results and the pulse are far from regular (http://www.tbideas.com/blog/2013/02/controling-a-high-power-rgb-led-with-a-raspberry-pi/)</blockquote>
<blockquote>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. 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.</blockquote>
<blockquote>RPIO.PWM provides PWM via DMA for the Raspberry Pi, using the onboard PWM module for semi-hardware pulse width modulation with a precision of up to 1µs. With RPIO.PWM you can use any of the 15 DMA channels and any number of GPIOs per channel. Since the PWM is done via DMA, RPIO.PWM uses almost zero CPU resources and can generate stable pulses with a very high resolution. RPIO.PWM is implemented in C (source); you can use it in Python via the provided wrapper, as well as directly from your C source.</blockquote>
Using PWM in RPi.GPIO To create a PWM instance: p = GPIO.PWM(channel, frequency) To start PWM: p.start(dc) # where dc is the duty cycle (0.0 <= dc <= 100.0) To change the frequency: p.ChangeFrequency(freq) # where freq is the new frequency in Hz To change the duty cycle: p.ChangeDutyCycle(dc) # where 0.0 <= dc <= 100.0 To stop PWM: p.stop() Note that PWM will also stop if the instance variable 'p' goes out of scope. An example to blink an LED once every two seconds: import RPi.GPIO as GPIO GPIO.setmode(GPIO.BOARD) GPIO.setup(12, GPIO.OUT) p = GPIO.PWM(12, 0.5) p.start(1) input('Press return to stop:') # use raw_input for Python 2 p.stop() GPIO.cleanup() An example to brighten/dim an LED: import time import RPi.GPIO as GPIO GPIO.setmode(GPIO.BOARD) GPIO.setup(12, GPIO.OUT) p = GPIO.PWM(12, 50) # channel=12 frequency=50Hz p.start(0) try: while 1: for dc in range(0, 101, 5): p.ChangeDutyCycle(dc) time.sleep(0.1) for dc in range(100, -1, -5): p.ChangeDutyCycle(dc) time.sleep(0.1) except KeyboardInterrupt: pass p.stop() GPIO.cleanup()
<blockquote>Any 5V LOGIC input can be driven from 3V3. The logic HIGH is defined as 2.7V which voltage can be reached with 5V or 3V3. It is the main reason why 3V3 is so commonly used: It is the lowest supply voltage you can have and still interface with 'old' 5V logic. In telephones and other mobile equipment you see more and more 1V8 as new I/O voltage.</blockquote>