Friday, August 03, 2012

Managing many LEDs on Arduino

Arguably the most simple Arduino project of all is the "blink" sketch which comes pre-installed the Arduino software (examples > basics > blink).  Some Arduino boards even have a surface mount LED already connected to pin 13 so you can do this sketch without any additional hardware.

/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.
 
  This example code is in the public domain.
 */
 
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;

// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);     
}

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
}

And once you get that far, it's not much more difficult to blink multiple lights in some sequence, using a separate arduino pin for each LED you wish to light, and some logic defining which to blink when, or which delay etc.  The "problem" is that as you add more LEDs to such a project each LED requires a dedicated pin. 10 LEDs = 10 pins, 15 LEDs = 15 pins etc, so that approach reasonably scales up to about 16 pins, give or take.  After that you need a smarter way to address the LEDs.  One way is to buy a fancy LED array or the alternative is to make your own.  I chose the latter of course :)

The video below to shows how to wire a circuit in order to allow your Arduino to drive more LEDs than you have pins, and also how to use a technique called multiplexing to avoid some of the compromises in not actually having one pin per LED.  I use this method to wire the circuit board in TINY (TINY Is Not space invaYders) which features a 5x5 array of 25 LEDs using "only" 10 pins.


No comments: