Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] RGB LED circuit help PWM control - beginner

Status
Not open for further replies.

tinkerer73

Member level 3
Member level 3
Joined
Apr 5, 2011
Messages
65
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,288
Activity points
1,892
Hello,

I'm new to the forum, and although I understand all of the basic concepts behind PWM control, and basic circuits using a 555 timer to do this, I'm an absolute beginner when it comes to real circuit designs. I'm trying to educate myself via reading, but I'm looking for help on a specific project that I'm working on at the moment.

I want to backlight a couple of displays with two different channels of RGB LEDs, where they can be set at the desired color(s) and those setting will remain in the controller even with the power removed.

I'm thinking that for simplicity, I'm better off trying to make this with preset mixes of color, and having push buttons to cycle through these, rather than having the RGB mix directly controlled.

I'm looking at using SMD LEDS rather than something like a 5mm superflux, because of space limitations.

Thanks in advance for the help!
 

That smells like a job for a microcontroller, allthough you might not have the resources being a newbie.
In that case you could build three 555 based PWM modulator circuits, one for each RGB channel, then to get your presets have a bank of trim pots in three's, one for each colour, then switch in each bank with a 3 pole (I think 4 pols are available) toggle switch.

If 4 presets is all you need you could use a rotary switch like one of these, but they are a sod to suss out the connections:

--|CK1051|SWITCH, 3 POLE 4 POS | CPC
 

Thanks dr pepper. I know I could do this the "manual" way, as you suggest, but it's the microcontroller route that I'm looking for. I actually considered going the route you suggested, but using rotary switches with a higher number of positions to allow more combinations, and using relays to cover the 3 colors. Ideally, I just want a couple of small push buttons to scroll through the presets.

I also understand I'm diving in deep, but it's the way I generally work. I tend to learn fast like this. I have a good understanding of basic electronics, and software, but I've never dealt with a microcontroller before.

I'm hoping someone can point me in the right direction here.
 

In that case I'd be looking at a pic 16f675 8 pin microcontroller, or a 16f628 if 6 i/o pins are not enough.
You can write pwm software in assy its not hard, if you go onto microchips database you'll probably find some application notes on pwm examples.
I recently built a time clock using aircraft instruments, the volatge for each instrument (hours, mins and secs) came from some assembler pwm code, refresh rate was only 30hz because of all the other stuff the pic did bit it works well.
You can store preset colour patterns in the pic's eeprom its non volatile, and as you say have a couple of buttons for up and down, 3 buttons would be better as you can use the third as a program button to set up the current colour profile.
Generating colours using pwm is not hard, structuring the rest might be a task if your a newbie on pic micro, but doable if your that way out.
 
Thanks again! This is a good place to start. I'll check out that database. I'm sure I can handle the coding with some examples.

Since this will have two "channels" of RGB (basically my displays will have two colors), would it be best to make two separate circuits sharing only a power supply, or integrate them all through one PIC?
 

2 pics would be my choice, as there is less programming, the same software can be used twice, unless you want a common display or something.
Some pics have built in pwm, I've never used it, I think you only get multi channels in the high end ones, its a trivial matter to implement is software.
 
OK, I've been looking at these, and I'm not having any luck finding the 16F675, but I've found various versions of the 16F628. Looks like they are available in 4 or 20 MHz, with 1, 3, or 8 timers, and in 3.5Kb or 2Kb memory. While I'm tempted to go "more is better", I'm guessing any of them will do for what I need. I'm leaning toward the 16F628-20 /P: https://ww1.microchip.com/downloads/en/DeviceDoc/40300c.pdf

Is there any reason I should consider a different model?
 
Last edited:

I can not let you have the code because it's part of a commercial product but I can tell you it is possible to get at least 10 channels of PWM LED control out of a single 16F628A with nothing more than some current limiting resistors for the LEDs and a capacitor across the PIC supply pins. You could use the other 6 available pins for your level/combination control inputs.

Brian.
 
Brian, that's helpful info.

Can either of you give me any points about selecting one of the variations of the 16F628 for this application?
 

You got me on that one, I thought the '628 only had 3 timers, tmr0,1 and 2, I never heard of a 8 timer variant, do you know the suffix?

Apologies I got mixed up a little I said 16f675, I meant 12f675, I've used this and the 676 which is 14 pins a few times, its a good chip and has the advantage of an on board a to d.

I'd go for the 20mc's version '628, but start of running it at 4mhz, if you run stuff on vero or breadboard at 20mc's things sometime misbehave.

A 3 timer version would be fine, I use the timer 0 as a structured interrupt so I know certain routines within the software are called at exact time intervals.
A lot of folks dont bother with interrupts bacause they dont understand them, all my software uses them, code is much more organised and changeable using the structure.
If I remember I'll fetch my memory stick with pic projects on it, you can have the pwm code, its not fantastic but its in programs that control servo's, operate meters for an analogue clock and now its in use controlling temperatures for a extrusion production system.
 
