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.
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.
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
- Here is an example of simplified MicroPython code for a fading LED:
import machine # Needed for controlling the GPIO pins
import time # Needed for creating delays
pwm = machine.PWM(machine.Pin(2)) # Set GPIO pin 2 as a PWM output
pwm.freq(1000) # Set PWM frequency to 1000 Hz
while True:
for duty in range(0, 1024): # Fade in: 0 → full brightness
pwm.duty(duty)
time.sleep_ms(2)
for duty in range(1023, -1, -1): # Fade out: full brightness → 0
pwm.duty(duty)
time.sleep_ms(2)
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(2, GPIO.OUT)
pwm = GPIO.PWM(2, 1000) # Pin 2, 1000 Hz
pwm.start(0) # Start with 0% duty cycle
while True:
for dc in range(0, 101): # Fade in
pwm.ChangeDutyCycle(dc)
time.sleep(0.02)
for dc in range(100, -1, -1): # Fade out
pwm.ChangeDutyCycle(dc)
time.sleep(0.02)