Code works on the Arduino but not on Spark

So, I’m trying to get numbers to show on my 8x8 LED display and it works when I use the Arduino, but when I flash it onto the Spark, all the lights go on and stays that way. Why is this?

@sammiol,

you might want to write a sample test code and blink 1 x LED 1st? :smiley:

@sammiol, though the Spark is VERY much like an Arduino, it isn’t one in all respects. Often, Arduino libraries will use very hardware specific code that will not work directly on the Spark. This is why you will see some members and the Elites “port” Arduino libraries. If you can point me to the library you arel trying to use, I would be glad to port for you if at all possible :smile:

@peekay123
I am using a max7219 8x8 LED matrix display, and this is the code I copied from online. (I am very new to coding).

unsigned char i;
unsigned char j;
/port definition/
int Max7219_pinCLK = D2;
int Max7219_pinCS = D1;
int Max7219_pinDIN = D0;

unsigned char disp1[38][8]={
{0x3C,0x42,0x42,0x42,0x42,0x42,0x42,0x3C},//0
{0x10,0x18,0x14,0x10,0x10,0x10,0x10,0x10},//1
{0x7E,0x2,0x2,0x7E,0x40,0x40,0x40,0x7E},//2
{0x3E,0x2,0x2,0x3E,0x2,0x2,0x3E,0x0},//3
{0x8,0x18,0x28,0x48,0xFE,0x8,0x8,0x8},//4
{0x3C,0x20,0x20,0x3C,0x4,0x4,0x3C,0x0},//5
{0x3C,0x20,0x20,0x3C,0x24,0x24,0x3C,0x0},//6
{0x3E,0x22,0x4,0x8,0x8,0x8,0x8,0x8},//7
{0x0,0x3E,0x22,0x22,0x3E,0x22,0x22,0x3E},//8
{0x3E,0x22,0x22,0x3E,0x2,0x2,0x2,0x3E},//9
{0x8,0x14,0x22,0x3E,0x22,0x22,0x22,0x22},//A
{0x3C,0x22,0x22,0x3E,0x22,0x22,0x3C,0x0},//B
{0x3C,0x40,0x40,0x40,0x40,0x40,0x3C,0x0},//C
{0x7C,0x42,0x42,0x42,0x42,0x42,0x7C,0x0},//D
{0x7C,0x40,0x40,0x7C,0x40,0x40,0x40,0x7C},//E
{0x7C,0x40,0x40,0x7C,0x40,0x40,0x40,0x40},//F
{0x3C,0x40,0x40,0x40,0x40,0x44,0x44,0x3C},//G
{0x44,0x44,0x44,0x7C,0x44,0x44,0x44,0x44},//H
{0x7C,0x10,0x10,0x10,0x10,0x10,0x10,0x7C},//I
{0x3C,0x8,0x8,0x8,0x8,0x8,0x48,0x30},//J
{0x0,0x24,0x28,0x30,0x20,0x30,0x28,0x24},//K
{0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x7C},//L
{0x81,0xC3,0xA5,0x99,0x81,0x81,0x81,0x81},//M
{0x0,0x42,0x62,0x52,0x4A,0x46,0x42,0x0},//N
{0x3C,0x42,0x42,0x42,0x42,0x42,0x42,0x3C},//O
{0x3C,0x22,0x22,0x22,0x3C,0x20,0x20,0x20},//P
{0x1C,0x22,0x22,0x22,0x22,0x26,0x22,0x1D},//Q
{0x3C,0x22,0x22,0x22,0x3C,0x24,0x22,0x21},//R
{0x0,0x1E,0x20,0x20,0x3E,0x2,0x2,0x3C},//S
{0x0,0x3E,0x8,0x8,0x8,0x8,0x8,0x8},//T
{0x42,0x42,0x42,0x42,0x42,0x42,0x22,0x1C},//U
{0x42,0x42,0x42,0x42,0x42,0x42,0x24,0x18},//V
{0x0,0x49,0x49,0x49,0x49,0x2A,0x1C,0x0},//W
{0x0,0x41,0x22,0x14,0x8,0x14,0x22,0x41},//X
{0x41,0x22,0x14,0x8,0x8,0x8,0x8,0x8},//Y
{0x0,0x7F,0x2,0x4,0x8,0x10,0x20,0x7F},//Z
{0x8,0x7F,0x49,0x49,0x7F,0x8,0x8,0x8},//Chinese character
{0xFE,0xBA,0x92,0xBA,0x92,0x9A,0xBA,0xFE},//Chinese character
};

void Write_Max7219_byte(unsigned char DATA)
{
unsigned char i;
digitalWrite(Max7219_pinCS,LOW);
for(i=8;i>=1;i–)
{
digitalWrite(Max7219_pinCLK,LOW);
digitalWrite(Max7219_pinDIN,DATA&0x80);//Obtain the MSB of the data
DATA = DATA<<1;
digitalWrite(Max7219_pinCLK,HIGH);
}
}

void Write_Max7219(unsigned char address,unsigned char dat)
{
digitalWrite(Max7219_pinCS,LOW);
Write_Max7219_byte(address); //
Write_Max7219_byte(dat); //
digitalWrite(Max7219_pinCS,HIGH);
}

void Init_MAX7219(void)
{
Write_Max7219(0x09, 0x00); //decode: BCD
Write_Max7219(0x0a, 0x03); //Brightness
Write_Max7219(0x0b, 0x07); //
Write_Max7219(0x0c, 0x01); //
Write_Max7219(0x0f, 0x00); //
}

void setup()
{

pinMode(Max7219_pinCLK,OUTPUT);
pinMode(Max7219_pinCS,OUTPUT);
pinMode(Max7219_pinDIN,OUTPUT);
delay(50);
Init_MAX7219();
}

void loop()
{
for(j=0;j<38;j++)
{
for(i=1;i<9;i++)
Write_Max7219(i,disp1[j][i-1]);
delay(500);
}
}

If you could explain to me what this code is doing without all the coding jargon, I would appreciate it :smiley:

There is code missing here. Can you copy-paste the link to the site you got this code from?

@peekay123

https://www.flickr.com/photos/andrew-d/8073604641/in/photostream/

Imgur

This is an i2c LED driver so the first thing to say is that you need pull-ups on your clock and data lines for the Spark. Arduino typically does not need them but Spark does.

This is also the same IC that is used on the Adafruit Matrix LED boards so you could use those libraries. They have already been ported.

@bko, I think the MAX7219 can use SPI but is not I2C. Adafruit refers to the MAX7219 but does not use it in the produce referenced.

@sammiol, I need a link to the code you downloaded, although I like your picture :smile:

1 Like

@peekay123

Click the link that’s there. I forgot to mention that it’s in the comments section, haha.

@sammiol, I found the code! You were missing some lines in the code you pasted above. Also, the pin defintions were for the Arduino UNO and needed to be changed for the Spark. HOWEVER, more importantly is that the MAX7219 works at 5V while the Spark uses 3.3V. So a direction connection between the two will not work and you will need a level shifter which converts the 3.3V signals to 5V signals for the MAX7219. You can use something like this from Adafruit.

Here is the corrected code. The pin connections are as follows (through the level shifter of course):

