Building on our previous project on LED brightness control, we will now take it a step further by creating a dynamic LED light show. Using PWM and timed variations in brightness, we’ll create smooth transitions, fades, and effects to make the LED visually engaging. Let’s bring the LED to life!By the end of this tutorial, you’ll learn:✅ Application of for-loop.✅ Basics of the delay() function to create smooth LED transitions.
Let's Get Started- If you haven't completed the LED brightness tutorial, we highly recommend reviewing it first to build a strong foundation before proceeding. Link: https://www.hackster.io/532624/pwm-control-on-mercury-board-led-brightness-a121f5
- Setting up the environment - if you haven't added the Mercury Board on your Arduino®, this is a good time to do so. Refer to this tutorial for setup: https://www.hackster.io/ral/getting-started-with-mercury-arduino-setup-705f4d
- Plug the USB Cable to Mercury Board and connect it you your laptop.
- Refer to the code below
- To initiate upload process, press and hold FLASH button when "Connecting..." is visible on Arduino Terminal.
Crash Course: For-loop and delay() in the Arduino® Environment
The for loop is a fundamental control structure used in Arduino to execute a block of code multiple times. More information in this can be found here: https://docs.arduino.cc/language-reference/en/structure/control-structure/for/
for (initialization; condition; increment/decrement) {
// Code to execute in each iteration
}
The delay(ms) function pauses the program for a specified time (in milliseconds). More information in this can be found here: https://docs.arduino.cc/language-reference/en/functions/time/delay/
delay(1000); // Pauses execution for 1 second
Coming back to the project...
The LED routine we’ll implement follows this sequence: LED OFF → LED ON → LED OFF → LED ON → Slow Fade Out. This pattern will create a dynamic effect, gradually transitioning into a smooth decay for a visually appealing LED show. ✨
Slow Fade Out
for (int brightness = 250; brightness >= 0; brightness--) {
analogWrite(LED_BUILTIN, 255 - brightness);
delay(2);
}
The loop starts with brightness set to 250 as in the previous state, LED was ON. The loop continues as long as brightness is greater than or equal to 0. In each iteration, brightness decreases by 1 (moving from 250 → 249 → 248... down to 0). A 2-millisecond delay is added to create a smooth fading effect instead of an instant change.
*** DISCLAMER ***Everything mentioned above is generally true; however, on the Mercury Board, which uses the ESP-12F module, the default built-in LED on the ESP-12F module, is wired in an inverted configuration. This means that when GPIO 2 (D7) is set HIGH, the LED turns OFF, and when GPIO 2 is set LOW, the LED turns ON.
As a result, the behavior of PWM is also inverted. When the PWM value is set to 0, the LED glows at full brightness, and when the PWM value is set to 255, the LED turns completely OFF. Otherwise, the conceptual understanding remains the same.
To account for this inverted LED behavior, we need to make a small adjustment in the code as follows:
analogWrite(D7, 255 - brightness);
Here, "brightness" represents the user input for the desired LED brightness.
Quick Video Tutorial
Comments
Please log in or sign up to comment.