Max7313 i2c 16port pwm led driver code

for anyone who cares:

working code for the arduino and the max7313.
you would have to lookup how to solder the adress pins on the max 7313. there are 64 combinations possible, so check out the datasheet. with combinations of more 7313 chips you can control 64 * 16 = 1024 LED (and dim them!) with 2 arduino pins!! A little hard to solder though.

this code interfaces two chips. First it flips through all the leds one by one, for 5 times. Then it dimms all the leds gradually all at once.

#include <Wire.h>

// code for max7313
// eric toering

// Using the Wire library (created by Nicholas Zambetti)
// On the Arduino board, Analog In 4 is SDA, Analog In 5 is SCL


   void stuur(byte adres, byte reg, byte data);
   byte plaat = 0x10;      // adresses of max chips
   byte plaat2 = 39;
 
   int intensiteit = 0xff;    // 
   
   byte chipdata = 0;


void setup()
{
  
  
  Wire.begin(); // join i2c bus (address optional for master)
  Serial.begin(9600);

  stuur(plaat, 0xf, 0x10);                  // blink 0 aan, 0x10 is glob uit
  stuur(plaat, 0x6, 0x00);                  // input en output config.
  stuur(plaat, 0x7, 0x00);                  // oninterresante getallen, afblijven!!
  stuur(plaat, 0x2, 0xff);                  // global intensity reg. 
  stuur(plaat, 0x3, 0xff);
  stuur(plaat, 0xe, 0xff);                  // config bit
  stuur(plaat2, 0xf, 0x10);                  // blink 0 aan, 0x10 is glob uit
  stuur(plaat2, 0x6, 0x00);                  // input en output config.
  stuur(plaat2, 0x7, 0x00);                  // oninterresante getallen, afblijven!!
  stuur(plaat2, 0x2, 0xff);                  // global intensity reg. 
  stuur(plaat2, 0x3, 0xff);
  stuur(plaat2, 0xe, 0xff);                  // config bit
  
  for (int i = 0x10; i < 0x17; i++){
      stuur(plaat, i, 0x00);
      stuur(plaat2, i, 0x00);
  }
  Serial.println("check");
  delay(500);
  for (int i = 0x10; i < 0x17; i++){
      stuur(plaat, i, 0xff);
      stuur(plaat2, i, 0xff); 
  }
  delay(500);
}



