I have a microcontroller with one available digital pin.
How can I control 25 LEDs?
Electrical Engineering Stack Exchange is a question and answer site for electronics and electrical engineering professionals, students, and enthusiasts. It only takes a minute to sign up.Sign up to join this community
I2C IO Expander: - Some protocol overhead, but it should be expandable to a very large (thousands) number of LEDs.
Simple shift register - Dead Simple interface, the LEDs may flicker when updating if the shift register clock is not fairly fast. Using a double buffered shift register will fix this.
Shift Register and I2C is good, but only if there are at least 2 pins free. I suggest Dallas 1-wire port extender DS2408 will be an optimal solution. Code for Dallas 1-wire device access is not so complicated and there are plenty of examles on the web.
You may use several DS2408 connected to one pin or implement more logic after single DS2408 whichever you'd like.
With only 1 IO pin, you can use a 1-wire expander/shift register or add another device to your I2C bus, as already pointed out. The only thing that hasn't been mentioned yet is the possibility of adding another slave to your SPI port.
SPI is called a 4-wire protocol - You have two data lines, a clock line, and a select line (as well as a common ground, but that doesn't usually count). However, the first three are shared among all the devices on the bus, so each device after the first one takes only one more trace/wire. SPI also increases your bus to full duplex, but that won't matter for this application.
If you really have only 1 pin available the Dallas 1-wire bus looks like the most obvious thing. However, since this is output-only, there are cheaper solutions. A serial PWM bus which carries both data and clock is easy to set up; see for instance Roman Black's page as also mentioned by davidcary. You only need serial-in parallel-out shift registers like the 74VHC164, which you can cascade for as many outputs as you want (one '164 has 8 outputs).
In this solution LEDs are driven statically.
If you do have other I/O you can share, like SPI of I2C, you may go for more luxurious solutions, like Maxim's MAX6950. The MAX6950 has blinking and brightness control, and slew-rate limiting, to name a few features. LEDs are multiplexed, which means you only need one 16-pin driver.
25 LED's depending on size can draw a lot of power for a single digital IO pin on a micro-controller. Rather than driving them directly, you should probably use a simple FET switch (so the IO is tied to gate) to control the power to the LED chain.
Depending on your application, there are other more robust ways to accomplish this. But throwing in a fet will probably get you working the fastest.
For example, if you want to gate a variable signal, there more complicated devices, sometimes called digital relays, that give you a way to digitally switch it.
As for individually controlling 25 LED's with a single IO port. As others suggested, you can use a few shift registers. There are more complicated solutions involving communicating with another IC using a serial protocol (I2C for example).
Roman Black describes the shift1 system that allows you to independently control any number of LEDs from a single microcontroller pin.
@Fake Name, @Tim, @pingswept all suggest chaining some shift registers together, the DO data output pin on one feeding the DI data input pin on the next. The 74HC595 would work fine. (Perhaps some other chip would work slighly better).
(@reemrevnivek, with this kind of daisy-chained SPI, each device after the first does not require any more pins on the microcontroller, contrary to what many people claim -- why do you listen to them? :-).
Normally this requires 4 pins on the microcontroler -- MISO, MOSI, SCLK, and latch. Since you are only doing output, you don't need a MISO input pin.
Roman Black has figured out that with a some very careful timing on on a single microcontroller output, and with some careful tweaking of an analog circuit, a single microcontroller pin can drive a simple-looking analog circuit that separates out the common SCLK, the DI pin of the first chip in the chain (MOSI), and the common latch signal.
Then that microcontroller can control 25 LEDs from a single pin.