Wiring up the RGB LED from the Maker kit

Greetings! New to the forums so if there is already a topic in place or some sacred conventions I am breaking let me know =)

I studied CS so I know precious little about electronics beyond capacitors and transistors. I have the maker kit and am trying to wire up the RGB LED that comes with it. I’ve successfully operated the buzzer/motor/various sensors without issue, but the LED is befuddling me.

It says it has a common anode, along with the 3 cathodes for controlling the various colors. I’m used to this being exactly reversed: one common cathode attached to ground, with 3 anodes connected to analog channels.

So first off, why the difference and how would I wire this up as opposed to the common cathode type?

Second, the datasheet shows different operating voltages for the different color LEDs. What resistors would I put in series for each one to get the appropriate voltage/current?

Thanks guys!

1 Like

I started writing up a reply to explain how to wire up your LED and calculate resistor sizes, but while I was reading the datasheet for the LED, I noticed the ‘forward voltage’ (aka ‘voltage drop’) is higher than the spark’s 3.3v output on the blue and green colors. I thought maybe you could connect it to VIN, which should be 5 volts if powered via USB, but I’m not sure if that would damage the other GPIO pins on the spark core that the LED is connected to.

So I’m going to ping @kennethlimcp, @bko, @peekay123 and @BDub and see if we can get their input here.

You can find the datasheet for the LED here: http://docs.spark.io/assets/datasheets/makerkit/rgb-led.pdf

And I like to use an online resistor calculator like this: http://ledcalculator.net/

You’ll notice that in the datasheet, the avg voltage drop for red is 2.0v and the blue and green is 3.5v. So the red color is going to take a higher value resistor than the blue and green, I’m just not sure what that is yet.

Also, here’s a good example of how to wire it up: (Don’t use these resistor values though, the LED that comes with the spark core is different)

I wouldn’t bother with the 5V supply unless you really need to. The calculation is (Vsupply - Vfd)/If or supply voltage minus forward LED voltage, divided by the LED forward current you want up to the 20mA per pin allowed by the STM32F series.

So at 3.3V, the Blue and the Green LED, the forward voltage is greater than what the core typically provides at 3.3V, so a very small or even no resistor will be fine. For the Red LED with a 2.0V Vfd you have to drop (3.3-2.0) = 1.3V at the max current per pin of 20mA, so that is 65 ohms. If you want to match the brightness of the RGB LEDs, you might want a slightly larger resistor since the blue and green will not be at full brightness. From the datasheet the green LED looks to be a lot more efficient, so you may not need to anything.

If you do the math for the 5V case, the red LED will need to drop (5-2) = 3V at 20ma or 150 ohms. The blue and green will be (5-3.5) = 1.5V at 20mA or 75 ohms each.

3 Likes

I gotten beaten to it just because I’m in class :stuck_out_tongue:

1 Like

FYI, I would not drive the pins at a calculated 20mA. Stay safe with a 15mA calculation, hook to VIN so you get to use larger more common perhaps resistor values, and ensure you use 5V tolerant Digital pins (all except D2 is 5V tolerant).

One important thing to note, VIN has a diode between the actual USB 5V and VIN… so in practice you get about a 0.2V drop there.

Now follow me on my crazy thought process… you will learn how to find the right values to work with.

Blue and Green LEDs

4.8 - 3.5 = 1.3V (drop across the resistor)
1.3 / 0.015 = 86.66666 ohms (bah… not a good resistor)
Let’s use 100 ohm resistor since it’s common and see what that does to the current.
1.3V / 100 = 0.013A (13mA) … I’d say that’s an acceptable compromise.

Red LED

4.8 - 2.0 = 2.8V
2.8V / 0.013 = 215.3 ohms (hmm, let’s do our trick again and find a close value)
220 is really close, BUT since we are using 100 ohms already, just use two of them in series for 200 ohms
2.8 / 200 = 0.014A (14mA) sweet… you’ll never see the difference…

