Let's code a dazzling lighting display with the PiicoDev RGB LED Module. We'll start off with some example code to control individual pixels, move onto generating beautiful colours and finish up with a multi-module demo: Controlling two modules to create a pair of traffic lights. Click through to the guide for all the code and useful extras.

Transcript

I'm going to show you how to get started making colourful patterns on a PiicoDev RGB LED module. We'll be working with a Micro:bit V2 today, I'll show you how to connect these two together, get some example code running, and then we'll even remix the code and get multiple modules working at the same time.

Let's get started. To follow along, you'll need a Micro:bit V2, a PiicoDev 3x RGB LED module and adapter for the Micro:Bit. and a PiicoDev cable to connect everything together. Plug your Micro:bit into the adapter, connect the cable to one of the ports, and connect the other end to your RGB module. And I've mounted everything to this PiicoDev platform to keep it nice and safe for the tutorial. And connect to your computer with a USB cable.

In the download section for this tutorial, find the three files we need to download, right click each link and save link as. I'm saving mine to a PiicoDev directory in my documents. Here they are.

We're going to be working with Thonny for this tutorial. If you've never used Thonny and the Micro:bit before, we have a guide for getting you started there. And if you prefer not to use Thonny and would like to use a web-based programming experience, we've made up a guide for python.MicroBit.org.

These examples are going to assume that your RGB LED module is in the default state, where the ID switches are off and the solderable ID jumpers on the back are both unsoldered. This is how it arrives out of the box.

Open up Thonny and connect to your Micro:Bit. and we'll upload all these files. So click the first file, hold shift, click the last file, right click and upload to Micro:Bit. Open up main, and this file is ready to run. Press control D to reboot your Micro:bit and run the script. We have a red, green and blue LED appear on the RGB module, and then they all go out.

Let's take a closer look at the script.We start off with the import to drive the module, so we import PiicoDev RGB and a function called wheel, more on that later. We import a sleep function to create a delay, and then we initialize our LED module as LEDs.

We've got some predefined colors here, we'll come back to those, but in example 1, we call LEDs.setPixel, and we're setting pixel 0 to red. So pixel 0 is this top LED here labelled 0. Now red has been defined as an RGB list. So here we have a list of 3 elements, 255, 0, 0. And that means that we have 255, which is the maximum for the reds position, then green is 0 and blue is 0. So likewise, when we call setPixel 1 to green, we have 0, 255, 0.

We're working in the RGB color space, so these numbers from 0 to 255 are the amount of red, green and blue light to mix together to make whatever color you like. So we set our 3 pixels to 3 different colors, and we need to call show to actually update the physical LEDs.

Once the LEDs are set, we call sleepMS for 3000 milliseconds, and finally we call LEDs.clear, which will blank the display.

I'm quite a fan of the magenta color, so I'll copy the name for magenta, and I'll paste that where green is in this example. I'll press Ctrl R to save and run, and here we have red, magenta and blue. That's quite nice because magenta is a mixture of red and blue.

By default, the RGB module has a brightness scaling that's about 50 out of 255. We can uncomment this line for setBrightness, and this will set the brightness to 127, which is still about half as bright as the display could be.

When we run the script again, it is like, it is much brighter. That's quite dazzling. If I highlight everything under example 1 and press Alt 3 to comment, I'll uncomment everything under example 2 with Alt 4, and then we'll run the next example.

Press Ctrl R to save and run. Now our color LEDs are smoothlystart the example with a loop counter initialized to zero and a power LED state set to true. This is going to be the state of our power LED. We can turn it off because on a display module you might want to remove that distracting green LED. By default, it will always be on.

In the infinite loop, we call the wheel function, which will pick a color out of the color wheel, and we pass it an angle between zero and 360-degrees. In this case, we're passing in our loop counter. The color is assigned to C, and we immediately call LEDs.fillC. That will fill every LED on the board with that color. We won't need to call show for this as it's automatic.

The next part of the example deals with toggling the power LED. This modular operator here basically just means that every 100th time we go through this loop, we're going to enter this bit of code. We invert the power LED state so that true becomes false, and false becomes true. Then we call LEDs.powerLED and pass it that true or false value. Finally, we increment the loop counter and put in a short delay.

If we change this plus one to a plus 10, everything's going to happen much faster. We'll move around our color wheel faster because we'll increment by a larger number each time. We'll also trigger this bit of code to toggle the power LED faster because now the loop counter is incrementing by 10. So this will trigger every 10th loop, not every 100th.

Switching to the fade LEDs example, we replace all the code in main and give that a run. Now we have the red LEDs glowing on and off nice and slowly.

With these examples, users can learn how to control different LED functions on their modules. Whether it's changing colors or fading on and off, the examples show how it's done. If users have any questions or need further guidance, they can always reach out to Core Electronics for technical support.Do the regular imports and setup.

This time our infinite loop has two for loops in it. For X in range 255. So this means execute this code 255 times, incrementing X each time we call LEDs fill and we have X in the red channel and then we just call sleep. So this for loop will steadily glow the LEDs on. Our next for loop decrements X from 255 to zero by negative one. So this for loop will then start at the full brightness and glow the LEDs off.

Now it's possible to control multiple LED modules by setting their IDs to different values. Here I have two PiicoDev RGB LED modules being controlled independently as if they're traffic lights at this little traffic intersection that I've created.

So here's traffic light A for traffic going this way and here's traffic light B for traffic going this way. And we can see each A and B are cycling between green, amber and red. So the traffic can only flow one at a time. A is green so we can travel in this direction and now it's red and B is green so we can travel in this direction. And it's going to constantly keep flipping, transitioning through amber each time we need to stop the traffic.

For this to work, each device needs to have a unique ID and that is set by the two switches on the front and the two solder jumpers on the back. We're leaving traffic light A in that default state like we did before where all the switches are off and all the jumpers are unsoldered. Traffic light B is the same except ID 1 has been set to on.

Now in the code, after we do our imports, we initialize each traffic light. Traffic light A we're initializing with all the default settings and traffic light B we're initializing with an ID argument. Here because we have ID 1 set, we put a 1 in the first place. If we set ID 2, we would put a 1 in the second place and a 0 in the first place. These last two positions are...If we soldered any solder jumpers, great. So now we have traffic light A and traffic light B initialized. We define some colors and then we just run this repeating loop that basically sequences green, amber, red for each light, making sure that one light is green at a time.

If you want to learn more about PiicoDev smart modules and how to address them, check out our PiicoDev connection guide. And there you have it, a bunch of examples and a fun little project for the PiicoDev RGB LED modules driven by a micro bit.

If you make something cool out of these examples or if you just have some questions, let us know on our maker forums. We're full-time makers and happy to help.

Until next time, catch you later.

Comments


Loading...
Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.