Page created on 25 May 2017

NOTICE: if you like this page, save it to your hard drive! It may disappear at any time!

(-also save the arduino sketches on Pastebin too-)

Overview: this page is about how to program a microcontroller to operate an RGB LED.

In particular, it was written for someone who had bought a anti-vandal push button with RGB ability for PC modding and didn't know how to make the RGB aspect work, and they expressed interest in animated effects.

This was the button they had:

https://www.tindie.com/products/Fusion/12mm-and-16mm-antivandal-rgb-colorpixel-buttons/

[screenshot of above appears below]

I don't have one of those buttons, but the text description on that page implies that the three primary-color LEDs are connected in a common-cathode configuration.


Sections of this page:
1) Some smaller dev boards, mostly Arduino
2) What is a DC buck and who cares?
3) What is a LED, and why it needs current control
4) Into the abyss: setting up a Nano to do this
5) final sketch notes


1) Some smaller dev boards, mostly Arduino

Arduinos are termed a "single board computer". They are basically a small circuit board fitted with a programmable computer processor. Some single-board computers do run Linux but Arduinos don't have an operating system; they can only run one program at a time. The processor has its own memory to store the program in, and when power is applied, the processor starts up and runs the single program in its memory. A program written for an arduino has an .ino extension, and it is called a "sketch".

Arduinos are not the only brand of single-board computer around; there are others with far more memory and much higher operating speeds. Ardunos are chosen here because they were designed with beginner users in mind and they have several features that make them particularly easy to use.

Firstly, here is a diagram of the different USB 1/2 plugs. Different boards typically have different plugs, and you need the right cord to connect them to your computer. The more-common plugs are highlighted.

Below is a photograph of some Arduino and Digispark boards, with a 6-inch-long ruler for scale. For all of these different boards, there is an Arduino IDE plugin to allow programming them using the Arduino IDE software.

Below is a brief description of the dev boards shown above.

A) Arduino Due: This board has 54 pins, but a few of those are power and ground pins. Arduino doesn't make these now but there's still China sources making them for about $15. It has an ARM processor which is the biggest-and-baddest of the Arduino processors. I have a couple but haven't ever used them for anything, as I've not built anything that really needed them. It has a micro-B socket for programming, and the input/output pins are 3.3 volts.

B) Arduino Mega clone: this board has 54 input/output pins that are 5 volts, although some of those are power and ground pins (I think there is really 47 input/output pins). The USB socket is usually B but some have micro-B. The input/output pins are 5 volts. These can be bought for about $7 from China sellers. The Mega looks like the Due but isn't; the Due is a much more powerful processor.

C, D and E) These Arduino Uno clones. These are Chinese clones, showing some of the variations available.
[C] uses a removable processor chip, often called a "DIP chip" (where the green arrow is pointing).
[D] uses an SMD chip, and has extra ICSP header pin holes (also green arrow).
[E] is also an SMD processor but has two more analog pins, A6 and A7 (again the green arrow). The normal Arduino Uno design does not have enough room to allow using all of the available input/output pins that the processor really has, so it only has 6 analog I/O pins. With this model, the manufacturer placed the two extra pins along the board edge (circled in red).

Uno boards usually come with a style-B plug or micro-B. The normal Uno style is the B plug, and the "Leonardo" style is the micro-B plug.

All of the Uno clones above have the same outer dimensions, they cost about $2.50 to $3 for China clones, and the main groups of pins (the ones along the left and right edges) work the same. Mostly,,,,,

[F] This board looks like an Arduino, but isn't. It is a WeMos D1, which is based on the ESP8266 processor and wifi module. The processors in these has a lot of memory and is very fast--but all of the left and right pins don't really work, since the processor doesn't have enough input/output pins to operate them all. They just made it shaped like an Uno so that you could use Uno proto shields (blank shields) on it. These cost about $3.50 from China sources; WeMos has their own store on Aliexpress.

The WeMos can do lots of cool things, but it's no good here because it only has one analog input line and we need four analog input lines. The WeMos D1 board is included in the photo just to demonstrate that everything that looks like an Uno, isn't an Uno and can't do the same things. There is a number of other oddball "Uno-like" boards out there with totally different processors on them (search Google for "Waveshare Uno" to see some).

G) Arduino Nano clone: this uses the same processor as the SMD Uno (boards D and E) but is made into a smaller package overall. All the Uno's input/output pins are present, but not all the power and ground pins are present. All of these I've seen have a mini-B style plug, and may or may not have the header pins soldered in. They cost about $2.50 from China sources.

H) is a screw shield for the Arduino Nano boards. With a Nano, the pins are normally soldered in sticking downward. You can plug a Nano into this board and then you have screw terminals for attaching wires, which makes it somewhat larger but easier to work with. These cost about $1.50 from China sources.

I) Arduino Pro Mini: this is an even-smaller version of the Uno, but it has a difference. It has no USB programming interface built in, so you also need a USB-to-serial adapter (J) to program it. You connect the Pro Mini to the USB-to-serial adapter with four wires, and then plug the serial adapter into a PC USB port, and it works like the other boards. Then you disconnect the programmer and install just the Pro Mini board in your project.

Also, Pro Minis have two kinds of variations available: you can get them with either an 8mHz processor speed or 16 mHz, and you can also get them with either 5-volt input/output pins or 3.3 volts.
The Pro Mini costs around $1.75, but the USB-to-serial adapter costs another $2 or so.

K and L) These are Digispark clone boards. These use a smaller processor than the Uno, and these only have 6 input/output pins. These are the smallest ready-to-use dev boards I know of, and they plug in to the Arduino IDE, so they are easy to program. There is two designs common: style [K] has a micro-USB port, and style [L] has edge connectors and fits directly into a style-A USB port for programming. Both end up being about the same size overall.

UPDATE: I could not get the Digispark boards to work on my Windows 10 PC. There are issues with the Windows drivers and the Digispark bootloader. If you have a Mac or Linux it might work. I didn't spend a whole lot of time searching for solutions beyond the Digispark wiki; the last updates seem to be over a year ago. Also I was using a clone board and not an official Digispark-made board, but users of the official boards reported the same problems. There is reports of this issue going back at least two or three years so I don't know how likely it is that a solution will be forthcoming. In short: the Digispark boards use an odd bootloader that only appears as a USB device for the first 5 seconds it is connected, and this prevents most USB troubleshooting methods in Windows from working.

Also, the Digispark boards turned out to not have enough input/output pins. Somewhere late in this project I realized that the motherboard could be sourcing or sinking current to light the LEDs, and so both the (+) and the (-) connections of each LED needed to be measured. Since there's two LEDs (for the power and the hard-drive) that means 4 inputs, and the RGB LED requires 3 outputs... and Digisparks only have 6 input/output pins. So I left them in the photo above, but they are out of this game.

For this project, I wrote the sketch for the Arduino Nano (#G above), and also tested it on an Arduino Uno.


2) What is a DC buck and who cares?

Inside a PC the normal power supply gives off four DC voltages: 12 volts, 5 volts, 3.3 volts and (I think) 1.1 volts. For this project we want the Arduino processor to be able to give the LEDs at least up to 5 volts, so the 3.3 and 1.1 volt levels are too low to work here. That leaves 12 and 5 volts.

Arduinos will run off of USB power, which is only 5 volts--but the problem they can have is that they "lose" some voltage interally. With only 5 volts power, they aren't capable of putting out 5 volts over the output pins. The ideal voltage for them is around 7 volts, but there's no source of 7 volts on a normal PC power supply.

A DC-DC buck is a device for converting a higher DC voltage down to a lower DC voltage. Why you may want one is because even if a device (like an Arduino) says that it can run off 12 volts DC (which it can) the simple voltage regulator built-in to the Arduino boards has a practical limitation: the amount of current it can produce at the lower voltage is indirectly proportional to the amount of volts it must lower from the source power supply.

So what I normally prefer to do when building with Arduinos is to use a 12 volt DC power supply, connect a DC-DC buck to that, and then adjust the DC-DC buck to 7 volts. The Arduino then is connected to the 7 volts, and runs much cooler and can provide more electrical current than it would off of 12 volts.

When I was writing and testing the sketch on the Nano, I was running it off the USB plug power--which is only 5 volts, and it worked just fine. BUT,,, I didn't run the LEDs at full power (255 PWM value). The maximum I was lighting them up to was only around 100 (PWM value). And I had more resistance than normal for the LEDs I hooked up.

So if you build yours exactly the same as I did mine, then it may work okay when run straight off 5 volts. In general however, I would say you will have fewer problems if you use a DC-buck and run your dev board off of 7 volts.

A DC-DC buck always loses around 2 volts internally, so the output side is always going to be at least that-many-volts lower than the input side.

The picture below shows two different DC bucks that are common. I used the smaller one for this project. You connect an input DC voltage to one side, and there is a screw adjuster that allows you to set the DC voltage that comes out to a lower amount on the other end. Some DC-DC bucks are fixed-output, but you want to get one that is variable-output for this use.

The common name for the smaller one is "mini-360 DC buck" or "MP2307 DC buck". It can provide around 3/4 amp, which is way more than we need here, but it is the smallest DC buck around.

The common name for the larger one is "LM2596 DC buck". It can provide around 3 amps, which is really way more than we need here, but if you can find it cheaper than the little one then it will work also.

These are the two most common adjustable ones I've seen around, but there are others too.

There are also modules that look just like these, called boost modules. What they do is they produce a higher voltage on the output than the input. I guess you could get a boost module and then connect it to the 5v power and set it to put out 7 volts, if you wanted.... I tend to think in terms of reducing a higher voltage, myself.


3) What is an LED, and why it needs current control

An LED is a semiconductor that doesn't behave like a light bulb. A light bulb has a certain amount of resistance just from the filament inside, and when hooked to the correct amount of volts, the proper amount of current flows through. LEDs don't do that. LEDs have a bias voltage: lower than that voltage they have a high resistance, and above that voltage they change to having a very low resistance. They don't have anything inside to control the current flow themselves.

The bias voltage is often called the "forward voltage" of the LED.

