![](/media/cache/group/Mbed-100-mk2_WXmQNC1.png.50x50_q85.png)
PwmOut Hello World
Dependencies: mbed
Fork of PwmOut_HelloWorld by
You are viewing an older revision! See the latest version
Homepage
Note
The PwmOut API uses 8bit I2C addresses. That means you take your 7 bit address and left shift it by 1, then pass it to the API. The API will automatically add the read / write bits.
This code example sets the period in seconds and the duty cycle as a percentage of the period in floating point (0 to 1 range). The effect of this code snippet will be to blink LED2 over a 4 second cycle, 50% on, for a pattern of 2 seconds on, 2 seconds off.
PwmOut_Example1
#include "mbed.h" PwmOut led(LED2); int main() { // specify period first, then everything else led.period(4.0f); // 4 second period led.write(0.50f); // 50% duty cycle while(1); // led flashing }
The following example does the same thing. Instead of specifying the duty cycle as a relative percentage of the period it specifies it as an absolute value in seconds. In this case we have a 4 second period and a 2 second duty cycle, meaning the led will be on for 2 seconds and off for 2 seconds.
PwmOut_Example2
#include "mbed.h" PwmOut led(LED2); int main() { // specify period first, then everything else led.period(4.0f); // 4 second period led.pulsewidth(2); // 2 second pulse (on) while(1); // led flashing }