void loop()
{
  
  
  /*  small routine for finding out how the adress pins are connected
   for (int i = 0; i < 255; i++){
    plaat = i;
    
  stuur(plaat, 0xf, 0x10);                  
  stuur(plaat, 0x6, 0x00);                  
  stuur(plaat, 0x7, 0x00);                  
  stuur(plaat, 0x2, 0xff);                  
  stuur(plaat, 0x3, 0xff);
  stuur(plaat, 0xe, 0xff);            
  stuur(plaat, 0x10, 0x00); 
  stuur(plaat, 0x11, 0x00); 
  stuur(plaat, 0x12, 0x00); 
  stuur(plaat, 0x13, 0x00); 
  stuur(plaat, 0x14, 0x00); 
  stuur(plaat, 0x15, 0x00); 
  stuur(plaat, 0x16, 0x00); 
  stuur(plaat, 0x17, 0x00); 
  Serial.println(i, DEC);
  delay (5);
  
  }
  
     for (int i = 0; i < 255; i++){
    plaat = i;
    
  stuur(plaat, 0xf, 0x10);                  
  stuur(plaat, 0x6, 0x00);                  
  stuur(plaat, 0x7, 0x00);                  
  stuur(plaat, 0x2, 0xff);                   
  stuur(plaat, 0x3, 0xff);
  stuur(plaat, 0xe, 0xff);                  
  stuur(plaat, 0x10, 0xff); 
  stuur(plaat, 0x11, 0xff);
  stuur(plaat, 0x12, 0xff); 
  stuur(plaat, 0x13, 0xff); 
  stuur(plaat, 0x14, 0xff); 
  stuur(plaat, 0x15, 0xff); 
  stuur(plaat, 0x16, 0xff); 
  stuur(plaat, 0x17, 0xff); 
  Serial.println(i, DEC);
  delay (5);
  
  }
  
}
  
 */

for (int y = 0; y < 5; y++){

for (int i = 0; i < 16; i++){
      
      if ((i % 2)>0){ stuur(plaat2, (0x10+(i / 2)), 0x0F); 
      }
      else { stuur(plaat2, (0x10+(i / 2)), 0xF0);}
      
      delay(60);
      
      stuur(plaat2, (0x10+(i / 2)), 0xFF);
      
}

for (int p = 0; p < 16; p++){
      
      int i = 15 - p;
      
      if ((i % 2)>0){ stuur(plaat, (0x10+(i / 2)), 0x0F);
      }
      else { stuur(plaat, (0x10+(i / 2)), 0xF0);}
      
      delay(60);
      
      stuur(plaat, (0x10+(i / 2)), 0xFF);
      
}

  
}  
  
  for (int x = 0; x< 5; x++){
    
  
  while (intensiteit > 0){

  for (int i = 0x10; i < 0x18; i++){
      stuur(plaat, i, intensiteit); 
      stuur(plaat2, i, intensiteit);
  }
  
  delay(40);
  
  intensiteit -= 0x11;
 
}

   while (intensiteit  < 0xff){
      for (int i = 0x10; i < 0x18; i++){
      stuur(plaat, i, intensiteit); 
      stuur(plaat2, i, intensiteit); 
  }
  
  delay(60);
  
  intensiteit += 0x11; 
   }  
  
  } 
 
 for (int i = 16; i > 0; i--){
      
      if ((i % 2)>0){ stuur(plaat2, (0x10+(i / 2)), 0x0F); 
      }
      else { stuur(plaat2, (0x10+(i / 2)), 0xF0);}
      
      delay(50);
      
      stuur(plaat2, (0x10+(i / 2)), 0xFF);
      
}

for (int i = 0; i < 16; i++){
      
      if ((i % 2)>0){ stuur(plaat, (0x10+(i / 2)), 0x0F); 
      }
      else { stuur(plaat, (0x10+(i / 2)), 0xF0);}
      
      delay(80);


      stuur(plaat, (0x10+(i / 2)), 0xFF);
      
}
  
}


void stuur(byte adres, byte reg, byte data){  // small hint: stuur = dutch for send
    Wire.beginTransmission(adres);
      Wire.send( reg); 
      Wire.send( data); 
     Wire.endTransmission();
}

have fun with it...

I care! :slight_smile: I want to drive 900 leds while reading 9 potentiometer inputs, for my project. It needs to be fast. I'm a programmer and this hardware stuff is pretty new to me but I'm learning. Any advice you could give me would be appreciated! This is what I want to build:

The sketch is very rough and not to scale. The final unit will be about 7 inches square and each stripe of leds will be 50 leds tall - 5 1-inch 10-led modules per stripe, 2 stripes per row. Each dark stripe down the middle is a touch-sensitive strip potentiometer. The stripes of leds need to animate similar to audio level indicators. The lone leds are position indicators. All output and input will be handled by software / serial, e.g. the device is dumb. I'd be happy with just one color but a 2-color led (that makes 3 colors) would be ideal.

I may end up going with smd leds to keep power consumption down. Ideally everything would run off the USB but I'm probably dreaming.

I'm researching led drivers and hope to use the ATMEGA168 / arduino as controller but not sure if just one is enough. I'm open to suggestions.

Update: promising links...

http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1824&appnote=en011636
http://itp.nyu.edu/~lpm248/advMicro/LED/

also Sparkfun makes a controller board for their 8x8 led matrix but high $

hi

there's a lot of information on using the MAX7313 here. Some students at MIT used it a few years ago to make a huge RGB-lighted disco floor.

It has a very Arduino-like design:

FTDI USB chip to an Atmega8 (!), which drives 16 MAX7313 chips, for a total of 192 RGB leds, each with intensity control.

