Leading on from the previous post on how to create your very own breathing LED circuit and coding comes the next logical step – making more than one that can act independently.
Unlike the Arduino board; the Raspberry Pi only has a single pin which is capable of being configured as a PWM which enables you to create the breathing effect. So how do we emulate the breathing effect across several outputs that can act of both pin 1 (our PWM) and each other independently? There are actually a few answers to this question. Most of them involve addition of other reasonably expensive pieces of hardware.
However I’m just about to walk you through a method that requires nothing more than a few additional lines of code, some MOSFET transistors, some wiring, and a lot of patience.
For this amazing trick you will need:
- To have read the previous tutorial
- A MOSFET transistor for every additional LED or LED array you wish to feed off the circuit.
- More LED’s
- A diode for each additional circuit
- Even more patience
- Copy & Pasting skills
Essentially what this method does is vary the GPIO pins you select to “Pull down” resistors (which make them function as GROUND when enabled). As the raspberry pi is unfortunate in its lack of PWM’s – I believe ANY pin can be programmed to be either a Pull up (to 3.3v) resistor or a pull down (to ground resistor). And it is this that makes the circuit possible.
Essentially the circuit works like this:
We take our signal from the PWM pin – this is then used as a signal gate for the MOSFET transistors, which then pull power from the Raspberry Pi’s +ve power pin – But the really beautiful part is that the ground instead of being constantly connected to ground is instead connected to a variable (on/off) ground in form of the GPIO pins. Only when the pin is set to ground will the circuit light.
If you have understood that, you can skip to the section about coding now.
Now I’m going to tell you about the hardware a bit more in depth and exactly how to wire this circuit up. For this section I’m going to assume that you are using the standard prototyping breadboard – If you aren’t – You should be.
First off lets establish our (+ve and -ve rails on the breadboard) so go ahead and take a wire running from the (+3.3v or +5v) pins to your +ve rail – If you do use 5v PLEASE be careful because this may fry your logic systems if done incorrectly. Next connect up your ground wire to your -ve rail on your breadboard.
Next take your MOSFET transistor and plant it in the board.
Your MOSFET should look like this:
I’m going to refer to the pins labels as G, D & S as in the photo for ease.
So next we want to connect our signal wire, take this from your PWM pin (labelled #18 on the cobbler) and feed that into pin G on your mosfet.
TIP: If you are going to have many different circuits – it may be best to make one of the remaining UNUSED +ve or -ve rails on your breadboard a signal rail as you will be reusing this often and it keeps things tidy.
Next connect a wire running from pin S to the GPIO pin on the cobbler that you wish to use to control the on/off of the circuit.
Finally run a diode from your +ve rail – through the +ve leg of an LED and into the D pin of the MOSFET. The diode should be orientated to allow power to flow TO the MOSFET – this will prevent any nasty feedback.
Use this method and repeat for the amount of circuits you wish to produce.
Tip: To help with any trouble shooting you may want to make an LED circuit that runs directly from your PWM pin to ground to make sure it is functional throughout.
Now you’re ready for coding. In order to demonstrate this to you I’ve created two scripts that run simultaneously. The more code wise amongst those reading will not like these scripts as they are messy – but have only been created to demonstrate the effect and no more.
For the coding we’re going to re-use the code from the previous tutorial here with one difference.
Instead of the line that reads “while count < 100000:” We are going to replace it with “while True:” – This will make the program loop infinately providing us with a constant PWM wave. Execute the program and close LX terminal.
Next there are three new functions that I will introduce to you (I couldn’t find these in any documentation so it was a bit of guesswork).
io.pinMode(x,io.PUD_DOWN) – Set’s the pin as a pull down resistor
io.pinMode(x,io.PUD_UP) – Set’s the pin as a pull up resistor
io.pinMode(x,io.PUD_OFF) – Turns the pull up/down off.
If we use this in a time based loop like this and execute this program at the same time as our PWM loop is running then you SHOULD get an effect that looks very like this: