# Code works on the Arduino but not on Spark

**URL:** <https://community.particle.io/t/code-works-on-the-arduino-but-not-on-spark/4560>\
**Category:** Troubleshooting\
**Created:** [May 23, 2014, 6:13pm UTC](https://community.particle.io/t/code-works-on-the-arduino-but-not-on-spark/4560 "2014-05-23T18:13:45Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![sammiol](https://avatars.discourse-cdn.com/v4/letter/s/439d5e/32.png) [@sammiol](https://community.particle.io/u/sammiol)\
**Post date:** [May 23, 2014, 6:13pm UTC](https://community.particle.io/t/code-works-on-the-arduino-but-not-on-spark/4560/1 "2014-05-23T18:13:45Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![kennethlimcp](https://avatars.discourse-cdn.com/v4/letter/k/c0e974/32.png) [@kennethlimcp](https://community.particle.io/u/kennethlimcp)\
**Post date:** [May 23, 2014, 6:40pm UTC](https://community.particle.io/t/code-works-on-the-arduino-but-not-on-spark/4560/2 "2014-05-23T18:40:13Z")

</div>

@sammiol,

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

---

<div class="post-metadata">

**Author:** ![peekay123](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/peekay123/32/810_2.png) [@peekay123](https://community.particle.io/u/peekay123)\
**Post date:** [May 23, 2014, 6:59pm UTC](https://community.particle.io/t/code-works-on-the-arduino-but-not-on-spark/4560/3 "2014-05-23T18:59:51Z")

</div>

@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 😄

---

<div class="post-metadata">

**Author:** ![sammiol](https://avatars.discourse-cdn.com/v4/letter/s/439d5e/32.png) [@sammiol](https://community.particle.io/u/sammiol)\
**Post date:** [May 23, 2014, 7:52pm UTC](https://community.particle.io/t/code-works-on-the-arduino-but-not-on-spark/4560/4 "2014-05-23T19:52:30Z")

</div>

@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 😃

---

<div class="post-metadata">

**Author:** ![peekay123](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/peekay123/32/810_2.png) [@peekay123](https://community.particle.io/u/peekay123)\
**Post date:** [May 23, 2014, 8:00pm UTC](https://community.particle.io/t/code-works-on-the-arduino-but-not-on-spark/4560/5 "2014-05-23T20:00:56Z")

</div>

> [@sammiol](#):
>
> DATA = DATA\<

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

---

<div class="post-metadata">

**Author:** ![sammiol](https://avatars.discourse-cdn.com/v4/letter/s/439d5e/32.png) [@sammiol](https://community.particle.io/u/sammiol)\
**Post date:** [May 23, 2014, 8:25pm UTC](https://community.particle.io/t/code-works-on-the-arduino-but-not-on-spark/4560/6 "2014-05-23T20:25:12Z")

</div>

@peekay123

[https://www.flickr.com/photos/andrew-d/8073604641/in/photostream/](https://www.flickr.com/photos/andrew-d/8073604641/in/photostream/)

[![Imgur](https://live.staticflickr.com/8033/8073604641_992be59c15_b.jpg "LED Matrix- Stupid Code") ](https://www.flickr.com/photos/andrew-d/8073604641/in/photostream/)

---

<div class="post-metadata">

**Author:** ![bko](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bko/32/20704_2.png) [@bko](https://community.particle.io/u/bko)\
**Post date:** [May 23, 2014, 8:36pm UTC](https://community.particle.io/t/code-works-on-the-arduino-but-not-on-spark/4560/7 "2014-05-23T20:36:27Z")

</div>

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](http://www.adafruit.com/products/870) so you could use those libraries. They have already been ported.

---

<div class="post-metadata">

**Author:** ![peekay123](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/peekay123/32/810_2.png) [@peekay123](https://community.particle.io/u/peekay123)\
**Post date:** [May 23, 2014, 8:52pm UTC](https://community.particle.io/t/code-works-on-the-arduino-but-not-on-spark/4560/8 "2014-05-23T20:52:09Z")

</div>

@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 😄

---

<div class="post-metadata">

**Author:** ![sammiol](https://avatars.discourse-cdn.com/v4/letter/s/439d5e/32.png) [@sammiol](https://community.particle.io/u/sammiol)\
**Post date:** [May 23, 2014, 9:12pm UTC](https://community.particle.io/t/code-works-on-the-arduino-but-not-on-spark/4560/9 "2014-05-23T21:12:32Z")

</div>

@peekay123

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

---

<div class="post-metadata">

**Author:** ![peekay123](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/peekay123/32/810_2.png) [@peekay123](https://community.particle.io/u/peekay123)\
**Post date:** [May 23, 2014, 9:18pm UTC](https://community.particle.io/t/code-works-on-the-arduino-but-not-on-spark/4560/10 "2014-05-23T21:18:34Z")

</div>

@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**](http://www.adafruit.com/products/757) 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);
      } 		
    }

```

---

<div class="post-metadata">

**Author:** ![sammiol](https://avatars.discourse-cdn.com/v4/letter/s/439d5e/32.png) [@sammiol](https://community.particle.io/u/sammiol)\
**Post date:** [May 23, 2014, 9:40pm UTC](https://community.particle.io/t/code-works-on-the-arduino-but-not-on-spark/4560/11 "2014-05-23T21:40:26Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![peekay123](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/peekay123/32/810_2.png) [@peekay123](https://community.particle.io/u/peekay123)\
**Post date:** [May 23, 2014, 9:48pm UTC](https://community.particle.io/t/code-works-on-the-arduino-but-not-on-spark/4560/12 "2014-05-23T21:48:00Z")

</div>

@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?
