Advice on Red green blue LEDs!!

jonbf

New Member
Hi
Hoping someone out there might be able to point me in the right direction.

I have an 8M picaxe kit that Ive been using to control the brightness of single colour LEDs using the PWMOUT pin and LED driver circuitry. This all works pretty well, and now Id like to do similar with R-G-B (Red green blue LEDs), mixing these three colours. To do this, Ive concluded that I need 3 seperate PWMOUTs so that the brightness of each colour can be controlled independently to set a colour mix??!!

To do this, I figure that I can either use 3 picaxe 8M chips, or use a 'bigger' picaxe chip that might support more PWMOUTs (if such a chip exists).

Am I barking up the right tree with either of these approaches, if so which would be the better approach,,,,,, or am I missing a more obvious solution (such as a way of doing it with just 1 picaxe 8M chip) ??

Please dont get to technical on me though as Im quite old!!

cheers
Jon
 

eclectic

Moderator
Jon. My first quick response is

3 08M chips (you're already familiar with PWM) and

3 separate LED's 1 R, 1 G, 1 B. (or multiples)

You'll get much more light output than with tricolour units.
(Have a quick look at the mcd figures in the datasheets)


e.
 

Tom2000

Senior Member
I tried scanning a common-cathode RGB LED, using a single PWM output that drove the cathode lead. I switched, in turn, the red, green, and blue anode leads for each LED, setting the PWM value each time.

It sorta worked, but I had a bit of flicker.

My test code is shown below.

Good luck!

Tom

Code:
#rem

   16Mcolorsw.bas
   
     Cycles an RGB LED's colors through all values
   
   Common cathode RGB LED hookup:
   
     Cathode to pin2
     Red to pin0
     Green to pin1
     Blue to pin4

#endrem


  symbol redpin = 0
  symbol grnpin = 1
  symbol blupin = 4

  symbol redptr = b4
  symbol grnptr = b5
  symbol bluptr = b6
  symbol redval = b7
  symbol grnval = b8
  symbol bluval = b9
  symbol ctr    = b10
  
  symbol dly    = 1
  
  symbol redinc = 3
  symbol grninc = 1
  symbol bluinc = 2
  
#rem  
  '128 byte sin table (addr 0->127, vals 0->254)
  
  symbol tblsize = 127
  
  eeprom 0,(127,133,139,146,152,158,164,170)
  eeprom 8,(176,181,187,192,198,203,208,212)
  eeprom 16,(217,221,225,229,233,236,239,242)
  eeprom 24,(244,247,249,250,252,253,253,254)
  eeprom 32,(254,254,253,253,252,250,249,247)
  eeprom 40,(244,242,239,236,233,229,225,221)
  eeprom 48,(217,212,208,203,198,192,187,181)
  eeprom 56,(176,170,164,158,152,146,139,133)
  eeprom 64,(127,121,115,108,102,96,90,84)
  eeprom 72,(78,73,67,62,56,51,46,42)
  eeprom 80,(37,33,29,25,21,18,15,12)
  eeprom 88,(10,7,5,4,2,1,1,0)
  eeprom 96,(0,0,1,1,2,4,5,7)
  eeprom 104,(10,12,15,18,21,25,29,33)
  eeprom 112,(37,42,46,51,56,62,67,73)
  eeprom 120,(78,84,90,96,102,108,115,121)
#endrem  
  

Main:

  setfreq m8
  
  dirs = %00010111
  
  high redpin
  high grnpin
  high blupin
  
  redptr = 0
  read redptr,redval
  grnptr = 0
  read grnptr,grnval
  bluptr = 0
  read bluptr,bluval
  ctr = dly
  
  
  do
    
    for redval = 0 to 254
      for grnval = 0 to 254
        for bluval = 0 to 254
    
          pwmout 2,62,redval   'at 8 MHz, 31.5 kHz.  254 = 100%
          low redpin
		    pause 1
		    high redpin
		    
		    pwmout 2,62,grnval
		    low grnpin
		    pause 1
		    high grnpin
		    
		    pwmout 2,62,bluval
		    low blupin
		    pause 1
		    high blupin
		    
		  next bluval
		next grnval
    next redval

  loop
  
end
 

hippy

Technical Support
Staff member
You can control RGB either on all legs simultaneously using two or three PICAXE's or control each in turn using a single PICAXE as per Tom2000.

Intensity depends on how long a colour is on for against how long it's not on and there are many ways to do that. My suggestion would be to grab an RGB LED and an 08M and start playing. You'll learn an awful lot from such a simple setup.

I've not tried it ( and setting the delay values could be challenging ) but I've theorised a very simple scheme could work ...

Code:
Do
  Turn R On : Delay R On Time : Turn R Off
  Turn G On : Delay G On Time : Turn G Off
  Turn B On : Delay B On Time : Turn B Off
  Delay Off Time
Loop
 

Tom2000

Senior Member
I like Hippy's scheme better than mine. You can use the pulsout trick to switch the LEDs. For the on interval, pulse the LED normally using pulsout. For the off interval, send a pulsout to a dummy pin.

That's not my trick, by the way. I learned that one somewhere in this forum.

Have fun!

Tom
 

hippy

Technical Support
Staff member
Thinking on that scheme, the maths may not be that hard nor too computationally intensive. Assume each LED can be on for 30% of the time for 100% brightness, adjust R's to balance intensities when all are on the same amount and -

R On Time = 1 to 101
G On Time = 1 to 101
B On Time = 1 to 101

Off Time = 304 - R On Time - G On Time - B On Time

Coarser resolution with a faster refresh would come from 1 to 51, 154-R-G-B and finer resolution with 1 to 201, 604-R-G-B and so on.

Code:
Do
  w0 = w0 + $100 Min $100 ' b1:b0
  w1 = w1 + $080 Min $100 ' b3:b2
  w2 = w2 + $0C0 Min $100 ' b5:b4
  w3 = 766-b1-b3-b5
  PulsOut RED_LED,b1
  PulsOut GRN_LED,b3
  PulsOut BLU_LED,b5
  PulsOut DUMMY_PIN,w3
Loop
I don't have an RGB but it would be interesting to see if the above does anything 'nice'.
 
Last edited:

jonbf

New Member
Thanks guys.
I think I will try it with 3 picaxe 8M chips, (using one as the principle and the other 2 as slaves .... if you see what I mean). I think I will also have a fiddle to see if i can get it working as per Tom and Hippys single chip approach.
Jon
 

JezWeston

New Member
For the single chip approach, I've found it very useful to overclock the Picaxe to 20 MHz. This just requires an external resonator and the "setfreq em20" command.

This speeds things up enough so that you can happily get twenty-thirty levels of brightness without flicker, just by turning the LEDs on and off directly and using delay times to set duty cycles.
 

wabernat

New Member
I recognize that this thread is ages old, but thought I'd share my experience here, as it seems the most appropriate place. I tried 3-way pwm with a 08M2 chip, but it was unsatisfactory. There is only one dedicated pwm out, and trying to jimmy up my own pwm with for/next loops produced unacceptable flicker. Too much math in too short a span.

Taking the show on the road to a 14M2 chip proved much more fruitful, as it has multiple pwms out. Notwithstanding the promise of "start" subroutines being able to work in lockstep and the claimed ability of PWM to work simultaneously in the background, the fact that I'm constantly changing all three duty parameters means that the LED elements do "fire" sequentially. Nonetheless, moving the cycle parameter down to 1 makes the flicker fast enough to fool the eye satisfactorily.



Code:
; for 14M2 chip
start0: 

if b0 = 127 then let b7 = 1  endif   ; b7 is the red control byte. 0 = increment b0, 1 = decrement b0
if b0 = 0 then let b7 = 0  endif       ; b0 is the red "duty" (high pulse width) parameter 
if b7 = 0 then inc b0 endif 	
if b7 = 1 then dec b0  endif

pwm B.2, b0, 1 		                  ; setting the cycle parameter to 1 almost eliminates flicker

goto start0

start1:
if b2 = 125 then let b8 = 1   endif  ; b8 is the blue control byte. 0 = increment b2, 1 = decrement b2
if b2 = 0 then let b8 = 0  endif       ; b2 is the blue duty parameter
if b8 = 0 then inc b2  endif
if b8 = 1 then dec b2  endif
 
pwm B.4, b2, 1

goto start1
 
start2:
if b4 = 129 then let b9 = 1 endif  ; b9 is the green control byte. 0 = increment b4, 1 = decrement b4
if b4 = 0 then let b9 = 0 endif     ; b4 is the green duty parameter
if b9 = 0 then inc b4  endif
if b9 = 1 then dec b4  endif

pwm C.0, b4, 1
 
goto start2
 

jims

Senior Member
Thinking on that scheme, the maths may not be that hard nor too computationally intensive. Assume each LED can be on for 30% of the time for 100% brightness, adjust R's to balance intensities when all are on the same amount and -

R On Time = 1 to 101
G On Time = 1 to 101
B On Time = 1 to 101

Off Time = 304 - R On Time - G On Time - B On Time

Coarser resolution with a faster refresh would come from 1 to 51, 154-R-G-B and finer resolution with 1 to 201, 604-R-G-B and so on.

Code:
Do
  w0 = w0 + $100 Min $100 ' b1:b0
  w1 = w1 + $080 Min $100 ' b3:b2
  w2 = w2 + $0C0 Min $100 ' b5:b4
  w3 = 766-b1-b3-b5
  PulsOut RED_LED,b1
  PulsOut GRN_LED,b3
  PulsOut BLU_LED,b5
  PulsOut DUMMY_PIN,w3
Loop
I don't have an RGB but it would be interesting to see if the above does anything 'nice'.
Hippy... what does the "DUMMY_PIN" do? Thank you, JimS
 

westaust55

Moderator
The "Dummy_Pin" itself is any spare pin not used for any other purpose.
The intent is to keep the cycle time of the loop constant and using PULSOUT command keeps the overhead for each time interval for R, G, B and delay the same.

Keep in mind that the original post was for an 08M which does not have the PAUSEUS command and PULSOUT achieved the same time delay increments (10 usec steps @ 4 MHz).
 
Top