On the downloads page you can get their excellent technical write-up, describing the code, as well as schematics and board files.

D

Actually I got my choice for this components from these guys...

I was also busy on building a disco-floor-like product, its sort of an interactive excersize floor for at schoolyards,

It works standalone on a PIC...
the advantage of using an arduino to interface the max7313 is that it saves you weeks of figuring out how to configure all the involved registers.

about the speed: with the PIC I managed to update the LED driver one milion times per minute or so... pretty fast :wink:
I think that your project is possible with the arduino, it might be a challenge though...

:slight_smile: I've been continuing research and I am going to try it with 8 multiplexed 7221's since there seems to be a lot of documentation and resources for the 7212/7219 and I'm kind of newbie, so the more pre-made stuff I can find the better.

I found this 7221 library for the arduino: http://www.wayoda.org/arduino/ledcontrol/index.html

I found some 7221's pretty cheap and they're on the way for experimenting.

What advantage is there to the 7313? Twice the leds?

Meanwhile I'm wondering if I'll have a decent refresh rate and will I have problems powering all these leds at once?

Thanks for your attention.

Hi phineus

I found some 7221's pretty cheap and they're on the way for experimenting.

I wish the chips were a bit cheaper, tough. 8 MAX 7221 at about 9 Euros each (in Germany) add up very quickly...

What advantage is there to the 7313? Twice the leds?

Actually 4 times the leds (64 vs.16 AFAIK) and you'll need only a single resistor for all the Leds.
With the 7213 you'd need 64 of them.
Downside: no PWN (which you'll probably don't need anyway for your project)

Meanwhile I'm wondering if I'll have a decent refresh rate and will I have problems powering all these leds at once?

refresh-rate :
Hard to say since we don't know anything about the type of input-signal you will be using, or if some extra processing has to be applied to the input-signal.
powering :
You should expect 160-200 mA for each 7221 with 64 Leds attached. You will not be able to drive more than two MAX7221 from USB-power. Get an external Powersupply for the leds (Pin V+ 19 on the MAX7221).

Eberhard

A small addition to my last post:
refresh-rate
The LedControl-Library was designed for easy usage and low memory footprint, not for maximum performance. But it would be easy to implement more efficient update strategies in your own code. If you need any help on this later just contact me..
Eberhard

Would I be better off with 7313's, like the disco floor uses? I would definitely need less of those for my project. Any drawbacks to 7313 vs 7221? And what's the maximum # of leds I could power at once from USB?

Thanks so much.

Hi phineus

Would I be better off with 7313's, like the disco floor uses? I would definitely need less of those for my project. Any drawbacks to 7313 vs 7221? And what's the maximum # of leds I could power at once from USB?

With one MAX7313 you can drive 16 Leds, with one MAX7221 you drive 64 Leds.
If I get your project-description right you will end up with 900Leds, that would be 15xMAX7221 or 57xMAX1313.

This will all get very expensive...

The maximum number of leds that can be driven from the usb-supply depends on the forward current of the Leds. The simple out-of-the-box Led will need something like 20mA.
The MAX7221 multiplexes the outputs, so there are never more than 8 Leds switched on at the same time.
This will add up to 160mA for one MAX7221. The USB-Specs allow a maximum of 500mA to be drawn from a single port. So with
2xMAX7221=320 mA+ Arduino-supplycurrent (60mA?)
you already getting near the limit.
You can save some power by using high-efficency Leds that draw less current, but they are probably not available in all colors.

But the powersupply-issue doesn't really count here, since switching Wallwarts are available that serve 2500mA for very little money (bought one for my foxboard for 15 Euro last week).
Eberhard

With one MAX7313 you can drive 16 Leds, with one MAX7221 you drive 64 Leds.

Is this mistyped or am I just confused? I thought from what you said earlier it was the other way around e.g. the 7313 was the more powerful chip.

Sorry mistyped,

64 leds for the 7221, 16 for the 7313

Eberhard