element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
microbit
  • Learn
  • Learning Center
  • STEM Academy
  • microbit
  • More
  • Cancel
microbit
micro:bit Blog [PROJECT] Color mixing with a RGB LED
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join microbit to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: andyforeverest
  • Date Created: 1 Dec 2018 7:33 AM Date Created
  • Views 1398 views
  • Likes 2 likes
  • Comments 5 comments
  • micro:bit education giveaway
  • micro:bit project
  • the great micro:bit education giveaway
Related
Recommended

[PROJECT] Color mixing with a RGB LED

andyforeverest
andyforeverest
1 Dec 2018
  1. Color mixing
  2. RGB LED - description and types
  3. Color mixing program

 

  1. Color mixing


One can display a certain color by mixing only three basic colors: Red, Green and Blue. Adding different levels of each color we get many colors.


If all colors are OFF, we get black, and if colors are fully ON, we get white. You can find many color mixing web sites to understand better and to play with different percentages of colors (for example: https://www.colortools.net/color_mixer.html).

 

 

image

2. RGB LED


An RGB LED is made of three LEDs: a red one, a blue one and a green one.
Some RGB LEDs have the anode terminal (or "+" terminal) in common and some have the cathode terminal (or "-" terminal) in common.

 

The one we are going to use in our project is a Common Cathode RGB LED.

 

Connections:

 

Micro:bit pinsRGB LED pins
0Red (via resistor)
1Green (via resistor)
2Blue (via resistor)
GNDCommon Cathode


Like with all LEDs we are going to use a resistor (220 Ohms, in our case) in series in order to limit the current, thus protecting the LED and the micro:bit.

3. Program

 

  • pressing button B will switch between colors. Also the initial of the color will be displayed (“R” or “G” or “B”);
  • pressing button A will increase the quantity of the color selected at the previous step. The quantity will be visually displayed on the LED matrix (using the “plot bar graph” block). The values are between 0 and 100;
  • microbit can generate 1024 steps of color (from 0 to 1023). Our program will only generate 1000 steps of color, by multiplying each value selected by the A button with 10 and writing this value to the pins that are connected to the LED color pins. Of course we can choose different values if we want the colors to increase quicker or slower.

 

image

  • Sign in to reply

Top Comments

  • andyforeverest
    andyforeverest over 6 years ago in reply to james.flynn +1
    I like this! This is a clever way - combining the bits coresponding to the LEDs and ANDing with some masks. Then you can call SetLedColor(MAGENTA_LED) - for example. Right? Another way to write the SetLedColor…
  • james.flynn
    james.flynn over 6 years ago in reply to andyforeverest

    yes, I went with the AND because it was less costly than a shift, but your approach works also.  Now if the hardware would allow for saturation levels, you could use your website to make a lot of different colors image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • andyforeverest
    andyforeverest over 6 years ago in reply to james.flynn

    I like this! This is a clever way - combining the bits coresponding to the LEDs and ANDing with some masks. Then you can call SetLedColor(MAGENTA_LED) - for example. Right?

     

    Another way to write the SetLedColor function:

     

    void SetLedColor(unsigned char ucColor)

    {  

        //Note that when an LED is on, you write a 0 to it:

        led_red = !(ucColor & (0x1 << 0));     //bit 0

        led_green = !(ucColor & (0x1 << 1)); //bit 1

        led_blue = !(ucColor & (0x1 << 2));   //bit 2

    }

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • james.flynn
    james.flynn over 6 years ago in reply to andyforeverest

    // LED Colors

    #define BLACK_LED        0

    #define RED_LED            0x01  //RED

    #define GREEN_LED       0x02  //GREEN

    #define YELLOW_LED     0x03  //RED + GREEN

    #define BLUE_LED           0x04  //BLUE

    #define MAGENTA_LED   0x05  //BLUE + RED

    #define CYAN_LED           0x06  //GREEN+ BLUE

    #define WHITE_LED         0x07  //GREEN+BLUE+RED

     

    // GPIOs for RGB LED

    DigitalOut led_green(LED_GREEN);

    DigitalOut led_red(LED_RED);

    DigitalOut led_blue(LED_BLUE);

     

    void SetLedColor(unsigned char ucColor)

    {   

        //Note that when an LED is on, you write a 0 to it:

        led_red = !(ucColor & 0x1);     //bit 0

        led_green = !(ucColor & 0x2); //bit 1

        led_blue = !(ucColor & 0x4);   //bit 2

    }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • andyforeverest
    andyforeverest over 6 years ago in reply to james.flynn

    How would you use these values in your program?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • james.flynn
    james.flynn over 6 years ago

    Nice site reference for the colors!  I always used:

     

    #define BLACK_LED     0

    #define RED_LED          1  //RED

    #define GREEN_LED     2  //GREEN

    #define YELLOW_LED   3  //RED + GREEN

    #define BLUE_LED         4  //

    #define MAGENTA_LED 5  //BLUE + RED

    #define CYAN_LED         6  //GREEN+ BLUE

    #define WHITE_LED       7  //GREEN+BLUE+RED

     

    But your referenced site allows for a lot of variation (in fairness, I always just used RGB leds that were either just on or off).

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube