RGB led color control

Go To Last Post
22 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello guys,
I want to mix the colour of my rbg led and I have some question; I need to create 3 pwm that you can see in the picture.
Which is the best way to create 3 phase correct pwm signal on arduino pro mini using registers and timers?

Attachment(s): 

Last Edited: Sun. Jan 21, 2018 - 06:00 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Is this a homework assignment?

 

You can start by getting PWM to work on 1 channel.

 

Tip:

More than 1 led may be on at the same time.

Paul van der Hoeven.
Bunch of old projects with AVR's:
http://www.hoevendesign.com

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you for your reply and sorry for my horrible english.

No it isn't, it's a personal project to play around with atmega328p register.
I already know how create a PWM to work on 1 channel: I can use inverted or non inverted mode, or a phase correct one.
I read chapter 15 to 18 of the atmega and I saw that there are some different possibilities to create my pwm; and I'm doubtful about which is the best way to do that in my project .

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

LW74 wrote:
Which is the best way to create 3 phase correct pwm signal on arduino pro mini using registers and timers?

You say you want the "best" (sic) way - and yet you seem pretty prescriptive about it!

 

Have you checked-out the many previous discussions here: http://www.avrfreaks.net/comment...

 

It can be done in software: http://www.avrfreaks.net/comment...

 

Last Edited: Sun. Jan 21, 2018 - 06:56 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Welcome to AVR Freaks!

 

There are several strategies. But, first, some details.

 

One of the first details to understand is that the PWM signals do not have to be synchronized. They can be, of course, but do not have to be. A corollary to this is that the PWMs do not even have to have the same timing source. 

 

The second detail is that the PWM frequency does not make very much difference. As long as the repetition frequency of the PWM signal is greater than 50-60Hz, your eye cannot distinguish it from being on continuously. This is a consequence of an effect called "persistence of vision". It is the same effect that makes moving pictures possible. Most designers choose a minimum repetition frequency of 100Hz or so (max period = 10ms or so). PWM frequency is NOT the same as timer clock frequency. An 8 bit timer takes 256 counts for a full period, so, for a minimum of 100Hz, the 8-bit timer's clock would need to be at least 25600Hz = 25.6KHz.

 

The first strategy is what I would call "mostly software". In this strategy, you use SOMETHING (typically a timer) to generate "ticks" for a software counter. You do the comparisons in software and use those comparisons to turn the LEDs on and off. The ticks would need to be at a frequency of at least 25KHz or so (with an 8-bit unsigned integer counter variable). There should not be much problem even going quite a bit faster.

 

The second strategy is "mostly hardware". In this, you use one or more timers. You use the COMPARE function for each timer (some timers have more than one compare function). In virtually every AVR, you can tie these compare functions directly to port pins to generate PWM signals. You DO need software to initialize the timer(s) but once this is accomplished, it just runs. No interrupts, no intervention by software. Its all in the background.

 

So, to implement the second strategy, which I think is what you are really asking about, look at the COMPARE function for any AVR timer. It is also useful to open the spec sheet for your micro and to a text search for PWM. Amazing what you will find. And, as noted in the details at the start of this post, if your timer does not have enough compare registers, use another timer. They do not have to be synchronized.

 

Now, for a hardware generated variable color control, you simply vary the contents of the compare registers according to the color pattern you want. THIS may be the hardest part, for you.

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

Last Edited: Mon. Jan 22, 2018 - 01:08 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

A completely different path is the WS2812

WS2812 is a RGB led with build in electonics which can be controlled with a uC over a serial interface.

You can buy these in led strips, loose, dot-matix like, round circular PCB's and other forms.

The color of eachs led can be controlled individually.

 

For Ideas, check out some youtube video's.

https://www.youtube.com/results?...

 

You can find a library to controll these on a site like github:

https://github.com/search?utf8=%...

 

Paul van der Hoeven.
Bunch of old projects with AVR's:
http://www.hoevendesign.com

Last Edited: Mon. Jan 22, 2018 - 02:18 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ka7ehk wrote:
As long as the repetition frequency of the PWM signal is greater than 50-60Hz, your eye cannot distinguish it from being on continuously

Some people can still perceive flicker at 60Hz, hence:

Most designers choose a minimum repetition frequency of 100Hz or so

 

ka7ehk wrote:
 The first strategy is what I would call "mostly software".

That's the one in my 2nd link in #4

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Paulvdh wrote:
WS2812 is a RGB led with build in electonics ... You can buy these in led strips, loose, dot-matix like, round circular PCB's and other forms.

Adafruit use the brand name, "NeoPixels"

 

Note that they also have RGBW (RGB + White) versions ...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I just typed "arduino rgb led" into Google. Very top hit was:

 

https://learn.adafruit.com/adafr...

 

To be honest I would have been astonished if I hadn't found multiple examples. Naturally anything from Adafruit is bound to be good!

 

(Google tells me that was one of 735,000 results ;-)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Just searching for "RGB LED" on this one site gives 619 hits

 

http://www.avrfreaks.net/search/site/rgb%20led?filter_by=9631

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I already read and checked-out those post but they didn't help.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It isn't clear from your first post what you want to do.  Do you want to have:  

 

  1. Red LED turned on for a short time, then Green, then Blue and each of the LED on times for each can vary and are independent of the other LEDs 
  2. The Red LED is given a time slice and the on time is a proportion of that time slice, then same for Green and then Blue
  3. Something else

 

Is the On/Off a control signal to enable the output or an output used for something else?

 

I think you need to define what you want and that will then help you in using the timers.

 

David

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

LW74 wrote:
I already read and checked-out those post but they didn't help.

Why didn't they help? What further/different information do you need?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Like Andy I'm kind of puzzled how the existing threads that showing working solutions do not help but I think you might want to start with just a single red (or any other simple color) LED and a single PWM output channel and just see if you can vary it's brightness. Driving of RGB LEDs is then simply a matter of doing that to three different (R, G and B) LEDs at the same time but if you understand how to bright/dim a single one then making the leap to doing it for 3 at the same time should be fairly trivial.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank for your reply, but I already search on Google...Probably you didn't read my question carefully I'm not interest to use function like digitalWrite (),I'd like to work on timers and interrupts of the atmega328p.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

LW74 wrote:
y I'm not interest to use function like digitalWrite (),I'd like to work on timers and interrupts of the atmega328p.

Arduino does not force you to use its functions, there is nothing stopping you from interfacing directly with the hardware!

T0 is used for millis(), if that function is important, then don't use t0 or reconfigure carefully for pwm output.

T1 / T2 are both available to use anyway you wish if you don't use the PWM() functions in Arduino, configure directly if you choose.

 

Jim

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What are the ON(OFF) times for the three LEDs?

What are the tolerances on the ON and OFF times for the LEDs?

How exactly do the OFF of one need to correspond to the ON of the next?

And what is the total period?

 

David (aka frog_jr)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

LW74 wrote:
Which is the best way to create 3 phase correct pwm signal

"The best" is the one that satisfies your requirements
----------
Your requirements:
Arduino pro mini ATmega328P (#1)
Phase correct pwm, 3 ch R, G, B (#1)
Invert/non-invert (#3)
8 bit color depth 0-255 is fine for play around (#3, #5)
Min 100 Hz PWM, min 256kHz timer clock (#5)
----------
1) choose 3 pwm channels out of 6 available in ATmega328P
2) Set clock value > 250 kHz for timers
3) Set phase correct pwm mode top 0xff for timers
4) play with pwm values (OCR?? registers) set/clear/increment/decrement... to see various color combinations.

Majid

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Since the WS2812 (or NeoPixel) seems to be the most popular RGB programmable LED, it is possible that you are misinterpreting the data sheet for this part.  PWM will not produce the three sections shown on your attached image.   The WS2812 uses a three-stage waveform that looks like the image to send RGB data from the microcontroller to the LED unit.  These three stages are very short so that long strings of LEDs can appear to change color nearly instantly.  The first stage is 0.45 microseconds long and is always logic high. The next stage is 0.4 microseconds long and is either logic 0 or 1, because it is the data bit. The third stage is 0.4 microseconds long and is always logic 0.   There are 24 of these three-stage sections needed to be sent to the Neopixel to make an individual color.  Each color is one of 256 brightness levels of red, one of 256 levels of green, and one of 256 levels of blue.  This speed level of programming has to be done in assembler for the AVR using precise op-code timing.  NeoPixels can be programmed on AVRs running as slow as 8MHz.  See the Adafruit library on NeoPixels for all the gory details.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

There is no indication that the OP is using WS2812.

 

It was mentioned by Paulvdh in #6 as,  "A completely different path"

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I read your question carefully and asked for clarity which, as yet, you haven't supplied. 

 

David

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Consider a periodic timer that provides a thread of control and time base.

The LEDs are tied to port bits 0, 1, & 2.

And your three LEDs/colors intensity as 8-bit numbers.

Create a counter for each value.

Compare the value to the counter and turn the LED on or off depending on the relative phase.

 

unsigned char red, green, blue, tick;    // when red, green or blue are changed anywhere, the ISR will change LEDs. 

 

isr()                                    // set timer to something perhaps 15,360 (256 * 60) or 250 KHz if you prefer

{

    tick = tick + 1;                // keep a sense of time/phase

 

    if (red < tick)

        PORT.SET = 1;

    else

        PORT.CLR = 1;

 

    if (green < tick)

        PORT.SET = 2;

    else

        PORT.CLR = 2;

 

    if (blue < tick)

        PORT.SET = 4;

    else

        PORT.CLR = 4;

}

 

In this example, the results will synchronize around tick.

 

The timer can be more complex to accommodate your original timing diagram.

Check out: "Split from: Interrupt Controlled Multi (8) Channel PWM 60Hz" post for an example of a more complete solution.

 

Last Edited: Thu. Feb 1, 2018 - 05:58 PM