Charlieplexing - Arduino controlling multiple LEDs

Post all your model railway electronic problems, solutions and discoverys here.
User avatar
minipix
Posts: 133
Joined: Fri Nov 03, 2017 1:29 pm

Charlieplexing - Arduino controlling multiple LEDs

Postby minipix » Fri Apr 17, 2020 11:41 am

After my Arduino-based DC controller was shelved, I've found an alternative use for my tiny Arduino Nano - controlling the lights around my layout. The Arduino only has a certain number of digital pins, but thanks to a concept called Charlieplexing we can get around that.

The end result I'm aiming for is to produce some sort of visible progression through the day, with lighting changes adding interest. Not everything needs to be controlled by the Arduino - some could be manually controlled by plugging in the adapter, such as street lights. But I'd like building lights to come on or off in particular orders, or randomly. For example, the engine shed needs to be lit first thing in the morning, the main booking hall needs to open before the other visitor attractions, and house lights can probably turn on and off pretty randomly to suggest people moving around inside.

Now to Charlieplexing. It's named after a guy called Charlie, unsurprisingly, and takes advantage of the fact that LEDs only accept current in one direction, and that Arduinos (and other similar microprocessors) can switch whether a particular input is positive, negative, or disconnected altogether. It means that you can use a wiring diagram like below to control any one of the LEDs individually just by setting what the pins are doing.

2020-04-17 11.36.42 upload.wikimedia.org 15be9053aeb5.jpg


By setting pin 1 positive, pin 2 negative and everything else off, you can see that only one LED can light up. Any other combination is possible. But it only lights up ONE LED at a time. It's worth noting that there is a limit to how much current an Arduino can handle, so you can't run several LEDs in parallel or series off a single pin. But by cycling through all the LEDs really really quickly, the human eye can't distinguish the flickering, and "persistence of vision" means all the LEDs look like they're on at the same time. Add in a little programming to control which LEDs are being included as it cycles through, and we have control of a whole load of LEDs using just a few pins!

There is a simple equation for working out how many combinations are possible for a given number of pins. If P is the number of pins you're using, the total number of LEDs you can control is P*(P-1). 2 pins will power 2 LEDs. 3 pins can power 6 LEDs. 4 pins can power 12 LEDs. 5 pins can power 20 LEDs. And so on.

I've already got a proof of concept working nicely, and I've started rewiring my layout accordingly. I'm hoping to find a light sensor I can use on the Arduino too, so that it can figure out itself whether to run a night or a day program depending on the ambient lighting on the layout (I've got one of those colour-changing light bulbs above my layout). The idea will be that as we go from night to dawn all the lights will start to come on as people wake up and get to work. Once we get into the morning, the lights will mostly all go out again, apart from some buildings that would always be lit. As we hit dusk all the street lights come back on and the house lights come on and off. Then going from evening to night everything except the streetlights turn off. When I've got everything working I'll see if I can record a video of the complete cycle to show you!

More information on Charlieplexing: https://en.wikipedia.org/wiki/Charlieplexing
Frontington & Backwoods Railway: https://www.matthewdawkins.co.uk/catego ... way-build/
LocoSound - sound effects for DC model railways: https://mafu-d.github.io/loco-sound

abenn
Posts: 237
Joined: Thu Jun 05, 2008 11:03 am

Re: Charlieplexing - Arduino controlling multiple LEDs

Postby abenn » Mon Apr 20, 2020 8:48 pm

A neat idea, but there's one thing I'd like to clarify about the diagram: Even though the three resistors are given different numbers, I presume they're all the same value, depending on the operating voltage and the LED characteristics. But if the LEDs have different characteristics (perhaps different colours are used in different places), each one would possibly need to have it's own specific resistor rather than relying on the three common ones.

User avatar
minipix
Posts: 133
Joined: Fri Nov 03, 2017 1:29 pm

Re: Charlieplexing - Arduino controlling multiple LEDs

Postby minipix » Mon Apr 20, 2020 9:03 pm

Absolutely right. As it happens I bought a pack of LEDs a while back that had resistors already attached, so I'm using those. Otherwise, yes you'd have to rely on the LEDs being "equal".

I'm still trying to work out how to sensibly draw out a circuit diagram to put up here. Any suggestions?
Frontington & Backwoods Railway: https://www.matthewdawkins.co.uk/catego ... way-build/
LocoSound - sound effects for DC model railways: https://mafu-d.github.io/loco-sound


Return to “Electronics”

Who is online

Users browsing this forum: No registered users and 2 guests