//    Spark    MAX7219
//    D4       CLK
//    D3       CS
//    D2       DIN


     unsigned char i;
     unsigned char j; 
    /*Port Definitions*/
    int Max7219_pinCLK = D4;
    int Max7219_pinCS = D3;
    int Max7219_pinDIN = D2;
     
    unsigned char disp1[38][8]={
    {0x3C,0x42,0x42,0x42,0x42,0x42,0x42,0x3C},//0
    {0x10,0x18,0x14,0x10,0x10,0x10,0x10,0x10},//1
    {0x7E,0x2,0x2,0x7E,0x40,0x40,0x40,0x7E},//2
    {0x3E,0x2,0x2,0x3E,0x2,0x2,0x3E,0x0},//3
    {0x8,0x18,0x28,0x48,0xFE,0x8,0x8,0x8},//4
    {0x3C,0x20,0x20,0x3C,0x4,0x4,0x3C,0x0},//5
    {0x3C,0x20,0x20,0x3C,0x24,0x24,0x3C,0x0},//6
    {0x3E,0x22,0x4,0x8,0x8,0x8,0x8,0x8},//7
    {0x0,0x3E,0x22,0x22,0x3E,0x22,0x22,0x3E},//8
    {0x3E,0x22,0x22,0x3E,0x2,0x2,0x2,0x3E},//9
    {0x8,0x14,0x22,0x3E,0x22,0x22,0x22,0x22},//A
    {0x3C,0x22,0x22,0x3E,0x22,0x22,0x3C,0x0},//B
    {0x3C,0x40,0x40,0x40,0x40,0x40,0x3C,0x0},//C
    {0x7C,0x42,0x42,0x42,0x42,0x42,0x7C,0x0},//D
    {0x7C,0x40,0x40,0x7C,0x40,0x40,0x40,0x7C},//E
    {0x7C,0x40,0x40,0x7C,0x40,0x40,0x40,0x40},//F
    {0x3C,0x40,0x40,0x40,0x40,0x44,0x44,0x3C},//G
    {0x44,0x44,0x44,0x7C,0x44,0x44,0x44,0x44},//H
    {0x7C,0x10,0x10,0x10,0x10,0x10,0x10,0x7C},//I
    {0x3C,0x8,0x8,0x8,0x8,0x8,0x48,0x30},//J
    {0x0,0x24,0x28,0x30,0x20,0x30,0x28,0x24},//K
    {0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x7C},//L
    {0x81,0xC3,0xA5,0x99,0x81,0x81,0x81,0x81},//M
    {0x0,0x42,0x62,0x52,0x4A,0x46,0x42,0x0},//N
    {0x3C,0x42,0x42,0x42,0x42,0x42,0x42,0x3C},//O
    {0x3C,0x22,0x22,0x22,0x3C,0x20,0x20,0x20},//P
    {0x1C,0x22,0x22,0x22,0x22,0x26,0x22,0x1D},//Q
    {0x3C,0x22,0x22,0x22,0x3C,0x24,0x22,0x21},//R
    {0x0,0x1E,0x20,0x20,0x3E,0x2,0x2,0x3C},//S
    {0x0,0x3E,0x8,0x8,0x8,0x8,0x8,0x8},//T
    {0x42,0x42,0x42,0x42,0x42,0x42,0x22,0x1C},//U
    {0x42,0x42,0x42,0x42,0x42,0x42,0x24,0x18},//V
    {0x0,0x49,0x49,0x49,0x49,0x2A,0x1C,0x0},//W
    {0x0,0x41,0x22,0x14,0x8,0x14,0x22,0x41},//X
    {0x41,0x22,0x14,0x8,0x8,0x8,0x8,0x8},//Y
    {0x0,0x7F,0x2,0x4,0x8,0x10,0x20,0x7F},//Z
    };
     
     
     
    void Write_Max7219_byte(unsigned char DATA) 
    {   
                unsigned char i;
    	    digitalWrite(Max7219_pinCS,LOW);		
    	    for(i=8;i>=1;i--)
              {		  
                 digitalWrite(Max7219_pinCLK,LOW);
                 digitalWrite(Max7219_pinDIN,DATA&0x80);// Extracting a bit data
                 DATA = DATA<<1;
                 digitalWrite(Max7219_pinCLK,HIGH);
               }                                 
    }
     
     
    void Write_Max7219(unsigned char address,unsigned char dat)
    {
            digitalWrite(Max7219_pinCS,LOW);
            Write_Max7219_byte(address);           //address,code of LED
            Write_Max7219_byte(dat);               //data,figure on LED 
            digitalWrite(Max7219_pinCS,HIGH);
    }
     
    void Init_MAX7219(void)
    {
     Write_Max7219(0x09, 0x00);       //decoding :BCD
     Write_Max7219(0x0a, 0x03);       //brightness 
     Write_Max7219(0x0b, 0x07);       //scanlimit;8 LEDs
     Write_Max7219(0x0c, 0x01);       //power-down mode:0,normal mode:1
     Write_Max7219(0x0f, 0x00);       //test display:1;EOT,display:0
    }
     
     
     
    void setup()
    {
     
      pinMode(Max7219_pinCLK,OUTPUT);
      pinMode(Max7219_pinCS,OUTPUT);
      pinMode(Max7219_pinDIN,OUTPUT);
      delay(50);
      Init_MAX7219();
    }
     
     
    void loop()
    { 
       for(j=0;j<38;j++)
      {
       for(i=1;i<9;i++)
        Write_Max7219(i,disp1[j][i-1]);
       delay(500);
      } 		
    }
2 Likes

@peekay123

Instead of using the level shifter, can I use the 5V power source from the arduino? Because I just tried that and it still doesn’t work.

@sammiol, if you power the Matrix from the Vin pin on the Spark, that will get you 5V assuming you have the USB connected.

The problem is that the Spark works at 3.3V and the digital pins only put out 3.3V and that is not enough for the MAX7219 inputs. That is why you need a level shifter. Some folks on other forums have said that they have it working without a level shifter but that may not be the case for you. So, let’s review you connections. ASSUMPTION - the Spark is powered via USB

Spark     MAX7219 Module
Vin       Vcc
GND       GND
D2        DIN
D3        CS
D4        CLK

Is this how it is connected?