edyb wrote:I've been looking at a few posts on Youtube about multiplexing and I thought maybe it would be possible to do so by using some transistors and flicking the LED's on really quickly in software to simulate the digital display. That works if you have the individual LEDs and can arrange them either in a matrix or to look like a 7-element LED numeric display.
However, if I used an LED numeric display already made (like the ROHM LBP-602MA2) it has all of the 8 LED's per number (7 for number and 1 decimal point) all connected to a common ground within the package. So I can't really "index" a particular LED in this package by using a matrix layout.
On the other hand, if I construct my own LED display an keep the grounds separate, I could theoretically light up any one of the group of 8 LED's using a 3x3 matrix (with one to spare). If I connect up 16 LED's for a 2-digit display then I have 16 LED's which I can index individually using a 4x4 array perfectly. Therefore I would require 8 GPIO pins total, and 4 PNP and 4 NPN transistors.
My program would know the position of each LED element in the display and turn on/off the appropriate ones based on the number one wishes to display. This would just be a translation table that knows for each number at each digit which LED's should be lit up as it maps to the 4x4 array, so my function could be made as simple as "displaynumber(var num)" and it would figure out what LED's to cycle through to display the number "num" on the display.
As long as it switches fast enough it should be able to do this without noticable flicker.
However, I am not sure based on the current part I have (with common ground) if it is possible to do this. Unless I can still create a 4x4 matrix and wire it up in a way where each node (16 in all) somehow links up to either feed current to or switch each of the LED inputs for the digital display.
In the simplest case, you can do it without transistors.
With a single LED 7-segment digit, you can wire 7 GPIO pins to 7 resistors and wire the common to ground and off you go. You can turn on each segment individually and all that's needed is a lookup table to make segments to digits.
With 2 digits, just double up on this, but as you are seeing, you run out of GPIO pins fairly quickly.
So... Rather than that method, you wire the 7 (or 8) GPIO outputs directly to the 7 LEDs and one resistor on the common pin and wire that to ground.
Now you can light the LED segments individually by setting the GPIO on each one in-turn to 1. You can light more than one LED, but the current is halved as there is only one resistor to the brightness is affected, so you can't reliably do that.
You can put the resistors on the other side, so back to 7 resistors again and light them all at once, but it's 6 more reistors.
Doing it that way (one resistor) then allows you to have some code that lights the segments in-turn in a loop, to make up each digit - but remember to only light up one segment at a time.
The next stage is to work out how to do more than one digit. So we need a way to turn the entire digit off or on... So to do this, we can wire the common via a resistor not to ground, but to another GPIO pin. This GPIO then becomes the "enable" for that particular digit. Set it to 1 and none of the segments will light, set it to 0 and the segments enabled will light - but remember to only light one at a time.
Then, if you parallel up the 7 GPIOs to a 2nd 7-segment digit and use one more resistor and one more GPIO pin (so now 7+2 = 9 pins) you can then control which digit is enabled and which segment on that digit gets lit up.
The programming becomes slightly more complex and remember that you need to keep the program running, but you only need one more GPIO pin per digit. So with 17 GPIOs avalable, then in-theory, using the full 8 segments, you can have 17 - 8 = 9 digits. And a program running a big, fast loop to keep them all lit-up without flickering.
If you've ever seen some of the older micros - zx80/81 for example - their screens used to flicker when you pushed a key as the CPU had to stop genrating the screen and deal with the key - same for some old calculators... you'll end up with something like that unless you're very clever with the program..
An optimisation (in terms of efficiency) is to go back to 7 (or 8) resistoed per digit - then you can concurrenty light up more than one segment, however the issue then is the current your sinking into the common GPIO pin for that digit may be too much for the GPIO pin to allow - that's when you may need to use a buffer of some sort (transistor, etc.) to increase the current capacity, however it simplifies the code a little, but you still need to keep the loop going to make sure all the digits get lit-up in turn.