Phara0h
Posts: 6
Joined: Fri Jul 27, 2012 1:33 pm

Driving a RGB LED (PWM and 555 IC's)

Fri Jul 27, 2012 1:45 pm

Hello Raspberry hardware geeks! I am just getting into the hardware world with a extensive programming background looking into learning how to interface hardware. My first big task was wanting to power a RGB LED through the GPIOs on the PI. The first problem i ran into, was it only having one GPIO that could handle PWM! So I was doing some research and I gave it my best to try to emulate it in code, and I could only get the LED to slightly dim, then after that it just became a strobe. I was reading on some places about using the go to fix for every hardware guy it seems "O just use a 555 to do the PWM for you". This would be all fine and dandy if I knew how to use a 555 timer.

So my question is how can I give one of my gpios that does not have PWM, to have PWM on it using a 555 IC?

How does one go about talking to a 555 IC? Like could some one run down the process of how I could control the brightness (aka speed) of the 555 just though one GPIO?

If its possible to emulate this in code (C code) would you mind sharing a example?

(PS please don't try to sound smart by using hardware terms and such, since I only have very basic knowledge with hardware and it will only make your answer not useful to me. Trust me I know you are extremely smart, just dumb it down for me.) :D

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

Re: Driving a RGB LED (PWM and 555 IC's)

Fri Jul 27, 2012 2:07 pm

The 555 timer is the most sold IC in history, so its no surprise to hear it being advised for a whole host of things.

You can learn about the 555 here (where else but wikipedia)
http://en.wikipedia.org/wiki/555_timer_IC

And yes it can be used to generate PWM, but only up to a certain duty cycle, say up to 45%, not over the whole range between 1 and 99%. To get above 45% just add an inverter behind the output.

So you can generate a pulse width modulated signal, but if you do it too slow, the led just starts to blink, as you have seen.

The problem is, that you cannot really control the 555 from the PI, for that you would need an analog output.

There are specialised IC's connected through I2C, or SPI to create multiple software controlled PWM signals, but that is probably beyond your capabilities.

creating (multiple) PWM signals in software is certainly possible, but an OS as linux makes it very hard to keep the timing right. In that regard using an Arduino would be a better choice.

User avatar
Grumpy Mike
Posts: 936
Joined: Sat Sep 10, 2011 7:49 pm
Location: Manchester (England England)
Contact: Website

Re: Driving a RGB LED (PWM and 555 IC's)

Fri Jul 27, 2012 2:12 pm

sound smart by using hardware terms and such
Any terms used are not an attempt to sound smart but to :-
1) Be accurate.
2) Get you used to using the right words
Anything you don't understand then please ask.

For a start do you understand what PWM actually is? If not see this:-
http://www.thebox.myzen.co.uk/Tutorial/PWM.html
I was reading on some places about using the go to fix for every hardware guy it seems "O just use a 555 to do the PWM for you".
The big problem with this is how you control the 555. This takes an analogue signal so you would either need a digital to analogue converter called an D/A or a device known as a digital pot which you control over an interface called SPI ( Serial Protocol Interface ).
However, having done that you do not get very good control anyway.

Your best bet would be to use a chip that has PWM control built into it. One good chip is a TLC5940, you can control 16 LEDs with one chip, that is 5 RGB LEDs, and chips can easily be chained together.
The sang with that is that you would have to write the code that interfaces that chip. The Pi being very new might not have code to do that yet but I believe it is the way to tackle your problem. At least you have some terms you can search on.

Phara0h
Posts: 6
Joined: Fri Jul 27, 2012 1:33 pm

Re: Driving a RGB LED (PWM and 555 IC's)

Fri Jul 27, 2012 3:43 pm

Thanks for the responses, I am using Linux as my OS on the PI. Do you have some ideas on how to do it in code even though the timing would be very hard? Maybe some C examples or even just sudo code would be cool.

I only really need to drive one RGB led, so having a chip like TLC5940 would be complete over kill (price wise and power wise) Power consumption for my main goal is important.

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

Re: Driving a RGB LED (PWM and 555 IC's)

Fri Jul 27, 2012 3:52 pm

"sudo code?" whats that?

sudo is a command you can put in front of any command line command, it has nothing to do with programming, and if you simply want a prefabricated piece of "code" to execute (with or without sudo), then I'm sorry it doesn't exist.

If you need just a single PWM then use the single PWM GPIO output.

Creating a PWM output (in any programming language, even assembler) on a Linux system isn't really possible, as Linux does many tasks at the same time, so your "PWM program", that doesn't do anything but regularly toggle a GPIO port, may not get the CPU's attention for an undeterminable time, so creating any signal with a determinable interval isn't possible on a system like linux, you will need extra hardware to do the timing, that is you need PWM (timer) hardware.

A microcontroller like the Arduino is much simpler, and you can just "count instructions" to create determinable timing.

pholy
Posts: 46
Joined: Sun Jun 24, 2012 11:29 pm

Re: Driving a RGB LED (PWM and 555 IC's)

Fri Jul 27, 2012 4:40 pm

"sudo code?" whats that?

"pseudo code" pronounced 'sue dough' -- almost like 'Sue do' this (or that)
as in imitation or almost like code - text that presents an idea but would never compile..

Gotta love those homophones!

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

Re: Driving a RGB LED (PWM and 555 IC's)

Fri Jul 27, 2012 4:58 pm

LOL

Phara0h
Posts: 6
Joined: Fri Jul 27, 2012 1:33 pm

Re: Driving a RGB LED (PWM and 555 IC's)

Fri Jul 27, 2012 10:26 pm

mahjongg wrote:"sudo code?" whats that?

sudo is a command you can put in front of any command line command, it has nothing to do with programming, and if you simply want a prefabricated piece of "code" to execute (with or without sudo), then I'm sorry it doesn't exist.

If you need just a single PWM then use the single PWM GPIO output.

Creating a PWM output (in any programming language, even assembler) on a Linux system isn't really possible, as Linux does many tasks at the same time, so your "PWM program", that doesn't do anything but regularly toggle a GPIO port, may not get the CPU's attention for an undeterminable time, so creating any signal with a determinable interval isn't possible on a system like linux, you will need extra hardware to do the timing, that is you need PWM (timer) hardware.

A microcontroller like the Arduino is much simpler, and you can just "count instructions" to create determinable timing.

Yes I meant pseudo code, not the Linux privileged escalating program.

tech_monkey
Posts: 130
Joined: Fri Mar 09, 2012 6:12 pm

Re: Driving a RGB LED (PWM and 555 IC's)

Fri Jul 27, 2012 10:56 pm

Pseudocode is a kind of structured English for describing algorithms. It allows the designer to focus on the logic of the algorithm without being distracted by details of language syntax. At the same time, the pseudocode needs to be complete. It describes the entire logic of the algorithm so that implementing it becomes a simple task of translating line by line into source code.
I was taught Pseudocode when learning how to program in assembler on a 6502 machine (yes the same as on the good old beeb and others) but this computer had a keypad and some 7 segment displays and some red LEDs you could turn on or off.
There is an actual standard for Pseudocode.
Any one who has written program in C, VB, BBC basic and Pascal will find it fairly easy to convert a Pseudocode program to the relevant language.
http://www.casatech.eu

Phara0h
Posts: 6
Joined: Fri Jul 27, 2012 1:33 pm

Re: Driving a RGB LED (PWM and 555 IC's)

Fri Jul 27, 2012 11:00 pm

tech_monkey wrote:Pseudocode is a kind of structured English for describing algorithms. It allows the designer to focus on the logic of the algorithm without being distracted by details of language syntax. At the same time, the pseudocode needs to be complete. It describes the entire logic of the algorithm so that implementing it becomes a simple task of translating line by line into source code.
I was taught Pseudocode when learning how to program in assembler on a 6502 machine (yes the same as on the good old beeb and others) but this computer had a keypad and some 7 segment displays and some red LEDs you could turn on or off.
There is an actual standard for Pseudocode.
Any one who has written program in C, VB, BBC basic and Pascal will find it fairly easy to convert a Pseudocode program to the relevant language.
Fact

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

Re: Driving a RGB LED (PWM and 555 IC's)

Sat Jul 28, 2012 12:19 am

pseudocode?

Something like this:

Variable PWM is percentage defining ratio between the time of the signal being low and high (between 0.0 and 1.0 for between 0 and 100%)
Variable FREQ is frequency of output signal (several KiloHz)
Set Variabele INTERVAL equals 1 divided by FREQ (a fraction of a millisecond)
Begin:
Set Low_Time equals to INTERVAL times PWM
Set High_Time equals to INTERVAL times (100%-PWM)
Forever Do:
Set output high
Wait Low_Time
Set output low
Wait High_Time
End Do:

Simple isn't it?
Problem is that "Wait" isn't deterministic for the kinds of frequencies needed, on Linux, at least not without extra hardware.

tech_monkey
Posts: 130
Joined: Fri Mar 09, 2012 6:12 pm

Re: Driving a RGB LED (PWM and 555 IC's)

Sat Jul 28, 2012 6:25 pm

Would the Gert board be useful as there is a video demo somewhere on the PI website with it running a stepper motor and I guess thats using PWM.
http://www.casatech.eu

domesday
Posts: 258
Joined: Fri Oct 21, 2011 5:53 pm
Location: UK

Re: Driving a RGB LED (PWM and 555 IC's)

Sat Jul 28, 2012 7:45 pm

tech_monkey wrote:Would the Gert board be useful as there is a video demo somewhere on the PI website with it running a stepper motor and I guess thats using PWM.
Not really no as it only uses the single built-in PWM output on the Pi, tho OP needs 3.

Chewy
Posts: 12
Joined: Fri Mar 09, 2012 11:34 pm

Re: Driving a RGB LED (PWM and 555 IC's)

Sun Jul 29, 2012 7:24 am

I empathise with Phara0h since I went through exactly the same loop whilst researching the control of motors from a pi (or whatever he wants to drive with PWM) and for me the answer was *cough* Arduino - The pi I will use for cameras or perhaps navigation or anything that is far less real time dependent.

I'm sure all of the interfacing to additional ICs is possible but I concluded it wasn't the easiest way to achieve the objective.

tech_monkey
Posts: 130
Joined: Fri Mar 09, 2012 6:12 pm

Re: Driving a RGB LED (PWM and 555 IC's)

Sun Jul 29, 2012 8:32 pm

domesday wrote:
tech_monkey wrote:Would the Gert board be useful as there is a video demo somewhere on the PI website with it running a stepper motor and I guess thats using PWM.
Not really no as it only uses the single built-in PWM output on the Pi, tho OP needs 3.
Couldn't you multiplex it, or would that produce too much flicker.
http://www.casatech.eu

Phara0h
Posts: 6
Joined: Fri Jul 27, 2012 1:33 pm

Re: Driving a RGB LED (PWM and 555 IC's)

Mon Jul 30, 2012 4:44 am

Chewy wrote:I empathise with Phara0h since I went through exactly the same loop whilst researching the control of motors from a pi (or whatever he wants to drive with PWM) and for me the answer was *cough* Arduino - The pi I will use for cameras or perhaps navigation or anything that is far less real time dependent.

I'm sure all of the interfacing to additional ICs is possible but I concluded it wasn't the easiest way to achieve the objective.
Thanks man! At least one person is not kicking ye down unders in here. It kinda sucks since I bought 2 of these things. What I would need to do is have some way the Arduino to talk to the PI. Now that would be nice. Or 1 Arduino for multi PI's

texy
Forum Moderator
Forum Moderator
Posts: 5161
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: Driving a RGB LED (PWM and 555 IC's)

Mon Jul 30, 2012 11:06 am

As Grumpy Mike has suggested, the TLC5940 chip is the way to go - it can even have a supply voltage from 3V to 5V, so ideal to be driven by the Pi, logic level wise. However you could also use serial comms from the Pi to an Arduino if you wanted to go that route - search the forums as tis has been done by many people. You haven't stated what the project is for, or how many LED's you want to drive?

Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

WPsites
Posts: 17
Joined: Sun Jun 10, 2012 11:05 am
Location: Southport, Merseyside, UK
Contact: Website

Re: Driving a RGB LED (PWM and 555 IC's)

Mon Jul 30, 2012 2:00 pm

I've just added a blog post to my site on how I've got Ruby talking to an Arduino over serial. Later today I'm going to put the Ruby code onto my Raspberry Pi, plug the Arduino into the Pi and have the Pi moving send move servo commands to the Arduino. The Arduino uses PWM to tell the servo what position to move to.

Check out my post, it might give you some ideas http://www.simonthepiman.co.uk/local-daemon-demo/

User avatar
Grumpy Mike
Posts: 936
Joined: Sat Sep 10, 2011 7:49 pm
Location: Manchester (England England)
Contact: Website

Re: Driving a RGB LED (PWM and 555 IC's)

Mon Jul 30, 2012 2:36 pm

Phara0h wrote:I only really need to drive one RGB led, so having a chip like TLC5940 would be complete over kill.
Well how about the A6281 chip. That has 3 PWM outputs and is designed for driving an RGB LED.

It is the chip at the center of the Shift Bright product.
http://docs.macetech.com/doku.php/shiftbrite_2.0

Phara0h
Posts: 6
Joined: Fri Jul 27, 2012 1:33 pm

Re: Driving a RGB LED (PWM and 555 IC's)

Tue Jul 31, 2012 10:17 pm

Grumpy Mike wrote:
Phara0h wrote:I only really need to drive one RGB led, so having a chip like TLC5940 would be complete over kill.
Well how about the A6281 chip. That has 3 PWM outputs and is designed for driving an RGB LED.

It is the chip at the center of the Shift Bright product.
http://docs.macetech.com/doku.php/shiftbrite_2.0

Sweet best answer yet, cheap and to the point. Now I have one question. Will i be able to push binary out my GPIO into this board? By looking at the code, I am guessing I just send my data (a series of on and off through one of my GPIO) then "latch" in the data though another GPIO, confirming im done.

User avatar
Grumpy Mike
Posts: 936
Joined: Sat Sep 10, 2011 7:49 pm
Location: Manchester (England England)
Contact: Website

Re: Driving a RGB LED (PWM and 555 IC's)

Wed Aug 01, 2012 9:23 pm

Yes just toggle the GPIO lines. The fact that Linux will steel time from you doesn't matter as long as the sequence of the GPIO toggling is in the right order.

Return to “Interfacing (DSI, CSI, I2C, etc.)”