Menu

all about electronic and microcontrollers

Sunday, October 17, 2010

Lesson nr.10-Software PWM & RGB LED

Hardware setup:
This time we will implement software PWM mode, to control the light intensity of an RGB Led (Red Green Blue, which represents a pixel) .PORT RA0~RA2 and PORT RB0~RB2 will be used for this experiment.
Port B is used, to see more easier transitions between RBG colors.



Circuit Diagram:
For those who want to build it on their own breadboard or other platform, here is the electronic scheme built in Eagle Cad, free version:



Software:
Here is the C program written for MikroC PRO for PIC 2010 (version v4.15).
/*
'*******************************************************************************
'  Lesson nr.10:
'          Software PWM & RGB LED
'  Written by:
'          Aureliu Raducu Macovei, 2010.
'  Description:
'          In this experiment we will show how to implement software PWM to
'          control an RBG LED. PORT RA0~RA2 and PORT RB0~RB2 will be used for this experiment.
'  Test configuration:
'    MCU:                        PIC16F628A
'    Test.Board:                 WB-106 Breadboard 2420 dots
'    SW:                         MikroC PRO for PIC 2010 (version v4.15)
'  Configuration Word
'    Oscillator:                 INTOSC:I/O on RA.6, I/O on RA.7
'    Watchdog Timer:             OFF
'    Power up Timer:             Disabled
'    Master Clear Enable:        Enabled
'    Browun Out Detect:          Enabled
'    Low Voltage Program:        Disabled
'    Data EE Read Protect:       Disabled
'    Code Protect:               OFF
'*******************************************************************************
*/
// LED RGB module connections
sbit RED1 at RA0_bit;
sbit GREEN1 at RA1_bit;
sbit BLUE1 at RA2_bit;
sbit RED2 at RB0_bit;
sbit GREEN2 at RB1_bit;
sbit BLUE2 at RB2_bit;

sbit RED1_Direction at TRISA0_bit;
sbit GREEN1_Direction at TRISA1_bit;
sbit BLUE1_Direction at TRISA2_bit;
sbit RED2_Direction at TRISB0_bit;
sbit GREEN2_Direction at TRISB1_bit;
sbit BLUE2_Direction at TRISB2_bit;
// End LED RGB module connections

int i = 0 ,PWN_COUNTER = 256;

void PWM_FADE_0_TO_1(void);
void PWM_FADE_1_TO_0(void);
void Delay(int num);

void main(void)
{
 CMCON  |= 7;                     //  Comparator is Off
 TRISA = 0;
 PORTA = 0;
 TRISB = 0;
 PORTB = 0;

 while(1){
          PWM_FADE_0_TO_1();
          PWM_FADE_1_TO_0();
          }
}

void PWM_FADE_0_TO_1(void)        // Duty cycle -  0%  to  100%
{
 for (i = 0; i < PWN_COUNTER; i++)
 {
  RED1 = 0;
  RED2 = 0;
  Delay(PWN_COUNTER - i);
  RED1 = 1;
  RED2 = 1;
  Delay(i);
  }
 for (i = 0; i < PWN_COUNTER; i++)
 {
  GREEN1 = 0;
  GREEN2 = 0;
  Delay(PWN_COUNTER - i);
  GREEN1 = 1;
  GREEN2 = 1;
  Delay(i);
  }
 for (i = 0; i < PWN_COUNTER; i++)
 {
  BLUE1 = 0;
  BLUE2 = 0;
  Delay(PWN_COUNTER - i);
  BLUE1 = 1;
  BLUE2 = 1;
  Delay(i);
  }
}

void PWM_FADE_1_TO_0(void)         // Duty cycle -  100% to 0%
{
 for (i = 0; i < PWN_COUNTER; i++)
 {
  RED1 = 1;
  RED2 = 1;
  Delay(PWN_COUNTER - i);
  RED1 = 0;
  RED2 = 0;
  Delay(i);
  }
 for (i = 0; i < PWN_COUNTER; i++)
 {
  GREEN1 = 1;
  GREEN2 = 1;
  Delay(PWN_COUNTER - i);
  GREEN1 = 0;
  GREEN2 = 0;
  Delay(i);
  }
 for (i = 0; i < PWN_COUNTER; i++)
 {
  BLUE1 = 1;
  BLUE2 = 1;
  Delay(PWN_COUNTER - i);
  BLUE1 = 0;
  BLUE2 = 0;
  Delay(i);
  }
}

void Delay(int num)
{
 while(num>0)
 num--;
}

