VEEROBOT Docs
StoreBlogWiki
  • 🌐Introduction
  • ROS Based Robots
    • 🐺Wolf Robot
      • ROS Introduction
      • Getting Started with ROS 2
      • Introduction to Wolf Robot
      • ROS 2 Setup
      • ROS Concepts
      • Using Wolf Examples
      • Creating a URDF File
      • Visualising in Gazebo
      • Controlling motors from ROS
      • LiDAR Basics & Control
      • Using a Camera with ROS2
      • ROS 2 Cleanup
      • ROS 2 Controller
      • Teleoperation
      • Slam Navigation
      • Wolf : Conclusion
  • Arduino Based Robots
    • 🚜Micro:Xbot
      • Preparations before First Use
      • Programming the Board
      • Powering the Board
      • Pin Mapping
      • Basic Programs
        • Blinking LED
        • Buzzer Program
        • Fading LED
        • RGB LED
        • Light Detection
        • Battery Voltage
        • Line Follower Module
        • Ultrasonic Sensor
        • Infrared Remote Control
        • Motor Control
        • Bluetooth Control
        • Display Test
        • Test Me
      • Robot Assembly
      • Advanced Programs
        • Bluetooth Controlled Robot
        • Obstacle Avoidance Robot
        • Line Follower Robot
  • ROS2 Repo
    • Introduction
    • Page 1
      • Sub Page 1
    • Page 2
Powered by GitBook
On this page
  1. Arduino Based Robots
  2. Micro:Xbot
  3. Basic Programs

RGB LED

Controlling a Programmable RGB LED

PreviousFading LEDNextLight Detection

Last updated 2 years ago

There are two 5mm RGB LED's soldered on the board. These LED's are interesting in that they are controlled using only a single pin. The LED's are called WS2812B Programmable LED's.

WS2812B is an intelligent control LED where the control circuit and RGB chip are integrated in a single 5mm through hole package. To understand these LEDs, let's walk through how this addressable LED protocol works.

Each separate red, green, and blue LED in a single WS2812B unit is set up to shine at 256 brightness levels, indicated by an 8-bit binary sequence set from 0 to 255. When combined, each LED unit requires three sets of eight brightness bits, or 24 bits, of information for full control. Let us see how it works:

  1. Our micro-controller transmits a sequence of eight green bits, eight red bits, and eight blue bits to the first LED in the series.

  2. When multiple LED's are present, the data sequence that controls the second LED starts directly after the first with red, green and blue data. The sequence continues in that pattern until it illuminates every LED present.

  3. The first LED takes in information for the entire chain of LED's, then passes the same data along without the sequence it applied to itself, transforming the second LED into the first component on the list.

  4. This "new number one" LED unit continues passing information along until there are no more binary LED sequences left.

To demonstrate the above procedure, we need to download and install a library to control these intelligent LED's. The library we will download is Adafruit_NeoPixel.h from

Libraries are often distributed as a ZIP file or folder. The name of the folder is the name of the library. Inside the folder will be a .cpp file, a .h file and often a keywords.txt file, examples folder, and other files required by the library.

There are two ways to install the library:

  1. In the Arduino IDE, navigate to Sketch > Include Library > Manage Libraries. Then the Library Manager will open and you will find a list of libraries that are already installed or ready for installation. Search for Neopixel strip using the search bar. Click on the text area and then select the specific version and install it.

  2. Navigate to the Releases page. Download the latest release. Extract the zip file In the Arduino IDE, navigate to Sketch > Include Library > Add .ZIP Library

Copy the below code, compile and upload:

/*
* RGB LED: The program controls two programmable LED's from a single pin
*
* At VEEROBOT, we invest time and resources providing this open source code,
* Please support VEEROBOT and open-source hardware by purchasing products
* from us @ http://veerobot.com
* -----------------------------------------------------------------------------
* You are free to redistribute it and/or modify it under the terms of the GNU
* Lesser General Public License as  published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* This Code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU Lesser General Public License for more details.
*
* See <http://www.gnu.org/licenses/>
*/

#include <Adafruit_NeoPixel.h>
#include "config.h"

#define PIXEL_COUNT 2  // Number of RGB LED's

Adafruit_NeoPixel strip(PIXEL_COUNT, RGB, NEO_RGB + NEO_KHZ800);

void setup() {
  strip.begin();  // Initialize RGB LED strip object (REQUIRED)
  strip.show();   // Initialize all LED's to 'off'
}

void loop() {
  colorWipe(strip.Color(255, 0, 0), 50);  // Red
  delay(1000);
  colorWipe(strip.Color(0, 255, 0), 50);  // Green
  delay(1000);
  colorWipe(strip.Color(0, 0, 255), 50);  // Blue
  delay(1000);
  rainbow(5);
  colorWipe(strip.Color(255, 255, 255), 50);  // White
  delay(1000);  
  theaterChaseRainbow(50);
  colorWipe(strip.Color(0, 0, 0), 50);  // Off
  delay(3000);
}

void colorWipe(uint32_t color, int wait) {
  for (int i = 0; i < strip.numPixels(); i++) {  // For each pixel in strip...
    strip.setPixelColor(i, color);               //  Set pixel's color (in RAM)
    strip.show();                                //  Update strip to match
    delay(wait);                                 //  Pause for a moment
  }
}

void theaterChase(uint32_t color, int wait) {
  for (int a = 0; a < 10; a++) {   // Repeat 10 times...
    for (int b = 0; b < 3; b++) {  //  'b' counts from 0 to 2...
      strip.clear();               //   Set all pixels in RAM to 0 (off)
      // 'c' counts up from 'b' to end of strip in steps of 3...
      for (int c = b; c < strip.numPixels(); c += 3) {
        strip.setPixelColor(c, color);  // Set pixel 'c' to value 'color'
      }
      strip.show();  // Update strip with new contents
      delay(wait);   // Pause for a moment
    }
  }
}

void rainbow(int wait) {
  for (long firstPixelHue = 0; firstPixelHue < 3 * 65536; firstPixelHue += 256) {
    for (int i = 0; i < strip.numPixels(); i++) {  // For each pixel in strip...
      int pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
      strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
    }
    strip.show();  // Update strip with new contents
    delay(wait);   // Pause for a moment
  }
}

// Rainbow-enhanced theater marquee. Pass delay time (in ms) between frames.
void theaterChaseRainbow(int wait) {
  int firstPixelHue = 0;           // First pixel starts at red (hue 0)
  for (int a = 0; a < 30; a++) {   // Repeat 30 times...
    for (int b = 0; b < 3; b++) {  //  'b' counts from 0 to 2...
      strip.clear();               //   Set all pixels in RAM to 0 (off)
      // 'c' counts up from 'b' to end of strip in increments of 3...
      for (int c = b; c < strip.numPixels(); c += 3) {
        int hue = firstPixelHue + c * 65536L / strip.numPixels();
        uint32_t color = strip.gamma32(strip.ColorHSV(hue));  // hue -> RGB
        strip.setPixelColor(c, color);                        // Set pixel 'c' to value 'color'
      }
      strip.show();                 // Update strip with new contents
      delay(wait);                  // Pause for a moment
      firstPixelHue += 65536 / 90;  // One cycle of color wheel over 90 frames
    }
  }
}

🚜
https://github.com/adafruit/Adafruit_NeoPixel