Arduino UNO Q - Fade LED

Controlling LED brightness — fading in and out smoothly — is a fundamental technique in Arduino programming. In this tutorial, you will learn how to use PWM (Pulse Width Modulation) to gradually increase and decrease the brightness of an LED on the Arduino UNO Q.

In this tutorial, you will learn:

Arduino UNO Q - Fade LED

Hardware Preparation

1×Arduino UNO Q
1×USB Cable for Arduino Uno Q
1×LED Kit
1×LED (red)
1×LED Module
1×220Ω Resistor
1×Breadboard
1×Jumper Wires
1×Recommended: Screw Terminal Block Shield for Arduino Uno
1×Recommended: Sensors/Servo Expansion Shield for Arduino Uno
1×Recommended: Breadboard Shield for Arduino Uno
1×Recommended: Enclosure for Arduino Uno
1×Recommended: Prototyping Base Plate & Breadboard Kit for Arduino UNO

Or you can buy the following kits:

1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Buy Note: Use the LED Module for easier wiring. It includes an integrated resistor.

Overview of LED

Pinout

LED has two pins:

  • Cathode(−) pin: connect to GND (0V)
  • Anode(+) pin: used to control LED brightness
LED Pinout

How It Works

After connecting the cathode to GND:

  • Connect anode to GND (0V) → LED is OFF
  • Connect anode to VCC → LED is fully ON
  • Send a PWM signal to the anode → brightness varies between 0 (off) and 255 (full)
How LED Fade Works

※ NOTE THAT:

Most LEDs require a current-limiting resistor (e.g., 220Ω) between the anode and the signal pin. Without it, you risk burning the LED.

Fading LEDs with Arduino UNO Q

The Arduino UNO Q STM32 MCU supports PWM on specific pins (such as pin 9). Connect the LED anode through a 220Ω resistor to a PWM-capable pin, and connect the cathode to GND. Use analogWrite(pin, value) to set the brightness (0–255).

Wiring Diagram

The wiring diagram between Arduino UNO Q Fade LED

This image is created using Fritzing. Click to enlarge image

How To Program

  • Set the pin mode to output:
pinMode(9, OUTPUT);
  • Adjust LED brightness using PWM:
analogWrite(9, brightness); // brightness: 0 (off) to 255 (full on)

MCU Code — Fade LED (with delay)

The Arduino UNO Q has two processors: the STM32 MCU (handles real-time hardware control) and the Qualcomm MPU (runs Debian Linux). In this section, only the STM32 MCU is programmed — the Linux side stays idle. A later section will show how both processors work together.

This example gradually increases and decreases the LED brightness using delay():

/* * This Arduino UNO Q code was developed by newbiely.com * * This Arduino UNO Q code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-q/arduino-uno-q-fade-led */ #define LED_PIN 9 // The Arduino UNO Q pin connected to the LED int brightness = 0; // how bright the LED is int fade_step = 5; // how many points to fade the LED by // the setup routine runs once when you press reset: void setup() { // declare pin 9 to be an output: pinMode(LED_PIN, OUTPUT); } // the loop routine runs over and over again forever: void loop() { // set the brightness of pin 9: analogWrite(LED_PIN, brightness); // change the brightness for next time through the loop: brightness = brightness + fade_step; // reverse the direction of the fading at the ends of the fade: if (brightness <= 0 || brightness >= 255) { fade_step = -fade_step; } // wait for 30 milliseconds to see the dimming effect delay(30); }

Detailed Instructions

  • First time with Arduino UNO Q? Follow the Getting Started with Arduino UNO Q tutorial to get your development environment ready before proceeding.
  • Wire the LED: Connect an LED with a 220Ω resistor to pin 9 according to the wiring diagram.
  • Connect: Plug the Arduino UNO Q into your computer with a USB-C cable.
  • Open Arduino App Lab: Launch Arduino App Lab and wait until it detects your Arduino UNO Q.
  • Create a new App: Click the Create New App button.
Create New App in Arduino App Lab on Arduino UNO Q
  • Give the App a name, for example: DIYables_FadeLED
  • Click Create to confirm.
  • You will see a set of folders and files generated inside your new App.
Arduino App Lab App folders and files on Arduino UNO Q
  • Find the sketch/sketch.ino file — this is where you will paste the MCU sketch.
  • Paste the sketch: Copy the MCU code above and paste it into the sketch file. Keep other files as default.
    • Install the library: Click the Add sketch library button (the open book icon with a + sign) in the left sidebar.
    Add sketch library in Arduino App Lab on Arduino UNO Q
    • Search for Arduino_RouterBridge created by Arduino and click the Install button.
    My Apps / DIYables Apps
    Run
    Bricks
    No bricks added...
    Sketch Libraries
    No sketch libra...
    Files
    python
    sketch
    .gitignore
    README.md
    app.yaml
    sketch.ino
    Add sketch library
    Arduino_RouterBridge Arduino

    This library provides a simple RPC bridge for Arduino UNO Q boards, allowing communication between the board and other devices using MsgPack serialization.

    0.4.1
    Install
    More Info
    • Upload: Click the Run button in Arduino App Lab to compile and upload to the STM32.
    Click Run button in Arduino App Lab on Arduino UNO Q
    • Check the LED: The LED should smoothly fade in and out.
    • Pro Tip: Decrease the delay(30) value (e.g., delay(10)) to make the fade faster, or increase it to make it slower.

    Code Explanation

    The explanation is in the comments in the code above.

    ※ NOTE THAT:

    The delay() function blocks all other code while it waits. For more responsive behavior (e.g., reading sensors or responding to commands), use millis() instead — as shown in the next section.

    MCU Code — Fade LED (without delay)

    This version uses millis() for non-blocking fading — the loop keeps running freely, and the LED smoothly fades in over a 3-second period:

    /* * This Arduino UNO Q code was developed by newbiely.com * * This Arduino UNO Q code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-q/arduino-uno-q-fade-led */ #define LED_PIN 9 // The Arduino UNO Q pin connected to the LED #define FADE_PERIOD 3000 // fade time is 3 seconds unsigned long fade_start_ms; // the setup routine runs once when you press reset void setup() { pinMode(LED_PIN, OUTPUT); // declare pin 9 to be an output fade_start_ms = millis(); } // fade-in in loop, and restart after finishing void loop() { unsigned long progress = millis() - fade_start_ms; if (progress <= FADE_PERIOD) { long brightness = map(progress, 0, FADE_PERIOD, 0, 255); analogWrite(LED_PIN, brightness); } else { fade_start_ms = millis(); // restart fade again } }

    Detailed Instructions

    • Replace the code in sketch/sketch.ino with this non-blocking version.
    • Click Run to upload.
    • The LED will fade in continuously over 3-second cycles.
    • Pro Tip: Change FADE_PERIOD 3000 to 1000 for a faster fade or 5000 for a slower one. You can also swap 0, 255 and 255, 0 in map() to fade out instead of in.

    Linux + MCU Bridge Programming

    The Arduino UNO Q has two processors that work together: the MPU (Qualcomm, runs Debian Linux) and the MCU (STM32, runs Zephyr OS with your Arduino sketch). They communicate using RPC via the Arduino_RouterBridge library — never via raw serial ports.

    • The LED is connected to the MCU (STM32) — wired to a PWM-capable digital pin on the STM32. The MCU controls brightness using analogWrite().
    • The MPU cannot control the LED directly — it must send commands to the MCU via Bridge.call(). The MCU executes the registered Bridge.provide_safe() functions.
    • The MPU has Wi-Fi — because the MPU runs full Debian Linux with Wi-Fi, it can receive Telegram commands and remotely set LED brightness or trigger fading.
    • Communication: Bridge.call() on the Linux side invokes Bridge.provide_safe() functions on the MCU side
    • ⚠️ Reserved: /dev/ttyHS1 (Linux) and Serial1 (MCU) are used by the Arduino Router — never open them directly

    In short: MPU sends brightness/fade commands → MCU receives them → MCU updates the LED in real time.

    MCU sketch — LED fade with Bridge control:

    /* * This Arduino UNO Q code was developed by newbiely.com * * This Arduino UNO Q code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-q/arduino-uno-q-fade-led */ #include "Arduino_RouterBridge.h" #define LED_PIN 9 int current_brightness = 0; bool fading = false; int fade_period_ms = 3000; unsigned long fade_start_ms = 0; int fade_direction = 1; // 1 = fade-in, -1 = fade-out void set_brightness(int value) { fading = false; current_brightness = constrain(value, 0, 255); analogWrite(LED_PIN, current_brightness); Monitor.println("Brightness set to: " + String(current_brightness)); } void start_fade(int period_ms) { fade_period_ms = period_ms > 0 ? period_ms : 3000; fade_start_ms = millis(); fading = true; fade_direction = 1; Monitor.println("Fading started with period: " + String(fade_period_ms) + "ms"); } void stop_fade() { fading = false; analogWrite(LED_PIN, 0); current_brightness = 0; Monitor.println("Fade stopped"); } void setup() { pinMode(LED_PIN, OUTPUT); Bridge.begin(); Monitor.begin(); Bridge.provide_safe("set_brightness", set_brightness); Bridge.provide_safe("start_fade", start_fade); Bridge.provide_safe("stop_fade", stop_fade); Monitor.println("Fade LED Bridge ready"); } void loop() { if (!fading) return; unsigned long progress = millis() - fade_start_ms; if (progress >= (unsigned long)fade_period_ms) { // reverse direction and restart fade_direction = -fade_direction; fade_start_ms = millis(); progress = 0; } long brightness; if (fade_direction == 1) { brightness = map(progress, 0, fade_period_ms, 0, 255); } else { brightness = map(progress, 0, fade_period_ms, 255, 0); } analogWrite(LED_PIN, brightness); }

    Python script (Arduino App Lab) — control LED brightness and fading from Linux:

    /* * This Arduino UNO Q code was developed by newbiely.com * * This Arduino UNO Q code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-q/arduino-uno-q-fade-led */ from arduino.app_utils import * import time def loop(): print("Starting auto fade (2 seconds per cycle)") Bridge.call("start_fade", 2000) time.sleep(8) print("Setting brightness to 128 (half)") Bridge.call("set_brightness", 128) time.sleep(2) print("Setting brightness to 255 (full)") Bridge.call("set_brightness", 255) time.sleep(2) print("Setting brightness to 0 (off)") Bridge.call("set_brightness", 0) time.sleep(2) print("Starting slow fade (5 seconds per cycle)") Bridge.call("start_fade", 5000) time.sleep(15) print("Stopping fade") Bridge.call("stop_fade") time.sleep(2) App.run(user_loop=loop)
    • Note: Make sure Bridge.begin() is called in the MCU sketch and the sketch is uploaded before running the Python script on the Linux side.
    • ⚠️ Warning: Never directly open /dev/ttyHS1 (on Linux) or use Serial1 (on MCU) in your code — these are reserved by the Arduino Router and accessing them will break the Bridge.

    Detailed Instructions

    • Upload the MCU sketch: Open Arduino App Lab, create a new App, paste the Bridge MCU sketch above into sketch/sketch.ino, keep the default libraries (no additional library needed), and click Run.
    • Add the Python script: Paste the Python code above into the Python tab of the same App.
    • Run the App: Click Run — the Python side cycles through fade and brightness patterns automatically.
    • Check the console: Open the Console tab → Python Console subtab to see what the Python side is doing.
    • Pro Tip: Call Bridge.call("set_brightness", 0) from Python to turn the LED off immediately.

    App Lab Console Output

    DIYables_Apps
    Stop
    sketch.ino
    1#include "Arduino_RouterBridge.h"
    Serial Monitor
    Python
    Starting auto fade (2 seconds per cycle) Setting brightness to 128 (half) Setting brightness to 255 (full) Setting brightness to 0 (off) Starting slow fade (5 seconds per cycle) Stopping fade

    Telegram Integration

    You can control the LED brightness and fading remotely over Telegram — set specific brightness levels or trigger smooth fading from anywhere.

    If you do not have a Telegram bot yet, see How to Create a Telegram Bot to get your bot token before continuing.

    This section covers:

    • Running a Python script on the Linux side of Arduino UNO Q to listen for Telegram messages
    • Forwarding brightness or fade commands to the MCU via Bridge.call()
    • Sending a confirmation reply back to Telegram

    MCU sketch: Keep the same MCU sketch from the previous Bridge section — no changes needed. Make sure it is already uploaded and running on the STM32 before proceeding.

    Python script (Arduino App Lab) — Telegram bot for LED brightness control:

    /* * This Arduino UNO Q code was developed by newbiely.com * * This Arduino UNO Q code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-q/arduino-uno-q-fade-led */ from arduino.app_utils import * import requests import time BOT_TOKEN = "YOUR_BOT_TOKEN" API_URL = f"https://api.telegram.org/bot{BOT_TOKEN}" last_update_id = 0 def send_message(chat_id, text): requests.post(f"{API_URL}/sendMessage", json={"chat_id": chat_id, "text": text}) def get_updates(): global last_update_id resp = requests.get(f"{API_URL}/getUpdates", params={"offset": last_update_id + 1, "timeout": 5}) return resp.json().get("result", []) def loop(): global last_update_id updates = get_updates() for update in updates: last_update_id = update["update_id"] msg = update.get("message", {}) chat_id = msg.get("chat", {}).get("id") text = msg.get("text", "").strip() if text.startswith("/brightness "): parts = text.split() try: value = int(parts[1]) if 0 <= value <= 255: Bridge.call("set_brightness", value) send_message(chat_id, f"Brightness set to {value}") else: send_message(chat_id, "Brightness must be between 0 and 255") except (ValueError, IndexError): send_message(chat_id, "Usage: /brightness <0-255>") elif text.startswith("/fade "): parts = text.split() try: period_ms = int(parts[1]) Bridge.call("start_fade", period_ms) send_message(chat_id, f"Fading started with {period_ms}ms period") except (ValueError, IndexError): send_message(chat_id, "Usage: /fade <period_ms>\nExample: /fade 3000") elif text == "/fade": Bridge.call("start_fade", 3000) send_message(chat_id, "Fading started with 3000ms period") elif text == "/stop": Bridge.call("stop_fade") send_message(chat_id, "Fade stopped, LED off") else: send_message(chat_id, "Commands:\n/brightness <0-255> — set brightness\n/fade <period_ms> — start fading\n/stop — stop fade\n\nExamples:\n/brightness 128\n/fade 3000") time.sleep(1) App.run(user_loop=loop)
    • Note: Replace YOUR_BOT_TOKEN with the token obtained from @BotFather on Telegram.
    • Send /brightness 128 to set the LED to half brightness.
    • Send /fade 3000 to start smooth fading with a 3-second cycle.
    • Send /stop to stop fading and turn the LED off.

    Detailed Instructions

    • Upload the MCU sketch: Use the Bridge MCU sketch from the previous section (upload it first if not already done).
    • Paste the Telegram script: Copy the Python code above into the Python tab of your App in Arduino App Lab.
    • Set your token: Replace YOUR_BOT_TOKEN in the script with your actual bot token.
    • Run the App: Click Run — the bot starts listening for Telegram messages immediately.
    • Test it: Send /brightness 200 to set the LED to near-full brightness, or /fade 2000 for a fast fade effect.
    • Pro Tip: Send /brightness 0 as an alternative to /stop — it turns the LED off without stopping the fade state.

    App Lab Console Output

    DIYables_Apps
    Stop
    sketch.ino
    1#include "Arduino_RouterBridge.h"
    Serial Monitor
    Python
    [2026-04-29 12:00:01] Telegram: /brightness 128 [2026-04-29 12:00:01] Brightness set to 128 [2026-04-29 12:03:20] Telegram: /fade 3000 [2026-04-29 12:03:20] Fading started with 3000ms period [2026-04-29 12:07:45] Telegram: /stop [2026-04-29 12:07:45] Fade stopped, LED off
    Telegram
    Telegram 12:45
    Welcome to Telegram!
    ArduinoBot 10:19
    Chatting with Arduino...
    telegram-botfather
    BotFather Yesterday
    Your bot has been created.

    ArduinoBot

    bot
    Today
    /brightness 128
    10:15 AM ✓✓
    Brightness set to 128
    10:16 AM
    /fade 3000
    10:17 AM ✓✓
    Fading started with 3000ms period
    10:18 AM
    /stop
    10:19 AM ✓✓
    Fade stopped, LED off
    10:20 AM

    OpenClaw Integration

    OpenClaw integration for Arduino UNO Q LED fade control is coming soon.

    • Coming Soon: OpenClaw support for remotely controlling LED brightness on Arduino UNO Q will be covered in a future update.

    Application/Project Ideas

    Here are some projects you can build with LED fading and Arduino UNO Q:

    • Ambient mood light: Use Telegram to set brightness levels for a desk lamp or nightlight from bed
    • Sunrise alarm: Program a slow fade-in at a scheduled time using the MPU's Linux clock
    • Battery indicator: Map battery voltage to LED brightness — dimmer means lower charge
    • Heartbeat effect: Create a pulse effect by fading in and out quickly — great for wearables or status LEDs
    • Night-light controller: Set full brightness in the evening, half at night, and off at dawn via scheduled Telegram commands

    Challenge Yourself

    Try these challenges with LED fading on Arduino UNO Q:

    • Easy: Change FADE_PERIOD in Fade2.cpp to make the LED fade in over 1 second instead of 3
    • Medium: Extend the Bridge sketch to expose a get_brightness() function that returns the current brightness value to Python
    • Advanced: Build a Telegram bot that accepts a named brightness level (/dim, /half, /full) and maps them to 64, 128, and 255 respectively, then applies them via Bridge.call()

    Function References

    ※ OUR MESSAGES

    • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
    • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!