22 comments:

  1. Hi!
    Sorry... but i didn't get the code... it's the first time i try to program a PIC.

    How do you do the fade effect? It seems that assign 0 to a color, wait (PWN_COUNTER - i) time units, and then assign 1.

    You don't use the PWM port, do you?
    thank you

    ReplyDelete
  2. oo... i think i got it! you simulate the pwm with the duty cycle... ok!

    Is not necessary a power driver for leds?

    ReplyDelete
  3. Hello Daniele,
    As can be seen from the scheme, port RB3 is not used (which is hardware pwm), everything is done by software, which means "i" takes value betwen 0 and 255.
    I thought so, betwen 0 and 1 logic, which means a duty cycle betwen 0% to 100%.
    No, is not necessary a power driver for LEDs. The microcontroller provides necessary current to drive all LEDs.

    ReplyDelete
  4. ok... thank you.
    I would like to simulate sunset and sunrise with a rgb strip led and this can be very useful! with the software PWM i can use several pieces of the strip independently.

    How can i know, if i need a power driver, which to choose? my strip is about of 30 high power LEDs, and now is powered by 12V - 2.0A

    thank you again

    ReplyDelete
  5. Output ports, as I know, provides up to 100mA, this means you can not control the strings of 30 LEDs, directly.
    Definitely, in this case, you need a power driver to handle 30 LED strip.
    You can use the configuration that you already posses. That means, you need to control current power driver (of 12V - 2.0A ), by microcontroller.
    The scheme below shows you how to do this: Picture Here

    ReplyDelete
    Replies
    1. I wanted to know if programming a 16F690 with a HEX written for a 16F84, the scheme is running the loop or nothing happens even if i connected the correct pins. How do you change a program and how it fits to the pic I want to use?
      I look forward information !!

      Delete
  6. so...my power provider is already a "driver", and i can connect all the components without buying other driver. Is it right?

    Thank you, your picture will be very useful!

    ReplyDelete
  7. Yes indeed, your power driver is more than enough for your application.
    With pleasure.

    ReplyDelete
  8. Hello. I have another question.
    Your function Delay() chooses the duty cycle frequency. But it depends also by the clock of the pic I guess. So which clock should I use?

    Is there a way to write a similar function that waits as many seconds as i want?
    Thank you again

    ReplyDelete
  9. Hi,
    I finally managed to write my program. I have a question for the scheme.
    The scheme shown in your picture, is good also for a strip of 10 leds instead of 30? Or the transistor tip122 is too strong and may burn the leds?
    Sorry but i have no idea of how make calculations.
    Thank you

    ReplyDelete
  10. Sorry for the delayed response, I have not been around here a while.
    I'll answer your questions in the order you wrote:
    1.In the current experiment we used the internal clock of 4MHz.
    2.Everything possible, but I have not tried this yet, I focused only on the project in question.
    3.You can try to put every three LEDs on each port (connected in parallel, each with its resistance), but I don't think you'll have a satisfactory intensity of the lights, the best choice, in this case, is to use transistors. The tip122 is really strong, but i don't see how to burn your Led's if you use current limiting resistors through them enough to work within normal parameters.

    Example calculation:
    1 Led to run into a normal parameter 25mA maximum is needed at the minimum voltage of 1.8-2v opening, thus resulting:
    At a current of 15mA per LED = 15mA * 30 Led's = 450mA total consumption. You need a transistor, wich trough collector can lead 450mA = you can use a BD139 wich has the collector current 1,5A , more than enough to carry all you Led's, at the resonable heat.
    I'm at your disposal for further questions.

    Kind Regards,
    ducu

    ReplyDelete
  11. Hello,

    Your lesson it's fine. I'm a beginner with pwm and pic but with you I have modified your code for a 10 channel chaser in two minute but I have another problem. It's possible to write your code for Pascal, could you maybe a little example?

    Thank you

    (sorry for my bad english)

    ReplyDelete
  12. Sorry Admin, I can't help you, because i don't possess Pascal knowledge.
    If you are a beginer, why you don't try to understand C language?

    ReplyDelete
  13. This lesson is very interesting. I'm a newbie for PIC C. But I want to modify this code control 8 LEDs connected to PORTB, with a small delay each LED. If is it possible to do that could you give a example?

    Thank you

    (sorry for my bad English)

    ReplyDelete
    Replies
    1. Hi Anonymous,
      what type of led, you want to use.

      Delete
  14. Suppose i wanna hook up 2 rgb instead of 1 in porta. Then 2 rgb leds in portb. all common cathode. Il connect each pair 's color in parallel. Do i still need a transistor then? If yes what type? npn or pnp? and how many? 1 per color each pair? Like 1 for red, 1 for green, 1 for blue. So all in all 6 transistors? Im sorry I'm not really knowledgeable in circuits behavior. I just wanna build this as an accessory. Thanks

    ReplyDelete
    Replies
    1. Hi Hannah,
      In this case you don't need an transistor.
      You must change the resistors value ( R1-R6) from 125 to 200 ohms and should shine enough to enjoy the project.

      Delete
  15. What if I want to use in your project a PIC16F690?

    ReplyDelete
  16. I too would like to use the PIC16F690 because I do not want to keep him locked up in a drawer !! I see so many schemes rgb and other circuits with the tried and tested 16f84 is fine but I wanted to know if you are planning a 16F690 with a HEX written for a 16F84, the scheme performs the loop or nothing happens even if i connected the correct pins. How do you change a program and how it fits to the pic I want to use?
    I look forward information !!

    If you do not understand something, or if you make some aplication Helped by this blog, let me know.

    ReplyDelete
    Replies
    1. Hello,
      You can not use a ".hex" file which is specifically compiled for PIC16F84, on a different microcontroller model.
      Yes, can be changed the current program to fit for pic16f690.
      You can do in this manner:
      Open Mikroc Pro for Pic, click on new project, select the desired microcontroller, copy / past all this program and with a little adjustment you can succeed.

      Delete
  17. Interesting lesson, after reading your code i get the idea on how fading is made,thanks

    ReplyDelete
  18. I adapted your code for use with 3 analog optocoupler to each independently work as audio limiter control for commercial product ,it take same time but was greatly successful thank u for the very useful lessons

    ReplyDelete

If you do not understand something, or if you make some aplication helped by this blog, let me know.