Understanding RGB LEDs – a deep dive

Understanding RGB LEDs - a deep dive

Hi, this is Wayne again with a topic “Understanding RGB LEDs – a deep dive”.
Hello, i’m carrie sunder of alpenglow industries, where we make everything from learn to solder kits to tools that help you with circuit board layout. Today, i’m going to show you how you can use different kinds of square waves to create different colors of light, so that you too can turn your old death defying sandboards into beautiful works of art. This is part of a make series on measurement and test equipment and is sponsored by liquid instruments. They make an all-in-one tool called the moku go and we’re going to use its logic, analyzer instrument to look at our color controlling square waves.

You can find more info on me make and the moku go in. The links in the description below square waves are the backbone of all digital communication and control. Anytime, you use an arduino or other microcontroller to talk to another device. You’Re generating square waves square waves are very simple structures. They can either be low or high, or in other words their voltage can either be at ground or at a fixed level. Like 5 volts by alternating between the two, we can create pulse trains, which can represent all kinds of information, like ones and zeros, which could translate to a number code to control the color of an led, or they could represent percentages that control the brightness of an Led we’re going to look at these waves using the logic analyzer in the moku go.

The moku go is a device that has eight different test instruments, plus a power supply in a small portable form factor. You can use it as an oscilloscope spectrum, analyzer waveform, generator, pid controller data, logger frequency response, analyzer, arbitrary waveform, generator or logic analyzer, just plug it into your computer and a power source and you’re ready to see what’s happening inside of your circuit. Colors colors are wonderful and they make everything. Fun leds or light emitting diodes are components that emit color when powered and red green, blue or rgb leds can create an entire rainbow of colors. When we add addressable rgb leds into the mix, we now have the ability to easily add both color and patterns to our projects with only three wires. But how do they work? Addressable leds rely on a microcontroller to tell them what color they should be and the microcontroller repeats these color instructions. Many many times per second, when you include telling the leds to be no color or turning them off entirely.

You can make them flash or twinkle or do pretty much whatever you want here. We’Ve hooked up a small strip of five addressable rgb leds to an adafruit metro mini 328. The mini is programmed and powered through the usb port. Five volts from the usb port is powering our strip.

There’S a single communication line coming from pin d4 on the mini and there’s a ground connection, we’ve also soldered a wire to the communication line between each led plus the beginning, input and end output and attached those to lines one through six on the digital. I o cable going into the moku go. This cable is used with the logic analyzer, which allows us to see many more digital signals than we can with the oscilloscope. The key word is digital. It can only display square waves which are either high or low, with almost instantaneous transitions between the two sorry, no triangle or sine waves allowed. Here we can now see all our led signals in the logic, analyzer and i’ll label them for clarity. As for our signals, what’s going on, the microcontroller generates an initial message that is just a chain of colors one color for each led on the strip. The first led sets itself to the first color in the chain, then chops that color out of the chain and passes the remaining message onto the next led, which then does the same thing.

So the message gets shorter and shorter, as it’s passed to each led until there’s no message left at the output end of the strip. Right now, our led strip is set to be full red, full, green, full blue. Then a mix of colors to make orange and purple, let’s dig into the color message, a little more and decode it. In our program, we set the led color by choosing a value between 0 and 255 for red for green and for blue, including 0, which is off.

There are 256 possibilities. 256 in binary is 2 to the 8th, meaning that our shades can be represented by a string of ones and zeros. That is eight digits, long or eight bits long. So there are three colors each represented by an eight bit number, which means the entire color message. Should probably be about 24 bits long and in fact we can see in our logic analyzer that our last single color message has 24 pulses. Now all we have to do is figure out which pulses represent ones which represent zeros and which sections of the message represent: red, green and blue. Going back to our red led, we see its message, has a string of short pulses at the beginning, short and long pulses in the middle and another string of short pulses at the end. We know that, for an rgb led to appear red that blue and green both have to be set to zero and red is set to some value up to 255.

So the two portions of the message, with only short pulses, have to represent blue and green at all zeros, and our middle section has to represent red with both short and long pulses. Then, with the long pulses being a one looking at green and blue, we can confirm this pattern and further deduce that green is the first set of eight bits and blue is the last. So now we can decode our signal and check it against the software. We wrote for a red led, we have all zeros for green, a zero and seven ones, which is a 127 for red and all zeros again for blue for a green led.

We have 127 0 and 0.. Our blue led is 0 0 127. Our orange led is 32 64 0, and our purple led is 0 48 32 checking against our code. We see that our values are only half what we’ve set them to wait.

Understanding RGB LEDs - a deep dive

Why are they different? Ah, our code also has a brightness value, which is set to 127 out of 255 or 50, so the final values sent to the leds are going to be half of what we set in our code. Now we know how to control these leds and can create all kinds of cool effects. If we add off, which is all zeros and white, which is all ones or 255, we can make the colors change over time and position and make all kinds of beautiful, animations and patterns. You can add a diffuser to create even more cool effects cool. But what’s going on inside the led itself, you probably already guessed this from the color format, but each of these rgb leds actually contains three tiny leds, a red, a green and a blue one. If we keep green and blue at 0 and set red to 5, we’ll see a very dim red as we increase red to 255 red gets brighter and brighter. So the number controls the intensity or brightness of each individual, red, green or blue led and our eyes average them all together to make one single color. We can break this down and see it more clearly using individual leds and we can vary their brightness using a square wave called a pwm.

Understanding RGB LEDs - a deep dive

Pwm stands for pulse width modulation. So when we create a repeating series of square waves or pulses at a particular frequency and then adjust or modulate the duty cycle or width of those pulses, we’re creating a pulse width, modulated square wave, which people usually just call a pwm. When we use a pwm to drive an led, what we’re doing is turning that led on and off hundreds or thousands of times each second, our eyeballs have a slow reaction time and can’t see the individual on and off pulses when they’re happening that fast. What we end up seeing is an average of the amount of light emitted over a few fractions of a second.

Understanding RGB LEDs - a deep dive

If the pwm driving that led is on half the time and off half the time, we’ll see that led at half brightness, so the duty cycle or time the pwm is high is also the percentage of time. The led is on and directly corresponds to brightness. Here we’re using a pwm to fade a red, a green and a blue led we’re changing the duty cycle from zero to a hundred percent and then back to zero for each color. So each led starts it off fades in to maximum brightness and then fades out to off again now we’re overlapping these fading sequences, one led is always off in each cycle and the other two are mirroring each other, so one led is going from min to max Brightness and the other is going from max to min when we put these red, green and blue leds together in a single rgb led component, we get that color mixing effect and can fade through the rainbow back to our addressable rgb led there’s one more piece of Info that will help you successfully create colors.

We’Ve learned that our numbers for red, green and blue control both overall led color and brightness, but how exactly are color and brightness related? The color is determined by the ratio of each individual rgb value to each other. So if red is 20, green is 40 and blue is 80. We have a 1 to 2 to 4 ratio, which is a light blue color. The brightness is determined by the sum of the values.

So if we multiply each value by 2, we’ll still create the same color, because the ratio hasn’t changed, but it will be twice as bright. Similarly, if we divide by two it’ll be the same color but half as bright, knowing that we can create a cool color gradient, i hope you’ve learned a bit about how we can create different colors with square waves by using pwms and by sending coded messages. Thank you very much to our sponsor liquid instruments who loaned us a moku go, so we could show you exactly how we can manipulate itself for more videos about electronics, be sure to subscribe to make’s channel and our alpenglow industries channel and liquid instruments channel as well. Also check out the links below for our websites and for more information on the moku go see you next time.

You .