element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • 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
  • 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] RGB lamp
  • 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 9:46 AM Date Created
  • Views 784 views
  • Likes 6 likes
  • Comments 1 comment
  • micro:bit education giveaway
  • micro:bit project
  • the great micro:bit education giveaway
Related
Recommended

[PROJECT] RGB lamp

andyforeverest
andyforeverest
1 Dec 2018

 

  1. Hardware setup

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.

2. Program
At startup we initialize red, green and blue variables with values chosen randomly between 0 and 1023. This way we have always a different starting color.

 

Then we can choose a changing direction for each color: some could go up and others could go down. This way the pattern. We set these values by setting directionRed, directionBlue and directionGreen to either “1” or “-1”.

 

In the forever loop, we will change the level of each color according to the direction set in the first step: if the direction is -1, then the level of color is going to decrease by 1 step, and if the direction is 1, then the color is going to increase by the same quantity.

 

When the level reaches a limit, either the low limit or the high limit, the direction is going to change (from -1 to 1 or from 1 to -1). In doing so, we only use addition to change the level, but the color level can go both ways.

 

The last step is to send the values to respective pins.

image
Ways to improve:

 

  1. If we want a random direction every time the system starts, one way is to set the direction depending on the level of color chosen first. Thus, if the value of red is an even number, then the direction is positive (1), else, if the value of red is odd, then the direction is negative (-1). This will give more randomness.

image

 

2. Visual representation of color level

 

To have a visual representation of the color level, we could display the level on LED matrix. The red color level will be display on column 0, green on column 2 and blue on column 4. We can use functions for this task.

 

image

 

Then we call the functions from the forever loop.

 

image

 

  • Sign in to reply
  • DAB
    DAB over 6 years ago

    Nice post.

     

    The visual programming approach makes it easy for programmers to understand control flow and execution.

     

    DAB

    • 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