1

I have set up a very simple test program to blink an builtin LED using PWM. Following the mbed example and setting the period to 4 seconds (0.25 Hz) and duty cycle to 0.5, the expected behaviour is for the LED to be on for 2 seconds and off for 2 seconds. However, the actual behaviour is that the LED flashes extremely quickly (almost imperceptable to the eye).

#include "mbed.h"

mbed::PwmOut ledPin = mbed::PwmOut(P0_13); //builtin led pin

void setup() {
  ledPin.period(4.0f);
  ledPin.write(0.0f);
}


void loop() {
  // put your main code here, to run repeatedly:
  ledPin.write(0.5f);
}

I have tried adding delay() and wait_ns() with different times in the main loop, tried having the main loop empty and setting the duty cycle in the setup() function as well as changing the period to larger numbers just in case it was in milliseconds all to no avail. Not quite sure what I'm missing from this extremely simple example.

1
  • According to mbed example, to create an instance of PwmOut object, it should be PwmOut ledPin(P0_13). For the setup(), set the duty cycle to 50% with ledPin.write(0.5f);, and delete the code within the loop() as itself is already a while(1) loop.
    – hcheung
    Commented Jul 16, 2023 at 3:27

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.