I never heard of a 8 timer variant, do you know the suffix?
According to the specs listed on Mouser's website, there are a bunch that have 8 timers. List here. It looks like the E/SO, I/P, I/SO, and I/SS all have 8 timers. However, I didn't find anything but the 628A on Microchip's site. All of the 8 timer units listed on Mouser are missing the A suffix.
 

The 628A definitely has 3 timers in hardware and as far as I know there are only two electrically different versions, the normal and 'L' (Low voltage) ones, they all clock at up to 20MHz and have an internal 4MHz clock.
Although hardware PWM uses more than one hardware timer, there is no reason why you need more than one if you generate PWM in software.

Brian.
 
I think I'll go ahead and order a few 628A-I/P and get started on a test unit. Meanwhile I'll research some code. I'm sure I'll have a bunch more questions once I get the thing going.

Thanks for the help so far.
 

Heres the code I wrote as promised, this is just the PWM bit cut and pasted, the output is on portc, bit4, you can change that to what you like:

;------------------------------ pulse width generation code ----------------------------------
;
;--------------------------- seconds pwm analogue out ----------------------------------------
;
;pulse actual output set point, only used in main program
;outpulse temporary high count
;outpulselo temporary low count
;
pwm movf outpulse,w
;pulse width modulator
btfss status,z
goto out1
;
bcf portc,4 ;low duration of outpulse pulse
movf outpulselo,w
btfss status,z
goto outlo1
;
movf pulse,w ;reload low and high duration counter
sublw d'255'
movwf outpulselo
movf pulse,w
movwf outpulse
return
;
outlo1 decf outpulselo,f
return
;
out1 bsf portc,4 ;set output bit and decrement outpulse counter
decf outpulse,f
return
;

Pulse is the actual pwm value you want, 0 for off and 255 for on, outpulse and outpulselo are just temporary storage, you need to define a variable for these in the assembler.
The base frequency is 1/256th of the interval you call the routine, in otherwords if you call the routine 10,000 times per second, the routine will output a pwm squarewave at 40hz.
You can write this into a program that doesnt use interrupts, but as I said it was specifically written for asn interrupt based programm, the good thing about it is that the processor can dissapear off and do other stuff while the output is running.
 
Thanks. That's very simply code. It's been a long time since I've coded anything (used to write computer programs in assembly over 10 years ago as a hobby), but I'm sure it'll come back very quickly.

One question about the 16F628 controllers: will I be able to crank up the PWM frequency in the 1-2Mhz range? I want to run it high so the PWM is not perceived and it looks smooth. You mentioned 40hz, and I'm sure it's just an example, but I wanted to make sure the processor will be able to do what I want.
 

1 to 2 mhz is way over the top, 30hz is the limit of human persistance of vision, tv's operate at 50hz.
You could get the code to run at 50hz without much trouble, maybe 100hz if you went for 20mhz clock, if you reduce the resolution down to 7 bit 0 to 128 then it'd run twice as fast.
I'd reccomend starting at 50hz base frequency.
 

Oops! I meant to put 1-2 Khz, not Mhz. LOL. Big mistake.

However, I personally can see the PWM on most products, and it drives me nuts- LED clocks, fluorescent lights, CRT televisions, etc. Maybe I'll shoot for 100hz, but I really want it to appear smooth and not modulated.
 

OK, as promised, I have more questions:

1) I've been looking into the PIC units with PWM built in, and on Micro's website, their example PWM code says it runs at 78kHz, but the hex value they have is 3f, which is 63, so I'm wondering if the 78.12 is a typo, or if I'm missing something.

I've decided to go with RGBYW, rather than simply RGB. I figure a pure white and pure yellow rather than mixed will be a nice option, and these will allow me to run higher duty cycles for most colors, decreasing perceived modulation. I know this makes things a tiny bit more complicated, but it shouldn't be a big deal. Just a matter of more lookup tables.

2) I'm also going to have a few separately switched units running off this, which will obviously receive control instructions from the main unit. I was going to use the 12F675. If I understand these correctly, that will still leave me enough outputs to control RGBYW, correct? As I figure it, 1 I/O will be used to connect to the main board for directions on which color to use, and the other 5 will be used for the 5 LED channels. Let me know if I'm missing something here.
 

Cant really answer 1 as I dont get the question, and I've never used microchips on board pwm, is it single or multi channel?
78kc sounds about right though for a special function pwm.
The '675 doesnt have pwm, unless you've found another special.

Question 2 not sure on your overall design, but maybe the '675 will do the job, the only consideration is that i/o GP3 on the '675 is an input only, so you can have a max of only 5 outputs, if the extra pin is going to be an input then theres no problem.
 
Can you post a block diagram of your proposed system.

My first guess based on what you have said is you should use the '675 (why not another 628A?) as a serial transmitter and use the USART in one or more '628A as the receiver. I use a similar principle to address many PIC devices on a long serial bus over more than 100 metres distance. You can invent a very simple protocol to with an address field and a command field so you can control as many slave PWM generators as you want.

As for the actual PWM, you can do it in hardware or software. for the relatively simple task of controlling LEDs, I would suggest software is easy, particularly when you want several independant PWM output channels.

Brian.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top