Enide! my web project

Pic RGB color generator

Description

PIC RGB is a circuit that generates random RGB colors using a RGB LED and fades between them.

The idea of this thing came from the candle simulator [1] and another project called TinyRGB [2] .
The challenge was to create an algorithm that could fade 3 independent colors at different speeds in the same time interval, using integer math, (of course).

There are two enhanced versions of this project named Pic RGB Power board and Pic RGB Power board with Infrared remote control. Both circuits drive a powerful 3W Prolight RGB LED.

 

Design and Implementation

The selected PIC was the same one of the candle simulator: 12F629 or 12F675 [3]. Both contain 1 KWords of flash and two timers. 12F675 contains an ADC which is not needed.

 

Schematic

Basic schematic includes a voltage regulator 78L05, the PIC with a decoupling capacitor and a few resistors.

picrgb schematic Since each PIC pin can supply a maximum of 25mA of current, and the LED specification indicates a maximum of 25mA, the LED resistors were calculated to provide 20mA of current in each color.

Vred = 2.1V, Vgreen=3.4 and Vblue=3.5V

R=V/I

Rred = 145 Ohm -> 150 Ohm

Rgreen = 80 Ohm -> 82 Ohm

Rblue = 75 Ohm -> 82 Ohm 

 

 

PCB

The PCB and Schematic were created using Eagle from Cadsoft.
Download the PDF with the smd PCB here.
Download the version 3a board with the switch here.
Click on the images below to display the top placement information for the two versions. Note that the PIC pin1 is the lower right one.

PCB components

 

 

 

 

 

 

Software

The software is written in C and consists on a random number generator, an intensity fading function and a triple PWM modulator. The triple PWM  modulator uses just one timer for all the output signals.

All three color components have 256 levels of intensity.

The random number generator is the same from the candle simulator, except that it now returns a char. The previous version returns a single random bit.

The fading function has suffered three evolutions along the process of development, each one being an improvement of the previous version.

  • Version 1: uses a simple step algorithm to determine the increment of each component, from the current color to the final color. Each iteration cycle adds the step value to the current color component value. Since the math is only integer based, there is an error reaching the final component values which makes impossible to show the right color. To solve this, each component step receives a extra 1. Now this extra one causes another problem... it may give a final component value bigger than the expected. So when the last iteration cycle is reached the correct final value is forced, rather than summing the step.
  • Version 2: uses pre-defined colors with the previous step algorithm. It didn't provide good results and was abandoned.
  • Version 3: Calculates the step and the error when reaching the final value. Then it calculates the number of cycles in which the error is going to be distributed (error period). In each cycle it adds the step value and an extra 1 from the error if the cycle number if a multiple of the error period.

 

Version 3 example:

Current color RGB=(10, 200, 50) needs to fade to Final color RGB=(10, 150, 30) in 2 seconds (40 cycles). The formulas are:

  • step=(final-current)/cycles
  • error=final - (step*cycles+current)
  • errorperiod=abs(cycles/error)

 

  Current RGB  Final RGB  Step  Error  Error Period (looks more like a frequency) 
Red   10  10  0  0
Green  200  150  -1  -10  4
Blue  50  30  0  -20  2


What does it mean? It means that Red will not be incremented in 40 cycles at all. It means that Green is going to be decremented 1 in each cycle, because of the step, and an extra 1 every 4 cycles, because of the error. And finally Blue will be decremented every 2 cycles one unit because of the error!

Finally the PWM modulator controls the brightness of the LEDs. There are several ways of creating the PWM signal, this time only one timer was used. To be able to display 256 levels for each color component from 50Hz to 100Hz, the timer must run from 50*256 interrupts per second to 100*256 interrupts per second. There's a counter variable associated with the timer which is incremented every time the timer generates an interrupt. Every time the counter variable overflows from 255 to 0, all 3 components (R,G,B) are turned on. In the following cycles the color components will be turned off when their values match the value of the counter.

 

UPDATE: Version 3a is now available and includes everything from version 3 and a new sequence mode. The sequence is Green, Green/Blue, Blue, Blue/Red, Red, Red/Green and starts over. To switch between the random mode and the sequence mode a button may be connected from GPIO1 (pin 6) to the GND.

Evaluation

After testing and comparing the three versions this is what I was able to conclude:

  • Version 1 looks nice but the final fade cycle is always noticed because it produces a sudden brightness jump.
  • Version 2 is really bad.
  • Version 3 is working great for some time now. The fade is really nice and smooth! There are no noticeable flickering effects or sudden jumps.

Compiling and running with the PWM at 100Hz you'll need to turn on code optimizations in HI-Tech picc otherwise there is not enough time to complete the ISR before another interrupts occurs. This is because the timer generates 100*256 interrupts per second and the PIC is being clocked from the internal 4Mhz oscillator. The time between interrupts is 39us and running at 4Mhz the PIC is able to execute an instruction every 4*1/Fosc = 1us. So it is possible to execute, at most, 39 instructions inside the interrupt service routine.

The resistor values are not critical. In fact you may choose values above or below the theoretical ones that make the light more pleasant to you. I'm using 200 Ohm for Red, 150 Ohm for green and 100 Ohm for blue and the amount of light coming out of the LED is huge and somewhat blueish!

Downloads

Datasheets:


Firmware:

Old versions:

New firmware:

Just connect a TSOP1736 or TSOP1738 to GPIO1 (the button pin) and your're set! Read the README.txt file for a small tutorial on the available remote control keys.

Note: Older versions 1 and 2 of the firmware were created using a circuit with a common cathode LED. If you want to test them with this circuit please change the ON and OFF macros in the C file and recompile. It should be enough.

References

  1. Candle simulator
  2. TinyRGB 
  3. PIC12f675/12F629 datasheet from Microchip