So let's say you want to run an LED off of a DC voltage supply. You need to know the forward voltage of the LED, the current limit of the LED and the voltage that you will connect it to. The voltage source must be higher than the LED's forward voltage value. And then you need a resistor of the correct value so that it will limit the remaining voltage (the difference between the power voltage and the LED's forward voltage) to the current limit you want.

Different LED types have different forward voltages, but for single-color red, green or blue LEDs there are some general figures you can usually assume:
red and green LEDs have a forward voltage of about 2.1 volts
blue LEDs have a forward voltage of about 3.1 volts

all of the above three colors has a maximum current of 20 mA.
It is usually safe to assume that the maximum current for a single-color LED is 20 mA, although the actual figure may be higher.

For other colors, you need to look up the specs.
At www.superbrightleds.com for example, they show a yellow 5mm LED that is stated as 3v and 50 mA, and an orange 5mm LED that is stated as 3.1v and 50 mA max.

For white LEDs and high-output type LEDs like Luxeons, Crees and Piranhas, you have to go get the specs. Many white LEDs have a forward voltage of 4.1 volts or more. The maximum currents that "flashlight-type" LEDs can use depends partly on what kind of heat sink they have.

For infared and UV LEDs again you must get the part specifications: IR leds normally have low forward voltages of only around 1.1 volts, but many have rather high current ratings of 50 to well over 100+ mA: they aren't visible but can get physically warm to the touch when running. UV LEDs often have high forward voltages over 4 volts and high current limits of 50+ mA.

If you shop around you may find places selling "12 volt LEDs", or some-other-voltage LEDs, usually with short connecting wires already attached. These aren't special LEDs; what this means is that they alread soldered on the correct value of resistor to work properly on the stated voltage.

In our case here, we want to run a red, a green and a blue LED. There are LED resistor calculators online that will figure out what values of resistors you need, for a given LED forward voltage and power source voltage.
One such page gave these results, for running off of 5 volts, and for a 20 mA current target:
red LED = 150 ohms
green LED = 150 ohms
blue LED = 100 ohms

For my Nano setup I guessed from memory about the resistors and used a 220 ohm resistor each for the red LED and the green LED, and a 150 ohm value for the blue. This would mean that mine would appear a bit dimmer at maximum power, but all three colors still lit up quite well even when the Nano was running off only USB 5 volt power. And a lot of people tend to think that PC case LEDs are usually too bright anyway... Which leads to our next section...


4) Into the abyss: setting up a Nano to do this

The Pretty Vandal sketch operates a single RGB LED and allows you to set the separate primary colors and maximum brightness of the power and hard-drive-activity indicators, as well as the rise rate and decay rate for each of the primary colors for those two indicators. It does not include any way to operate the normal case power and hard-drive-activity LEDs.

The Pretty Vandal PlusPlus sketch is another version of the sketch that runs on an Arduino Nano or Uno. In addition to the single RGB LED setup that the above sketch runs, the PlusPlus version can also operate the normal power and hard-drive-activity LEDs. They are only a single color, but it allows you to set the maximum brightness and the rates that they rise and decay at.
,,,
Additionally, each of the three separate output features in the PlusPlus version can be separately enabled (the RGB output, the power LED and the hard-drive-activity LED). So you can use it to dim and fade the normal power and hard-drive-activity LEDs of a PC case, even if you don't have a RGB power switch for it to operate.

Both the Nano and the Uno images below show how to connect either the 5v or the 12v/7v versions.

Youtube action video of both setups: https://youtu.be/h_f2d-F2hKY

Both the pretty_vandal_RGB.ino and pretty_vandal_plusplus.ino files works with both the Arduino Nano and the Arduino Uno, but the pin assignments for each are not the same. Both sets of pin assignments are included in the sketch, and you need to comment out the one you don't need.

Pretty Vandal Pastebin link: https://pastebin.com/aLiwi4rK

Pretty Vandal PlusPlus Pastebin link: https://pastebin.com/N0kp2D82

Below is the hookup guide for the Nano, for either sketch.

Common-cathode refers to how the RGB LEDs are connected. LEDs have two wires, named the cathode and the anode. RGB LEDs often have either the cathodes or the anodes connected, so that it only requires 4 wire connections instead of 6. It is possible to use either type, but this sketch and diagram are for the common-cathode type.

~

~~

~~~

~~

~

Below is the hookup guide for the Uno, for either sketch.


5) final sketch notes

In the Pretty Vandal sketches, the power LED input is checked 10 times a second, the hard-drive-activity light is checked 50 times a second and the LED display outputs are updated 10 times a second.

One problem with the hard-drive-activity input is that it flickers on and off a lot at very short intervals. This becomes a problem when you want the hard-drive-activity color to "fade" down, since often there's not much times between that input blinking off and on again.

Also note that while the power activity LED needs to fade between three states [off/sleeping], [constant-on] and [hard-drive], the [hard-drive] color always has to fade back to the power colors. So the difference in color change is between the hard-drive RGB settings and the power-on settings, not between the hard-drive settings and [LED=off].


[~end of page~]