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 with your PiicoDev RGB LED module and a Raspberry Pi Pico. We'll connect them up, get some example code working, and by the end of the tutorial we'll even be able to control multiple modules like in my little road intersection here. You can see I've got two traffic lights operating independently. Let's get started.

Now to follow along you'll need a Raspberry Pi Pico with the pin soldered facing downwards, a PiicoDev RGB LED module, an expansion board for Raspberry Pi Pico, and a PiicoDev cable to connect everything together. Plug your Pico into the expansion board making sure the USB connector is on the same side as the two-pin battery connector. Plug in your PiicoDev cable at the bottom and connect your RGB LED module. And for the first example make sure that the ID switches are off and that the ID jumpers are unsoldered. This is how it should arrive out of the box. And I've just mounted everything to this PiicoDev platform to keep it nice and stable.

Next, connect to your computer with a USB cable. In this guide's article find the download MicroPython module section and right-click each link, select save link as. I'm going to save all these files into a PiicoDev directory under my documents. We're going to be using Thonny for this tutorial. If you've never used Thonny with Raspberry Pi Pico before check out our guide for that first.

In Thonny, navigate to where you've saved your files and connect to your Pico. Select the first file, hold shift, select the second file, right-click and upload to. And that will upload all the files to our Pico. Let's open up main.py. This script is ready to run so press ctrl R to run and you'll see we get a red, green and blue LED. This is a really simple example that just illuminates the first LED to red, the next LED to green and the last LED to blue.

Let's take on controlling multiple RGB LED modules. First, we need to modify the code. Open up main-2.py in Thonny, and upload it to your Pico. This code is set out to control four RGB LED modules. The pins we use are predefined in the code, which means you can connect and control up to four RGB LED modules, just like this road intersection here.Taking a look at the code, we import the PiicoDev RGB functionality and a function called "wheel" for later use. We also import "sleepms," a function that creates a delay. The LED module is initialized, and a series of colors are defined as three-element lists, using values between 0 and 255 to mix red, green, and blue and create various colors. We have a "set brightness" function that can also be set between 0 and 255, acting as a scalar for the brightness.

At "example one," LED set pixel is called, setting pixel number 0 (the topmost pixel) to red, pixel 1 to green, and pixel 2 to blue. We then call "leds.show" to update the physical device with the new colors. After a three-second delay, "leds.clear" turns off all LEDs.

Individual LEDs on the RGB LED module can be controlled using this code. Changing the color of a specific LED is as simple as changing the color label of the corresponding pixel. For example, changing the color of pixel number one (this LED number one in the middle) from green to magenta creates a nice purple color.

Next, we comment out "example one" and uncomment everything under "example two." This code sets all LEDs to the same color, selected from the color wheel. The program cycles through the color wheel using the "wheel" function - this creates a smooth transition between colors over time.are doing. In this example code, we have an LED display module with a power LED in the top left that turns on and off periodically. The LED is always on by default, but we can turn it off if desired.

The example code starts with two state variables initialized to zero and true respectively. We then use an infinite loop to set the color of the LEDs using the color wheel. As the loop progresses, the color of the LEDs changes slightly, creating a colorful display. We use the fill function to fill the LEDs with the chosen color, and this function automatically calls the show function as well.

In addition to the colorful LED display, the example code also toggles the power LED state every 100th loop using the modulo function. This turns the power LED on and off, creating a flashing effect. We achieve this by passing the state of the power LED to the power LED function, which updates the state of the onboard power LED.

Finally, we increment our loop counter and introduce a small delay. This means that we can adjust the speed of the power LED and the color changing of the LEDs by changing the delay duration in the code. With these variables, we can adjust the speed and behavior of the LEDs to our liking.Looks like gradually over time.

Jump back onto the guide and copy the fade LEDs remix and paste it over everything in main, and now we can run it. Here we have the LEDs just glowing on and off on the red channel so they gradually get brighter and then dimmer, and we do this in a similar way.

We do the regular setups, and then in an infinite loop, we have two for loops for x in range 255 so from 0 to 255 which is the brightest, we just set the red channel to x so x will increment all the way up to 255, and then in the next for loop, we have x in range 255 to 0 with an increment of negative 1, and so we've incremented our LEDs all the way up to 255, and the second loop decrements them down to 0. So this loop will handle the glow on, and this loop will handle the glow off.

Now it's possible to control multiple RGB LED modules at the same time. Here I have two modules connected to the same Raspberry Pi Pico, and I'm able to do this by changing the ID switch on one of them.

Our first device is like it's always been for this tutorial with all the switches off, and our second device just has the ID number one switch set to on. Now that both modules have a separate ID set, I've copied the traffic light example from the tutorial, and I'm running that in Thonny.

Now when we initialize our LED modules, I've initialized traffic A as our default settings for the PiicoDev RGB LED module. That will be this one with all the switches off, and traffic B is initialized and we pass in an ID argument which is the position of the ID switches. Since we've only set ID one to on, it is the only element in this list that is set to a one. ID two is off, and so the second position has a zero, and both the jumpers on the back are unsoldered, so those positions are zero as well.

Now we can control two LED modules just by referring to them by their names.

What follows in this loop...Are more of what we've seen where we're just calling setPixel and show but this time we get to call setPixel for traffic A and for traffic B independently.

This light is green so traffic flows this way but now it's turned to red so traffic flows this way and you can see that on each light we transition through amber in the middle.

And so there you have it a bunch of fun examples for the PiicoDev RGB LED module. Now if you'd like to learn more about addressing multiple modules check out our PiicoDev connection guide for smart modules and if you've made anything cool from these examples or you just have some questions let us know on our forums.

We're full-time makers and here 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.