The snap:bit is an electronic component for the Snap Circuits educational electronic kit. It features a socket for connecting the BBC micro:bit. This allows the Snap Circuits to be programmatically controlled by the micro:bit.
This project demonstrates how to automatically change the color of the RGB LED of the Color Organ (U22) by programming it with the BBC micro:bit. The program will smoothly switch the RGB LED through all colors of the rainbow for a spectacular show.
This project builds on top of what we learned in the Program the RGB LED of Color Organ with Micro:bit project.
Snap Circuits diagramBuild the circuit shown in the diagram above.
Note that the Color Organ must be connected to a 6V power source to operate properly. This is the reason we use two Battery Holders (B1) connected in series.
At the same time, the BBC micro:bit must be connected to a 3V power source. Powering the micro:bit with a higher voltage will damage it.
To avoid accidents with powering the micro:bit with a high voltage, we power it with a USB cable instead of from the battery holders of Snap Circuits. This also supplies a stable 3.3V current from the micro:bit pins to the color organ, which is important for proper operation of the RGB LED.
CodeYou can build the code yourself in the MakeCode Editor. You will find the "analog write pin" block under the Advanced > Pins section.
Alternatively, open the ready project here: https://makecode.microbit.org/_cqgRaRHp7Xv1
Once ready, download the code to your micro:bit.
How it works...Close the Slide Switch (S1) that is between the two Battery Holders (B1). This closes the circuit between the (+) and (-) snap buttons of the Color Organ (U22), which turns it on.
As the micro:bit is powered from the USB cable, it triggers the "on start" event. The micro:bit makes sure that RGB LED of the color organ is completely switched off by writing "0" signal to all pins P0, P1, and P2 of the micro:bit. These pins are connected to the R, G, and B snap buttons of the color organ, respectively.
Then the micro:bit enters a for-loop that incrementally writes a higher value to pin P0, which increases the intensity of the red color of the RGB LED. We can see how the color organ turns on slowly by smoothly turning its RGB LED on to a bright red color.
When the execution of the "on start" block finishes, the micro:bit starts executing the "forever" cycle. On each iteration, the micro:bit will continuously do the following:
- Incrementally write a higher value to pin P1 to increase the intensity of the green color until the RGB LED emits a yellow color.
- Incrementally write a lower value to pin P0 to decrease the intensity of the red color until the RGB LED emits a green color.
- Incrementally write a higher value to pin P2 to increase the intensity of the blue color until the RGB LED emits a cyan color.
- Incrementally write a lower value to pin P1 to decrease the intensity of the green color until the RGB LED emits a blue color.
- Incrementally write a higher value to pin P0 to increase the intensity of the red color until the RGB LED emits a magenta color.
- Incrementally write a lower value to pin P2 to decrease the intensity of the blue color until the RGB LED emits a red color.
This process repeats as long as the circuit is powered, performing a spectacular show of endless shades of colors.
Note that after every writing to the micro:bit pins, the execution is paused for a very short time - 10 ms. This is to pace the speed of the show. Otherwise, colors will switch too fast to enjoy them.
Now, take some time to play with the code. Try experimenting with the duration of the pauses as well as with the order and values of the for-loops. How does this change the performance of the show?
Comments
Please log in or sign up to comment.