Microcontrollers to control RGB LEDs

Started by dbp512, April 18, 2016, 05:00:39 PM

Previous topic - Next topic

dbp512

So I recently decided to buy a PICkit and flash my own TAPLFO chips. The purpose of flashing my own is to be able to create other things as well. I thought I'd start off with something simple, and use 3 PWM channels to control an RGB light, so I can make any color I want. I'd like to expand on this by using a CV in to blend between 2 colors, to serve as a cool looking visual indicator for a pot. I figured this wouldn't be a terribly difficult first project. 
 
Although I don't really know what microcontrollers to use, or any reasons as to why I'd choose one. That being said, I looked for a chip that had 3 PWM output and 3 ADC on different pins, so I could have independent control over each color channel. I figured since I don't know what pulses would work well, this would allow me to experiment; that led me to a PIC16F1455. I'll only need one of these to serve as a test platform, but to box up I'd want to use the smallest chip I could. I found a PIC12F1572, which has 3 PWM out and an ADC. Would both of these chips be good for this application? I'm aware they're different families so the coding would have to change, but PWM frequency should carry over pretty closely. 
 
Also, does anyone have any resources about programming these bad boys? The stickied threads are a little outdated and google, like mouser, has so much it gets daunting.
Dave's not here, man

On some nights I still believe that a car with the gas needle on empty can run about fifty more miles if you have the right music very loud on the radio
- Hunter S. Thompson

Benoi31

#1
If this is your first ever project with PICs, I am not sure that it is the easiest one you can pick!

I am not an expert, but PWM seem rather complex to create and make... I know it sounds easy "in your head", but when coding it, even really simple stuff can become incredibly difficult because of the "computer way of thinking" of the PICs (well, in a way there are computers!).

A good start for me would be to blink a LED (I know, boring, but easy)
Then maybe relay bypass (easy as well)

I use MPLabX with the XC8 compiler from microchip to programm my PICs, works really well, and there are a lot of ressources on the web about these programs.
It is C or C++ language so quite easy to code as well (and useful for other stuff)

I wrote a tutorial about how to start with PICs, with the relay bypass as an example, using a PIC12F675 :
http://www.coda-effects.com/2016/04/relay-bypass-conception-and-relay.html

samhay

The pic12F1xxx chips are pretty neat. They are new though, so you won't find many projects written specifically for them, and I don't think you can flash then with a PICkit 2 - you need the newer '3' version. However, the code here is written for similar chips, and might be some help.

http://www.diystompboxes.com/smfforum/index.php?topic=109015.20

The 12F1572 has 3 PWM outputs and 4 ADC inputs, but some of these are on the same pin
As a general rule you will have 3 pins on the pic that you can't use for anything fun - the two supply pins and the MCLR (which you can use as a switch input). As such, you can have 3 PWM and 2 ADC or vice versa. If you want 3 each, you will need to go to a larger (more pins) chip.



I'm a refugee of the great dropbox purge of '17.
Project details (schematics, layouts, etc) are slowly being added here: http://samdump.wordpress.com

waltk

#3
You can also do this with a DigiSpark board.  The DigiSpark is a very small, minimalist arduino board based on an ATTINY85.  You can find them on Ebay for less than $2.00.
You don't even a standalone programmer for these as they plug right in to a USB port.

Here's a link to an RGB fader project for it: http://www.instructables.com/id/Digispark-RGB-LED-Fader/

One other tip; RGB LEDs are available in both common-cathode and common-anode versions.  Make sure you know which ones you have when wiring/coding.

cloudscapes

Quote from: Benoi31 on April 19, 2016, 03:59:38 AM
If this is your first ever project with PICs, I am not sure that it is the easiest one you can pick!

I am not an expert, but PWM seem rather complex to create and make... I know it sounds easy "in your head", but when coding it, even really simple stuff can become incredibly difficult because of the "computer way of thinking" of the PICs (well, in a way there are computers!).

I'm not sure for PIC16's, but for PIC32s, getting PWM working is little more than a couple lines of code, using a library. Literally this:

pwm_period1 = PWM_Init(5000, 1, 0, 2);
PWM_Set_Duty(pwm_period1/2, 1);
PWM_Start(1);


And on another IDE I used to use, I think it was only two lines. But yes if you're doing everything in assembly, it's very difficult. But why would you when there are a ton of libraries available?
~~~~~~~~~~~~~~~~~~~~~~
{DIY blog}
{www.dronecloud.org}

slacker

#5
Quote from: samhay on April 19, 2016, 04:16:14 AM
I don't think you can flash then with a PICkit 2 -

If you're interested you can do the 12F18xx and some of the 12F15xx,  not the 1572 unfortunately, using V1.21 of the PICkit2 software from here http://www.microchip.com/forums/m638607.aspx, that's not me on there by the way. You have to use the stand alone software though you can't do it directly from MPLAB/MPLABx.

ElectricDruid

I use PWM in assembly all the time, and it's not too difficult. You have to set up the registers right, but once you've done that, it's easy. My first-ever PIC project was a LFO with PWM output, so it can't be that hard.

I'd second what Samhay said: the 12F1572 is a great chip, but you need to look at the "8-PIN ALLOCATION TABLE" on pg3 of the datasheet. This shows you what functions are on each pin. If all the functions you need are on the same pin and can't be moved anywhere else, you're out of luck. In this case, like Samhay said, you're short by one pin, since PWM3 uses one of your ADC channels and can't be moved elsewhere. Your other suggestion (16F1455) only has two PWM channels. Still, there must be lots of 14-pin 16F1xxx chips you could use. Give me a mo, and I'll have a look.

HTH,
Tom

ElectricDruid

PIC 16F1825 would be a possibility. Four PWM channels and enough ADCs to cover all your needs.

HTH,
Tom

dbp512

Thanks for the responses, I'll take a look at the other chips. I made sure to check the allotment tables, and for there 12F1572 I was planning on only using a single ADC. I only wanted one chip that had 3 ADC and 3 PWM to serve as a test platform, but that can be as large as it needs to be. I wanted an 8 pin chip to blend between two predetermined colors, because it will look aesthetic. Although I might opt for another chip that has better documentation. If PWM will be harder than I might imagine, I don't want to make things any more difficult. 
 
As for a programmer, I'll most likely end up with a PICkit3, since thats what mouser sells. I know that won't make much difference if I'm not using there 1572, but it might make a difference for other chips. I'm not sure what the rules are about these.
Dave's not here, man

On some nights I still believe that a car with the gas needle on empty can run about fifty more miles if you have the right music very loud on the radio
- Hunter S. Thompson

ElectricDruid

Fair enough. Once you've got some code and colours worked out, 12F1572 will serve your purposes well then, I'd have thought.

Tom

dbp512

So it looks like the 12F1572 is a pretty good choice for RGB light control. Good enough for microchip to promote it for such purpose:
http://www.microchip.com/promo/rgb-badge-color-mixing-demo
There's a ton of information and source code available. I'm still looking through it, but it seems as if I'll be able to find everything I need to get it working. One thing I realized, it has 16 bit PWM, while the 16F1455 is 10 bit. I'm not sure if that would impact the translation from PWM to LED color, but between the differences required for programing the two chips and the great resources the new chip has, I think I'll forgo the 16F1455 I'd use as a test bed, and go straight to the 12F1572 and get comfortable playing with that.
Dave's not here, man

On some nights I still believe that a car with the gas needle on empty can run about fifty more miles if you have the right music very loud on the radio
- Hunter S. Thompson

cloudscapes

you don't need high bit PWM to control LEDs. go with what's easy/comfortable instead.

10bit PWM means a LED color (or non-RGB led) can have 1024 levels of brightness. that's massive, your eye won't be able to tell the difference between brightness level 437 and 438. 16bit means 65536 levels of brightness. using that resolution just doesn't make sense to control an LED.

I usually drive LEDs with 8bit data (PWM or otherwise). it's plenty.
~~~~~~~~~~~~~~~~~~~~~~
{DIY blog}
{www.dronecloud.org}

dbp512

Oh, ok. I wasn't aware you could set the bitrate of the PWM, I thought it was whatever the chip was capable of. You'll notice I know absolutely nothing about this. Even so, I thought since I'd be using a CV to shift between two preset levels it wouldn't matter how fine the resolution is. Although I'd assume 8 bit is easier to encode.
Dave's not here, man

On some nights I still believe that a car with the gas needle on empty can run about fifty more miles if you have the right music very loud on the radio
- Hunter S. Thompson

cloudscapes

I can't speak for pic1x's specifically, but generally each chip has a couple bit depths for the PWM. not every value between 1 and 16, but a couple. like 10 and 16 or something. Most support 16, my point was that aspect of it doesn't matter for LEDs. Get whatever chip you want, and whatever PWM bitdepth it supports, that'll be fine for driving LEDs.

However,

An RGB LED needs 3 PWM drivers (one for each color). It depends on the chip, but some may only have one or two PWM-capable pins. PWM can't be output from any pin you want. There are usually pre-set pins that are capable of it that you have to stick to. Tiny 14 or 8 pin pics may only have one or two.

The PIC12F1572 you posted will be fine. Less so because the PWM depth is 16bit, but because you know it'll have 3 of them and that it works. ;)

I'm out of my expertise compared to some others in the thread. I haven't done much RGB LED stuff compared to others. Personally, I'd just write my own software implementation using whatever pins I want, but that's probably more advanced than you need, so disregard.

~~~~~~~~~~~~~~~~~~~~~~
{DIY blog}
{www.dronecloud.org}

dbp512

I'm aware PWM is limited to certain pins; I've read the datasheets for a handful of chips. If college taught me anything its how to drink research. Granted literature on neuroscience are a bit different than microcontrollers, but I understand at least the first few pages that show the pin allocation. Its good to know that driving an LED doesn't need a high bitrate like the demonstration mentioned.

As for writing my own code, it would be difficult but a good learning experience. I'll spend some time reading up on programming. I could work my way up to PWM: get each light to turn on, blink, blend colors, etc. I'm in no hurry, and it would be nice to get a better understanding of how it actually works.
Dave's not here, man

On some nights I still believe that a car with the gas needle on empty can run about fifty more miles if you have the right music very loud on the radio
- Hunter S. Thompson

samhay

>So it looks like the 12F1572 is a pretty good choice for RGB light control. Good enough for microchip to promote it for such purpose:
http://www.microchip.com/promo/rgb-badge-color-mixing-demo

It's nice to see microchip showing good demonstrator projects, but there is a lot of code in there that you don't need if you simply want to read a pot and then spit out 3 PWM outputs.

If you decide to use microchip's mplab IDE and xc8 compiler, I'm happy to give you a hand.
I'm a refugee of the great dropbox purge of '17.
Project details (schematics, layouts, etc) are slowly being added here: http://samdump.wordpress.com

slacker

#16
Quote from: dbp512 on April 20, 2016, 07:59:18 PM
Oh, ok. I wasn't aware you could set the bitrate of the PWM,

The number of bits quoted for a PWM module is the maximum it can do, they can do any number up to that maximum. There's two things to consider with PWM speed and number of bits and they're interrelated, say you want 10 bits then like Etienne said that's 1024 levels, that means the PWM wave form needs 1024 steps. The PWM modules is driven by the processor's main clock and each step takes one clock cycle so the maximum speed is clock speed/number of bits, so say your PIC is running at 1Mhz that's 1,000,000/1024 = 976Hz. If you need a faster PWM speed then you need to either run the PIC faster or reduce the number of bits, 8 bits would be able to do up to 1,000,000/256 = 3.9Khz and 4 bits could go up to 62Khz.
For driving LEDs speed isn't likely to be an issue so you'll probably be doing it the opposite way round, picking how many bits you want and then running PWM module at a speed that gives you that number.
For 8bit PICs there's online calculators like this one http://www.micro-examples.com/public/microex-navig/doc/097-pwm-calculator.html that will show what speeds and number of bits you can get, and they'll generate the code needed to set up the PWM module.

ElectricDruid

Slacker's hit multiple nails on their various heads there. +1 agree with all of that from me. A good explanation of the basics of PWM on the PIC.

Tom