Now technically you’d want to hook each LED color up to PWM so you have total control over how bright each one is… and get that perfect color, or maybe create the perfect rainbow generator. Unfortunately there’s only two 5V tolerant PWM outputs (D0, D1). So what you SHOULD do then, is hook up the LED to the output of something like a ULN2003 darlington array. Each output is capable of 500mA, and you can hook the input of the driver straight up to the 3.3V PWM outputs (A0, A1, A4, A5, A6, or A7). Now just recalculate things for 30mA of drive (max of the LED) and don’t forget to account for the ~1V drop of the darlington driver outputs.

4.8 - 1.0 - 3.5 = 0.3V
0.3V / 0.03mA = 10 ohms.
At this low of resistance, you will likely end up over driving the LED if you are not careful. Your best best is to create a more known and regulated voltage reference for your LED power, or reduce the voltage drop of the driver by using discrete MOSFET drivers. It’s just a little more work to wire up. Here’s a good thread discussing that: https://community.spark.io/t/wiring-to-drive-an-rgb-led-strip/3888

At the end of the day you might just want to control the on-board RGB LED and call it done!

Or use a NeoPixel :wink:

Also, if you want to play it really safe when starting your wiring, you should use 270 ohm resistors… because at this level you’ll never exceed 20mA on any pin if you accidentally short the other end of that resistor to GND.

3 Likes

Thanks for the thorough answers guys!

@BKub regarding your answer, I thought in the common cathode type LED you could drive the brightness by varying the analog values (instead of pulsing). I presume you can’t do something similar with the common anode? I don’t have any PWM gizmos =)

Hi @nybblr

You can vary the brightness with common-anode or common-cathode. When you use an analogWrite() on Spark or Arduino, there actual output is a PWM waveform, not a digital-to-analog converter. The doc is here:

http://docs.spark.io/firmware/#i-o-analogwrite

The difference between common-cathode and common-anode is that the PWM values work backwards. For full brightness with common-cathode, you would do analogWrite(pin,255); but for common-anode, you would do analogWrite(pin,0);

Hi there !
I am triying to wire the RGB LED I recived with the Starter Kit, so an ANODE one (Rainbowduino). Even I considered the resistors values you gave, I only have 1k Ohms, 330 Ohnms and 10 Ohms availbles (Starter Kit).
So I did’nt use any resistor for GREEN (D0) and BLUE (D1) and I I wired 6x 10 Ohms resistors (serial) for RED (A0).

Them, I wrote the following code:

int REDPin = A0;    // RED pin of the LED to PWM pin **A0**
int GREENPin = D0;  // GREEN pin of the LED to PWM pin **D0**
int BLUEPin = D1;   // BLUE pin of the LED to PWM pin **D1**
int brightness = 0; // Full brightness for an ANODE RGB LED

void loop()
{

  analogWrite(REDPin, brightness);
  analogWrite(GREENPin, brightness);
  analogWrite(BLUEPin, brightness);

  delay(500);  // wait for 500 milliseconds to see the dimming effect
}

But nothing happens. Could you please help me finding the problem ?

Thank you guys!

brightness = 0

Won't do much, could you try 255

There's not going to be a dimming effect, since you don't actually do anything.
Try the following:

void loop()
{
  analogWrite(REDPin, brightness);
  delay(500);
  analogWrite(GREENPin, brightness);
  delay(500);
  analogWrite(BLUEPin, brightness);
  delay(500);
  analogWrite(REDPin, 0);
  delay(500);
  analogWrite(GREENPin, 0);
  delay(500);
  analogWrite(BLUEPin, 0);
  delay(500);    
}

This should go through various colors.

Hi Moors7!
Thank you for your quick answer. I changed the code as you suggest, and nothing happens :frowning: Do you think the resistors values I used wont be the origin of my problem ?

Could you please post your full code? That might make it easier to debug.

