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

Let's make a colourful lighting display with a PiicoDev RGB LED module and a Raspberry Pi. We'll connect these two together, get some example code working, and by the end of the tutorial, we'll even be able to control multiple modules independently. Let's get started.

To follow along, you'll need a Raspberry Pi single board computer. I'm using a Raspberry Pi 4 Model B today, a PiicoDev RGB LED module and adapter for Raspberry Pi, and a PiicoDev cable. 100mm cables are great for Raspberry Pi projects.

Mount the adapter to your Pi's GPIO, making sure that the Ethernet arrow points towards the Ethernet port. On a Pi 3, this will be a USB connector. Connect your PiicoDev cable to one of the ports, and connect the other end to your LED module. And I've mounted everything to this PiicoDev platform to keep it nice and safe.

We'll assume you're already comfortable using a Raspberry Pi like a desktop computer. If you need help with that, check out our Raspberry Pi for Beginners workshop.

Power on your Pi, and go to Preferences, Raspberry Pi Configuration. We need to make sure we have I2C enabled. Under the Interfaces tab, find I2C, and make sure it's enabled.

In the article for this tutorial, find the Downloads section, and we'll download the example script. Right click that link, select Save Link As, and I'll save mine to a PiicoDev directory in my home directory. I'll call that rgb.py. I'll right click that and open in Thonny. And this code is ready to run.

But before we do, just make sure you have PiicoDev installed, or upgrade it if you need to. Go to Tools, Manage Packages, and search PicoDev. That's with two I's. Find the package, and just click Upgrade.paragraphed transcript:

Now we're ready to run. Press Ctrl R to run the script, and you'll see we get a red, a green, and a blue LED, and then they all go out.

We start off by importing the PiicoDev RGB LED driver module, and a function called wheel, more on that later. We import a sleep function to create a delay, and then we initialize our RGB LED module as LEDs.

There's some predefined colors, and then we call setPixel, 0, red.

That will set pixel number 0 on our module to the color red. And likewise, pixel number 1 to green, and pixel number 2 to blue. So our pixels are numbered 0, 1, and 2.

Now looking at these colors, they're actually defined as a three element list. The first element is the red component of the light, the second is the green, and the third is blue. And these numbers can be integers between 0 and 255. So of course, red is defined as all red and no green or blue. And you can see that in green and in blue as well. And there's a few other colors defined here, so we can have a bit of a play around.

I'm going to change this green to magenta. And now when I run the script, we can see we have red, magenta, and blue appear on our LED module.

It's actually the call to LEDs.show that updates the physical device itself. And then we sleep for 3000 milliseconds or three seconds, and finally call LEDs.clear, which blanks the display.

If we comment out everything in example one, and uncomment everything under example two, I'm using the keyboard shortcut alt 3 and alt 4 to block comments. Let's give example two a run.

We have our LEDs scrolling through various colors, and curiously, our power LED is blinking as well.

Let's check out the or Install if you need to.the LED color and one that counts the brightness of that color. Within those loops, we have another loop that updates the color of each LED based on the values of the red, green, and blue components that we get from our sine and cosine functions. And then at the end of each inner loop, we call LEDs.show to update the LEDs with the new colors. We also have a delay within each inner loop to control the speed of the fade. This is a great example of how loops can be nested to achieve more complex functionality. And with the power of CircuitPython and the Adafruit Circuit Playground Express, we can easily create these types of light shows without needing to write a ton of code. It's all about experimenting and having fun.In this tutorial, we're going to be learning how to use our brand new LED modules to create a traffic light. The LED modules we're using come with an identification system that allows us to identify and control each individual module. So we're going to create a traffic intersection with two traffic lights that we can control independently.

To begin, we're going to set up a simple LED module with the identification system. We define some colors for red, amber, and green, and the remaining light from zero to 255. We're using a loop that starts at zero and counts up to 255, passing that into the green channel. Then we have another loop that starts at 255 and counts down to zero, passing that into the red channel. Our loops just count the red channel up and then count the red channel down. And of course, we're calling fill, so we don't need to call show.

Next, we set up a little diorama of a traffic intersection to demonstrate how we can independently control the state of both traffic lights. Traffic light A is set up for traffic going one way and B is set up for traffic coming the other way. We can see that we're able to independently control the state of both of these traffic lights.

Traffic light A is in the default state, where all the ID switches are set to off, and all the ID solder jumpers are unsoldered. Traffic light B, however, has ID switch position number one set to on. In the code for this project, we're adding the argument ID, and it's a four-element list. Each element in that list is the state of the ID switch or jumper. We've set ID switch one on, so we put a one in that place and a zero in every other place. If we had set ID switch number two on instead, then we would have a one in the second position and a zero in the first position.

By changing the state of that switch and passing in this new ID, we're able to control two modules independently. And that's how we can create a traffic intersection with two traffic lights that we can control independently.Code is just the sequencing of each traffic light for green, red, and amber. So here traffic flows one way and we have traffic light A set to green and traffic light B set to red. We transition through amber and then when traffic flows the other way we have traffic light A set to red and traffic light B set to green. We transition through amber again and we go back to the start.

You can find this code at the bottom of the tutorial article. If you want to learn more about how to control PiicoDev modules that have settable IDs, check out our PiicoDev connection guide.

So there you have it, a few of the basics on how to drive the RGB modules and then a little more complicated a project at the end. If you make anything cool from these examples or if you just have some questions, let us know on our maker forums. We're full-time makers and here to help.

Catch you next time.

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.