amnuts
Posts: 7
Joined: Fri Jun 14, 2013 10:55 pm

Controlling one RGB LED via hardware PWM?

Mon Sep 14, 2015 10:17 am

Hi there,

Is it possible to control a single RGB LED via the hardware PWM pin (on the B rev 2 board) so that I can not only change the colours of the LED but also give a pulse effect? I've played only with single-colour LEDs and have successfully made it fade in/out with PWM but I would like to try it with a single RGB LED next.

I've seen examples of people using full LED strips or multiple LEDs (using one or more TLC5940 chip), but does it need to be this complicated for a single LED? I also know it's possible to use software pwm (using something like wiringpi) which means I could use the standard GPIO pins, so that's an option, but the hardware PWM pin would seem a bit better if possible - or is that not really the case? (Obviously as it's just one - or maybe two, eventually - RGB LEDs then exact, precise timing is not essential, especially as this is just a fun thing to try!)

Any help or advice would be very welcome, thanks!

Andy

User avatar
joan
Posts: 15383
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Controlling one RGB LED via hardware PWM?

Mon Sep 14, 2015 10:47 am

An RGB LED is actually 3 LEDs in one module. You would need three PWM channels and recent Pis only have two hardware PWM channels.

My pigpio library provides hardware timed PWM on all the gpios on the expansion header.

Massi
Posts: 1691
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: Controlling one RGB LED via hardware PWM?

Mon Sep 14, 2015 10:48 am

Well, a rgb led is indeed a group of 3 leds :)
so, if your "color control" is about turning on and off each base led, you can have hardware pwm to control the brightness, but only 8 colors possible
Otherwise i think you need 3 channels, so software pwm is for you.
pigpio has a very good pwm management and a precision even lower that 5us, if you need it.
So i'd give it a try.

PS: i'm assuming pwm will control a transistor powering leds..

amnuts
Posts: 7
Joined: Fri Jun 14, 2013 10:55 pm

Re: Controlling one RGB LED via hardware PWM?

Mon Sep 14, 2015 11:32 am

Many thanks for your feedback so quickly!

@joan; thanks for the heads-up on your pigpio library - I'll check that our in due course.

@Massi; I was just going to use the RGB with three resistors (one for each anode) in the range of 220-330Ω - a really simple circuit. Should it be something more sophisticated than that?

So if I just wanted 8 colours max - which, to be honest, may even be overkill for what I need which is essentially to be able to fade white, red and blue - it sounds like a single PWM pin and then three GPIO would suffice? (GPIO to toggle high/low state of LED colour and PWM for brightness/fading.) Does the PWM get connected to one specific pin of the LED? (can't imagine it's the cathode, but as you may be able to tell, I don't have any clue what I'm talking about here... :-) )

I think I also need to crack a book or two on simple electronics, but that's fun for another time. ;-)

BMS Doug
Posts: 4422
Joined: Thu Mar 27, 2014 2:42 pm
Location: London, UK

Re: Controlling one RGB LED via hardware PWM?

Mon Sep 14, 2015 11:50 am

amnuts wrote: So if I just wanted 8 colours max - which, to be honest, may even be overkill for what I need which is essentially to be able to fade white, red and blue - it sounds like a single PWM pin and then three GPIO would suffice? (GPIO to toggle high/low state of LED colour and PWM for brightness/fading.) Does the PWM get connected to one specific pin of the LED? (can't imagine it's the cathode, but as you may be able to tell, I don't have any clue what I'm talking about here... :-) )

I think I also need to crack a book or two on simple electronics, but that's fun for another time. ;-)
you can control this with just 3 pins (connect common cathode to Gnd and use PWM control on each anode), the hardware timed PWM that PiGPIO offers on all GPIO pins will be fine for this task and thus you might as well use it and have as many colours as you can.
Doug.
Building Management Systems Engineer.

Massi
Posts: 1691
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: Controlling one RGB LED via hardware PWM?

Mon Sep 14, 2015 12:28 pm

without much thinking, i supposed 3 gpio controlling 3 MOS for RGB colors and a fourth MOS - controlled by hardware PWM and connected to all leds - to control brightness.
Now, i suspect there can be some issues with 2 mos in series :) this should be investigated deeper..

PS: look at this.. i'd go with software pwm :)
http://aruljohn.com/blog/raspberrypi-ch ... s-rgb-led/

amnuts
Posts: 7
Joined: Fri Jun 14, 2013 10:55 pm

Re: Controlling one RGB LED via hardware PWM?

Mon Sep 14, 2015 12:56 pm

It definitely looks like software pwm would be the easiest way to go. I'll look into pigpio sooner rather than later. :-)

Thanks all for the advice!

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 13696
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Controlling one RGB LED via hardware PWM?

Mon Sep 14, 2015 8:54 pm

make sure you adjust the series resistor concurrent with the forward voltage of the LED's, the red led probably has a lower forward voltage (around 2V) than the blue and green leds (around 3V), so leaving 1V or about 0.3V for the series resistors, in fact with just 0.3V you will see large variations in current when the 3V3 supply varies just a bit. You need to adjust the resistors to get similar LED currents (that is about 15mA) regardless of the LED colour.

It would be better to control 3 n-FETs, and power the LED's with 5V, but think about the primary rule with PI GPIO's, and make sure that 5V can never reach the GPIO's. Als, use "digital" FET's, thus FETs that fully open with just 3.3V gave voltage.
that means that most Pin-through-hole FET's, like the 2N7002 are unusable, as they are not meant to be steered open by 3V3 levels. Instead use something like a DMG1012T.

User avatar
joan
Posts: 15383
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Controlling one RGB LED via hardware PWM?

Mon Sep 14, 2015 9:17 pm

I have had 8 RGBs connected to a Pi2. I used a 1k resistor for the red leg and 470 ohm resistors for the blue and green legs. They were too bright when used on the Pi. I had to use PWM to get a tolerable brightness.

Return to “General discussion”