RGB LEDs are everywhere—from decorative lighting and smart wearables to artistic installations and tech gadgets. Mastering how to control them opens up a world of creative possibilities! Whether you're building a smart home setup, adding flair to a wearable, or experimenting with visual effects, RGB LEDs bring your ideas to life with vibrant color.
In this fun and beginner-friendly tutorial, we’ll use the Adafruit NeoPixel library with the Mercury Board to create a smooth color pulsing effect. Watch as the onboard RGB LED transitions seamlessly between Red, Green, and Blue, giving you a hands-on intro to dynamic LED control and creative coding.
✨ Get ready to light up your projects—one color at a time! 🔴🟢🔵
By the end of this tutorial, you’ll learn:✅ How to install External Libraries in Arduino® Environment.✅ Learn about functions.✅ How to control WS2812 - onboard RGBLED on Mercury using Adafruit NeoPixel Library.
Quick Video TutorialLet's Get Started- 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: External Libraries in Arduino® Environment
⚙️ External libraries in Arduino are pre-written sets of code that add extra functionality to your projects. Instead of writing everything from scratch, libraries allow you to reuse optimized and tested code for specific tasks like controlling sensors, displays, motors, or LEDs.
They make your life easier by providing simple functions and examples so you can focus on building and experimenting rather than complex coding.
🌈 Introduction to Adafruit NeoPixel Library
The Adafruit NeoPixel library is a popular external library designed to control RGB LEDs (also known as NeoPixels). It provides easy-to-use functions for setting colors, brightness, and animation effects across individual LEDs or strips.
In this tutorial, we use this library to control the onboard RGB LED on the Mercury Board, creating smooth and vibrant color transitions with just a few lines of code!
🛠️ Installing the Adafruit NeoPixel Library
To get started, we first need to install the Adafruit NeoPixel library in the Arduino IDE. Follow these steps:
Open the Arduino IDE.
- Open the Arduino IDE.
- Navigate to Sketch > Include Library > Manage Libraries…
- In the Library Manager window, type "Adafruit NeoPixel" in the search bar. and find "Adafruit NeoPixel by Adafruit" in the results.
- Select the latest version from the dropdown and click Install.
That’s it! The library is now ready to use. You can now include it in your sketches and start programming colorful RGB effects.
💡 This same method can be used to install any external library from the Arduino Library Manager.Coming back to the project...
On the Mercury Board, the onboard RGB LED is connected to pin D0 (GPIO 0).
A deeper dive into the code...
#include <Adafruit_NeoPixel.h>
This line imports the Adafruit NeoPixel library, which contains all the functions we need to control NeoPixel RGB LEDs easily.
#define LED_PIN RGB
LED_PIN is defined as RGB_LED which is a predefined constant in the hardware setup of the Mercury Board, referring to the onboard RGB LED pin.
#define NUM_PIXELS 1
This means you're controlling a single NeoPixel LED. If you had a strip of, say, 32 LEDs, this would be 32.
Adafruit_NeoPixel pixels(NUM_PIXELS, LED_PIN, NEO_GRB + NEO_KHZ800);
Here we are creating a "NeoPixel Object". This object, named pixels,
represents our LED setup.
* `NUM_PIXELS`: The number of LEDs in your strip or ring (1 in this case).
* `LED_PIN`: The Arduino pin connected to the data line of the NeoPixel LEDs.
* `NEO_GRB + NEO_KHZ800`: This specifies the color order (Green, Red, Blue) and clock speed
void setup() {
pixels.begin();
}
Initializes the NeoPixel library and gets the LED ready to receive color data.
pixels.setPixelColor(0, pixels.Color(255, 0, 0));
pixels.show();
delay(1000);
This function sets the color of a specific LED. In this case, "LED -> 0", is set to color RED.
pixels.Color(255, 0, 0)
Sets the color in order (RED, GREEN< BLUE). Even though WS2812 LEDs (like NeoPixels) physically expect color data in GRB order (Green, Red, Blue), the Adafruit NeoPixel library handles this internally.
Lastly, we set a delay of 1 sec, and the show goes on.
_____
Stay tuned for more tutorial coming up in future!!!
Comments
Please log in or sign up to comment.