Many Colors with Variable RGB LED

Introduction

In this sample, we will connect an RGB Controllable LED to a Raspberry Pi 2 and vary the intensity level of each color using a technique called Pulse Width Modulation (PWM).many colors with variable rgb led

The ability to control the level of intensity of each of the three colors on the LED will allow us to make an infinite number of color possibilities. How can we control the intensity? An LED's intensity can be controlled in one of two ways. The first way is to control the amount of current going to the LED. This approach is somewhat complex and requires additional hardware. The second way is through Pulse Width Modulation and can be done with a few basic parts and the right code (for a good explanation of what PWM is an how it works see this blog: “PWM – What is it, How does it work and how to detect it“).

It is commonly known that the Raspberry Pi 2 only has only one pin for hardware based Pulse Width Modulation and therefore cannot be used to drive the three pins on the LED. To accommodate this, a software based PWM is needed. This sample will demonstrate how a software based PWM can control the intensity of the LED. The trade of between a hardware based PWM and a software based PWM is gong to be CPU overhead versus additional hardware. Although using a software based PWM has some limits it will get the job done!

Even though the GPIO APIs are only available on Windows IoT Core, this sample can run on your desktop but, of course, will not attach to the controller. This is because the sample uses the new meta data API's found in the Windows.Foundation.Metadata namespace to detect the presence of the GPIO Controller. This is useful to allow the code to be run on your desktop to help debug other aspects of the application.

Build the Circuit

Let's start by constructing the simple circuit. In this example I use a small breadboard with a cobbler board and 40-pin ribbon cable to connect the Raspberry Pi 2 GPIO to the board (using a cobbler and ribbon cable is not required). If you do not have these components I have included a wiring diagram that shows you how to connect directly to the Raspberry Pi 2 pins. Note that the hardware list calls for male to male connectors when using the cobbler board. If you choose not to use the cobbler board you will need the same number of wires, but male to female instead.

When placing the cobbler board on the breadboard, I have positioned it so that the first pins are in row 1 which positions the four pins for 5V+, 5V-, 3V3+ and 3V3- into the second row. The pin farthest from the ribbon cable on the cobbler is in row 20. Having this exact positioning is not critical but will help you follow the wiring diagram in this sample. The pins that are used by the cobbler board are highlighted in the diagram below.diagram many colors with variable rgb ledStart by placing the LED onto the breadboard. The red lead will go into position J26. The common cathode lead is inserted into J25. The green lead is inserted into J24 and the blue lead into J23. You will need to spread the leads out slightly to get them to insert at the same time into the these four positions on the breadboard.

For more detail: Many Colors with Variable RGB LED


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top