Reso-nance numérique | Arts et cultures libres

Outils du site

Panneau latéral


Table des matières


  • A : there is one hardware PWM output on the RPi, connected to P1-12 (GPIO18).
  • B+ : This solution works only for one PWM output (GPIO18). I thinks it is because WiringPi is designed for previous version of raspberry pi (Model B+ has new PWMs on GPIO12, GPIO13 and GPIO19).
  • Maximum 16mA per IO pin (sink or source) with the total current from all pins not exceeding 51mA (true for Model B so presumably true for Model B+ but not confirmed as full schematic not yet available). See a detailed explanation here.
  • The maximum permitted current draw from the 3.3 V pins is 50 mA. Maximum permitted current draw from the 5 V pin is the USB input current (usually 1 A) minus any current draw from the rest of the board.[15]
    • Model A: 1000 mA - 500 mA → max current draw: 500 mA
    • Model B: 1000 mA - 700 mA → max current draw: 300 mA
  • The PWM pin available on the GPIO header is shared with the Audio system (true for Model B so presumably true for Model B+ – not confirmed as full schematic not yet available). This means that you can’t use the PWM output and play audio through the 3.5mm jack at the same time.

<blockquote> Basically there are two ways to create PWM:

  • Hardware PWM
    • very fast (max. possible frequency / period)
    • software independent, independent from program flow
    • clean signal
    • BUT: not all pins might support HW PWM
  • Software PWM
    • each I/O pin can be used for SW PWM
    • BUT: software controlled timing has to be implemented, implementation and timing have to respect CPU usage, not a real clean signal

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). (</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 (</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>

PWM logiciel

<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:
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.setup(12, GPIO.OUT)
p = GPIO.PWM(12, 0.5)
input('Press return to stop:')   # use raw_input for Python 2
An example to brighten/dim an LED:
import time
import RPi.GPIO as GPIO
GPIO.setup(12, GPIO.OUT)
p = GPIO.PWM(12, 50)  # channel=12 frequency=50Hz
    while 1:
        for dc in range(0, 101, 5):
        for dc in range(100, -1, -5):
except KeyboardInterrupt:

PWM matériel

Pulse Width Generator

Contrôle de puissance

<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>

/home/resonancg/www/wiki/data/pages/materiel/rpi/pwm/accueil.txt · Dernière modification: 2018/01/18 13:02 de resonance