Micro:bit Experiment 05: RGB LED —— Elecfreaks Mirco: bit Starter Kit Course

in microbit •  7 years ago 

Introduction

RGB LED is a kind of LED. It can emit light with three different colors: red, green, blue. In this experiment, we are trying to make RGB LED gradually shift its light among the three different colors.

Component List

Hardware:

1 x Micro:bit Board

1 x Micro-B USB Cable

1 x Microbit Breadboard Adapter

1 x Transparent Breadboard - 83 * 55 mm

1 x RGB LED

3 x 100 Ohm Resistors

1 x Breadborad jumper wire 65pcs pack

Tips: If you want to buy all components above, you may need Elecfreaks Micro:bit Starter Kit.

Software:

Microsoft Makecode Online Editor

Major Component Introduction

RGB LED

RGB LED is a kind of LED, which has integrated red LED, green LED, and blue LED into a component. We all knows that the three primary colors of light are red, green, and blue. With different groups of the three color lights, we can create all colors of the world. Similarly, if we use RGB LED to group lights with different brightness, then it can form various colors.

RGB LED can be divided into 2 types: common anode and common cathode. In common-cathode RGB LED, its common port usually connects GND, while in common-anode RGB LED, its common port connects VCC. In this experiment, we choose common-cathode RGB LED .

Hardware Connection

Please complete hardware connection according to the picture below.

After connection, you can see:

Programming

Open Microsoft Makecode, write code in edit area. I would like to suggest you program by yourself first.

Of course, if you want to see the whole program directly, you can click the link below. Just click "Edit"on the right top corner of the interface you opened, then click "Download" on the right bottom to download code into micro:bit directly.

Code Explanation

Change Value

Set the value for local and global variables. change r by 1 equals "r=r+1".

repeat

Run part of the program the number of times you say.

function

A function is some amount of code you can reuse in your program. You create a function using a function definition which names the function and has its code. A function call is when you use a function by its name somewhere in your program.

call function

Use a function by its name somewhere in a program.

In this experiment, we have set three variables: r、g、b, which is used to record brightness value of red light, green light, and blue light separately. At the initial status, only red light will be illuminated.

Then we have to create 3 functions to realize gradual change among the three colors.

For example, "RtoG" function is for realizing transformation of red light and green light. If we implement the circulation of the function for once, then P0 analog input "-1"( red light brightness decreased a little bit)

, and P1 analog input "+1" ( green light brightness increased a little bit). After implement circulation for 1023 times, P0 analog input become "0" (red light turned off), and P1 analog input becomes "1023"(green light illuminated). Finally, it has realized gradual change smoothly.

Circulate the three functions to realize color gradual change smoothly.

Experiment Result

RGB LED light will shift among the three colors: red, green, and blue smoothly in balance.

1

Question:

If we want to use RGB LED to emit cyan light, magenta light, yellow light, then how to design circuit and program? We look forward to your comments or further discussions with us.

Relative Readings:

Start Your Micro:bit Programming Trip

Micro:bit Experiment 01: LED Scroller —— Elecfreaks Mirco: bit Starter Kit Course

Micro:bit Experiment 02: Button —— Elecfreaks Mirco: bit Starter Kit Course

Micro:bit Experiment 03: Trimpot —— Elecfreaks Mirco: bit Starter Kit Course

Micro:bit Experiment 04: Photocell — Elecfreaks Mirco: bit Starter Kit Course

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Hi! I am a robot. I just upvoted you! I found similar content that readers might be interested in:
https://www.elecfreaks.com/9978.html