Author Topic: PWM with RGB LEDs  (Read 6069 times)

isdale

  • Newbie
  • *
  • Posts: 4
PWM with RGB LEDs
« on: January 09, 2013, 02:22:10 am »
I built a bunch of my new Digispark boards and shields tonight.  When I got the RGB shield built I modified the code a bunch and tried to play using PWM to make less intense colors, as well as mixing.  This is something that gets done with these RGB leds in other places (most notably the strips).
Unfortunately while Red and Green are on PWM capable pins, Blue is not.
  Red = pin 0
  Green = pin 1
  Blue = pin 2 -- not PWM Capable

To get around this issue, I can use a cable instead of header pins and have blue connect to pin 4 instead.
Other work arounds?

I did note that PWM doesnt give a lot of variation in color/intensity.  It seems analog values over 50 are pretty much full on for red/green.  Although blue doesnt kick on until value is well over 100.
Mixing the colors will also require a bit of experimentation after fixing blue pin, as the perceived intensity for each color is not the same for the same given analog value.

Anyway - fun night putting shields together!
« Last Edit: January 09, 2013, 02:22:10 am by isdale »

digistump

  • Administrator
  • Hero Member
  • *****
  • Posts: 1465
PWM with RGB LEDs
« Reply #1 on: January 09, 2013, 02:24:54 am »
@isdale this was done to allow for usb communication with the rgb shield - see the DigiRGB sketch for one way to do the blue with softPWM.

If you want to connect blue to P4 instead of P2 - flip the shield over and you\'ll see two solder jumpers, one has a little trace connecting its two pads, the other does\'t - cut the one with the trace and bridge the other with solder this will disconnect blue from p2 and connect it to p4

Bluebie

  • Sr. Member
  • ****
  • Posts: 486
PWM with RGB LEDs
« Reply #2 on: January 09, 2013, 07:08:24 am »
The trouble you\'re having with intensity is that the PWM controls linearly the photon output of the LED elements, but what you want to control is the perceived brightness. We perceive brightness on a curve. Commonly this is referred to as gamma correction in Arduino communities. Here\'s primitive code for applying gamma correction to your colours making them look more linear:

byte gamma_correction(byte input) {
  unsigned int multiplied = input * input;
  return multiplied / 256;
}


The Arduino software (actually avr-gcc) is smart enough to convert that division in to a bitshift operation, so it\'s very fast and effectively a 2.0 gamma correction curve. You can get fancier with software PWMing the hardware PWM values to add extra resolution and get full 8-bit gamma corrected colour. This primitive function causes some loss of data due to integer rounding, but is good enough for many uses.

marcmerlin

  • Newbie
  • *
  • Posts: 15
Re: PWM with RGB LEDs
« Reply #3 on: January 21, 2013, 03:42:15 pm »
@isdale this was done to allow for usb communication with the rgb shield - see the DigiRGB sketch for one way to do the blue with softPWM.

If you want to connect blue to P4 instead of P2 - flip the shield over and you\'ll see two solder jumpers, one has a little trace connecting its two pads, the other does\'t - cut the one with the trace and bridge the other with solder this will disconnect blue from p2 and connect it to p4


I expect this will be an FAQ. I was also wondering "OMG, so silly, why not have used the 3rd PWM pin" :)
Maybe you'd want to paste that on http://digistump.com/wiki/digispark/tutorials/rgb.


Also, you say 'digirgb sketch' with softpwm. There is no link I can find on the rgb tutorial page, and I'm not sure I'm finding that sketch.
Can you post a link?


Thanks,
Marc

digistump

  • Administrator
  • Hero Member
  • *****
  • Posts: 1465
Re: PWM with RGB LEDs
« Reply #4 on: January 21, 2013, 04:51:54 pm »
The wiki has been updated and the examples is located at Examples->DigisparkRGB->DigisparkRGB in the Arduino IDE


The wiki is also open to editing now!


Thanks,
Erik

marcmerlin

  • Newbie
  • *
  • Posts: 15
Re: PWM with RGB LEDs
« Reply #5 on: January 22, 2013, 06:22:11 pm »
got it, thanks for the reply.