You are viewing an older revision! See the latest version

PwmOut

/media/uploads/mbedofficial/pwmout_interfaces.png

The PwmOut interface is used to control the frequency and mark-space ratio of a digital pulse train.

Hello World!

Example code for dimming an on-board LED

#include "mbed.h"

PwmOut led(LED1);

int main() {
    while(1) {
        for(float p = 0.0f; p < 1.0f; p += 0.1f) {
            led = p;
            wait(0.1);
        }
    }
}

API

Details

The PwmOut Interface can be used on mbed pins p21-p26, and also on-board LED1-LED4

The PwmOut interface can express the pulse train in many ways depening on how it is to be used. The period and pulse width can be expressed directly in units of seconds, millisecond or microseconds. The pulsewidth can also be expressed as a percentage of the the period.

Implementation details

On the mbed LPC2368 and LPC1768, the PwmOut hardware is limited to share the period value between all outputs. Therefore, if you change the period of one output, you change them all. The pulsewidth can be set independently for each output.

Examples

Control a R/C model servo

#include "mbed.h"

PwmOut servo(p21);

int main() {
    servo.period(0.020);          // servo requires a 20ms period
    while (1) {
        for(float offset=0.0; offset<0.001; offset+=0.0001) {
            servo.pulsewidth(0.001 + offset); // servo position determined by a pulsewidth between 1-2ms
            wait(0.25);
        }
    }
}

All wikipages