How to Create a Fading LED with PWM

In this tutorial, you will learn how to use Pulse Width Modulation (PWM) to gradually fade an LED in and out using OmniBoard Studio.


Steps to Create a Fading LED Project

1

Open OmniBoard Studio and create a new project.

2

Build the block diagram to fade an LED in and out using a PWM block and a variable for brightness.

Try to build the diagram yourself, or click here to see one solution.
  • Place a Start block, then connect it to a While True block.
  • Inside the loop, use a For Loop block counting from 0 to 1023 (fade in). Connect a Set PWM Duty block inside the For Loop and set the duty cycle to the loop variable.
  • Add a short Timer block (e.g. 2 ms) inside the For Loop so the fade is visible.
  • After the fade-in loop, add a second For Loop counting from 1023 down to 0 (fade out) with the same Set PWM Duty and Timer blocks.
PWM LED Flowchart
3

Build the circuit — it is the same as the Blinking LED circuit.

  • Make sure the GPIO pin you choose supports PWM output (check your microcontroller's datasheet).
  • Connect the LED anode (long leg) through a resistor to the PWM-capable GPIO pin, and the cathode (short leg) to ground.
PWM LED Circuit
4

Compile the code and upload it to your microcontroller.

5

Watch your LED smoothly fade in and out in a continuous breathing pattern!

Bonus Information