Also you need to add pinMode(each_pwm_pin, OUTPUT); // sets the pin as output

http://docs.spark.io/firmware/#i-o-analogwrite

Docs need to make more of a point of that being a requirement… I’ll fix that now.

2 Likes

Here after the full code, I still don’t know what’s wrong…

int REDPin = A0;    // RED pin of the LED to PWM pin **A0**
int GREENPin = D0;  // GREEN pin of the LED to PWM pin **D0**
int BLUEPin = D1;   // BLUE pin of the LED to PWM pin **D1**
int brightness = 255; // Full brightness for an ANODE RGB LED

void setup()
{
  pinMode(REDPin, OUTPUT);     // sets AO pin as output
  pinMode(GREENPin, OUTPUT);     // sets D0 pin as output
  pinMode(BLUEPin, OUTPUT);     // sets D1 pin as output
}

  void loop()
{
  analogWrite(REDPin, brightness);
  delay(500);
  analogWrite(GREENPin, brightness);
  delay(500);
  analogWrite(BLUEPin, brightness);
  delay(500);
  analogWrite(REDPin, 0);
  delay(500);
  analogWrite(GREENPin, 0);
  delay(500);
  analogWrite(BLUEPin, 0);
  delay(500);    
}

Cheers!

Gimme a sec and I’ll wire one up!

That’s really kind, thank you !

A little out of focus but it’s working!

I didn’t change your code, just copied it right into Spark Dev and programmed the Core I had sitting next to me.

http://imgur.com/oCtZZ3P

I just used a 100 ohm for RED, and a jumper for GREEN and BLUE.
RED measures 10mA, and GREEN and BLUE both measure 12mA.

Green is REALLY bright naturally to the eye, so that probably needs to be turned down a bit. Experiment with resistors there until you get the right blend.

The long pin of the RGB LED is Common Anode and I hooked it to 3V3 on the Core.

Hopefully you can figure out what is wrong with yours based on this :wink:

3 Likes

AWESOME it works ! thanks a lot BDub ! I know what was wrong, I actually hooked the common Anode to Ground and not to 3V3… I actually used the same logic that I used for wiring a simple LED. I defenetly learnt something today. Thanks again !

1 Like

Great! Once stuff like this is wired up I like to play, so here’s some fancy rainbow code for ya…

#include "application.h"
#include "math.h"

int REDPin = A0;    // RED pin of the LED to PWM pin **A0**
int GREENPin = D0;  // GREEN pin of the LED to PWM pin **D0**
int BLUEPin = D1;   // BLUE pin of the LED to PWM pin **D1**
uint8_t red = 0;
uint8_t green = 0;
uint8_t blue = 0;
int wheelIdx = 0;

void wheel(uint8_t &red, uint8_t &green, uint8_t &blue, int wheelPos);

void wheel(uint8_t &red, uint8_t &green, uint8_t &blue, int wheelPos) {
   double frequency = 0.0255;
   red = floor(sin(frequency*wheelPos + 0) * 127 + 128);
   green = floor(sin(frequency*wheelPos + 2) * 127 + 128);
   blue = floor(sin(frequency*wheelPos + 4) * 127 + 128);
}

void setup()
{
  pinMode(REDPin, OUTPUT);   // sets AO pin as output
  pinMode(GREENPin, OUTPUT); // sets D0 pin as output
  pinMode(BLUEPin, OUTPUT);  // sets D1 pin as output

  // Serial.begin(9600);
}

void loop()
{
  wheel(red, green, blue, wheelIdx);

  analogWrite(REDPin, red);
  analogWrite(GREENPin, green);
  analogWrite(BLUEPin, blue);
  delay(20);

  // String temp = "RED: "+String(red)+" GREEN: "+String(green)+" BLUE: "+String(blue);
  // Serial.println(temp);

  if (++wheelIdx > 255) wheelIdx = 0;
}
3 Likes