Works perfectly on UNO but garbage on NANO

I've been trying to control a series of Led Matrix with (MAX7219) with the SPI version of LedControl. It does work flawlessly on a UNO, but exactly the same code on a NANO I get lots of erros.

Also with the UNO I can have SPI at 10MHz but in the Nano I can go beyond 5MHz

Video Leds on Nano
Video Leds on UNO

Any Ideas why and how to fix this?

Code

#include "LedControl.h"
#include "lcd.h" 

uint8_t DIN = 11;
uint8_t CS = 9;
uint8_t CLK = 13;

#define NUMBER_OF_MATRIX_ARRAYS 36

LedControl lc=LedControl(CS, NUMBER_OF_MATRIX_ARRAYS);


/**********************************************************************/
void setup() {
  Serial.begin(115200);
  
  for (uint8_t i = 0; i < NUMBER_OF_MATRIX_ARRAYS; i++) {
    lc.shutdown(i, false);
    lc.setIntensity(i, 0);
    lc.clearDisplay(i);
  }

}



/**********************************************************************/
void PrintChar(uint8_t DisplayNumber, char s) {
  char inArray[8];

  for (uint8_t i=0; i<8; i++)
    inArray[i] =  pgm_read_byte(&font8x8[s][i]);
 
  for(uint8_t i = 0; i < 8; i++) {
   lc.setRow (DisplayNumber, i, inArray[i]);
  }

}


/*************************************************************************/
void loop() {
  char DataString[] = "00ABCDEFGHIJKLMNOPQRSTUVWXZ012345678";
  char ClearString[] = "                                    ";
  
  for (uint8_t j = 0; j < NUMBER_OF_MATRIX_ARRAYS; j++) 
     PrintChar(j , DataString[j]);

  delay(500);

  for (uint8_t j = 0; j < NUMBER_OF_MATRIX_ARRAYS; j++) 
     PrintChar(j , ClearString[j]);
  
  delay(500);

}

What is the Uno and what is the Nano do you use?
Could you please show the photos?

Welcome to the forum.

Thank you for the videos and the full sketch. That is very helpful.
We like to know more.
How do you power the leds ? What is "lcd.h" ? How are the led modules organized ? Do you use a old version of LedControl ?

In a simulation, there is enough power and there are no lower voltage levels.
I tried to copy your project in Wokwi simulation, but I got stuck: garbage-on-nano.ino - Wokwi Arduino and ESP32 Simulator

I'm more interseted in what you mean by this than anything else.
Do you mean you can't go beyond 5mHz?

I'm not sure why this would happen unless you've got a dodgy/wrong SPI connection on the Nano. Have you tried a Mega?

Where is DIN and CLK used? Is that contending with the Nano inbuilt compiler settings but not the Uno's?

Hi Koepel

I tried to power the leds from the Arduinos, connected only by USB, but also tried with an external 5V power source without USB connected. Same result.

lcd.h is the font:

const static char PROGMEM font8x8[128][8] = {
  { B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000 },   // 0 (NUL)
  { B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000 },   // 1 (SOH)
  { B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000 },   // 2 (STX)
  { B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000 },   // 3 (ETX)
  { B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000 },   // 4 (EOT)
  { B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000 },   // 5 (ENQ)
  { B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000 },   // 6 (ACK)
  { B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000 },   // 7 (BEL)
  { B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000 },   // 8 (BS)
  { B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000 },   // 9 (TAB)
  { B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000 },   // 10 (CR)
  { B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000 },   // 11 (VT)
  { B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000 },   // 12 (FF)
  { B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000 },   // 13 (LF)
  { B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000 },   // 14 (SO)
  { B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000 },   // 15 (SO)
  { B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000 },   // 16 (DLE)
  { B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000 },   // 17 (DC1)
  { B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000 },   // 18 (DC2)
  { B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000 },   // 19 (DC3)
  { B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000 },   // 20 (DC4)
  { B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000 },   // 21 (NAK)
  { B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000 },   // 22 (SYN)
  { B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000 },   // 23 (ETB)
  { B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000 },   // 24 (CAN)
  { B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000 },   // 25 (EM)
  { B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000 },   // 26 (SUB)
  { B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000 },   // 27 (ESC)
  { B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000 },   // 28 (FS)
  { B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000 },   // 29 (GS)
  { B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000 },   // 30 (RS)
  { B00000000, B00000000, B00000000, B00000000, B01100000, B11110000, B11110000, B01100000 },   // 31 (US)
  { B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000 },   // 32 (SPACE)
  { B00010000, B00010000, B00010000, B00010000, B00010000, B00000000, B00010000, B00000000 },   // 33 (!)
  { B00101000, B00101000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000 },   // 34 (")
  { B00101000, B00101000, B01111100, B00101000, B01111100, B00101000, B00101000, B00000000 },   // 35 (#)
  { B00010000, B00111100, B01010000, B00111000, B00010100, B01111000, B00010000, B00000000 },   // 36 ($)
  { B01100000, B01100100, B00001000, B00010000, B00100000, B01001100, B00001100, B00000000 },   // 37 (%)
  { B00110000, B01001000, B01010000, B00100000, B01010100, B01001000, B00110100, B00000000 },   // 38 (&)
  { B00110000, B00010000, B00100000, B00000000, B00000000, B00000000, B00000000, B00000000 },   // 39 (')
  { B00001000, B00010000, B00100000, B00100000, B00100000, B00010000, B00001000, B00000000 },   // 40 (()
  { B00100000, B00010000, B00001000, B00001000, B00001000, B00010000, B00100000, B00000000 },   // 41 ())
  { B00000000, B00010000, B01010100, B00111000, B01010100, B00010000, B00000000, B00000000 },   // 42 (*)
  { B00000000, B00010000, B00010000, B01111100, B00010000, B00010000, B00000000, B00000000 },   // 43 (+)
  { B00000000, B00000000, B00000000, B00000000, B00110000, B00010000, B00100000, B00000000 },   // 44 (,)
  { B00000000, B00000000, B00000000, B01111100, B00000000, B00000000, B00000000, B00000000 },   // 45 (-)
  { B00000000, B00000000, B00000000, B00000000, B00000000, B00110000, B00110000, B00000000 },   // 46 (.)
  { B00000000, B00000100, B00001000, B00010000, B00100000, B01000000, B00000000, B00000000 },   // 47 (/)
  { B00111000, B01000100, B01000100, B01000100, B01000100, B01000100, B00111000, B00000000 },   // 48 (0)
  { B00010000, B00110000, B00010000, B00010000, B00010000, B00010000, B00111000, B00000000 },   // 49 (1)
  { B00111000, B01000100, B00000100, B00001000, B00010000, B00100000, B01111100, B00000000 },   // 50 (2)
  { B01111100, B00001000, B00010000, B00001000, B00000100, B01000100, B00111000, B00000000 },   // 51 (3)
  { B00001000, B00011000, B00101000, B01001000, B01111100, B00001000, B00001000, B00000000 },   // 52 (4)
  { B01111100, B01000000, B01111000, B00000100, B00000100, B01000100, B00111000, B00000000 },   // 53 (5)
  { B00011000, B00100000, B01000000, B01111000, B01000100, B01000100, B00111000, B00000000 },   // 54 (6)
  { B01111100, B00000100, B00001000, B00010000, B00100000, B00100000, B00100000, B00000000 },   // 55 (7)
  { B00111000, B01000100, B01000100, B00111000, B01000100, B01000100, B00111000, B00000000 },   // 56 (8)
  { B00111000, B01000100, B01000100, B00111100, B00000100, B00001000, B00110000, B00000000 },   // 57 (9)
  { B00000000, B00110000, B00110000, B00000000, B00110000, B00110000, B00000000, B00000000 },   // 58 (:)
  { B00000000, B00110000, B00110000, B00000000, B00110000, B00010000, B00100000, B00000000 },   // 59 (;)
  { B00001000, B00010000, B00100000, B01000000, B00100000, B00010000, B00001000, B00000000 },   // 60 (<)
  { B00000000, B00000000, B01111100, B00000000, B01111100, B00000000, B00000000, B00000000 },   // 61 (=)
  { B00100000, B00010000, B00001000, B00000100, B00001000, B00010000, B00100000, B00000000 },   // 62 (>)
  { B00111000, B01000100, B00000100, B00001000, B00010000, B00000000, B00010000, B00000000 },   // 63 (?)
  { B00111000, B01000100, B00000100, B00110100, B01010100, B01010100, B00111000, B00000000 },   // 64 (@)
  { B00111000, B01000100, B01000100, B01000100, B01111100, B01000100, B01000100, B00000000 },   // 65 (A)
  { B01111000, B01000100, B01000100, B01111000, B01000100, B01000100, B01111000, B00000000 },   // 66 (B)
  { B00111000, B01000100, B01000000, B01000000, B01000000, B01000100, B00111000, B00000000 },   // 67 (C)
  { B01110000, B01001000, B01000100, B01000100, B01000100, B01001000, B01110000, B00000000 },   // 68 (D)
  { B01111100, B01000000, B01000000, B01111000, B01000000, B01000000, B01111100, B00000000 },   // 69 (E)
  { B01111100, B01000000, B01000000, B01111000, B01000000, B01000000, B01000000, B00000000 },   // 70 (F)
  { B00111000, B01000100, B01000000, B01011100, B01000100, B01000100, B00111100, B00000000 },   // 71 (G)
  { B01000100, B01000100, B01000100, B01111100, B01000100, B01000100, B01000100, B00000000 },   // 72 (H)
  { B00111000, B00010000, B00010000, B00010000, B00010000, B00010000, B00111000, B00000000 },   // 73 (I)
  { B00011100, B00001000, B00001000, B00001000, B00001000, B01001000, B00110000, B00000000 },   // 74 (J)
  { B01000100, B01001000, B01010000, B01100000, B01010000, B01001000, B01000100, B00000000 },   // 75 (K)
  { B01000000, B01000000, B01000000, B01000000, B01000000, B01000000, B01111100, B00000000 },   // 76 (L)
  { B01000100, B01101100, B01010100, B01010100, B01000100, B01000100, B01000100, B00000000 },   // 77 (M)
  { B01000100, B01000100, B01100100, B01010100, B01001100, B01000100, B01000100, B00000000 },   // 78 (N)
  { B00111000, B01000100, B01000100, B01000100, B01000100, B01000100, B00111000, B00000000 },   // 79 (O)
  { B01111000, B01000100, B01000100, B01111000, B01000000, B01000000, B01000000, B00000000 },   // 80 (P)
  { B00111000, B01000100, B01000100, B01000100, B01010100, B01001000, B00110100, B00000000 },   // 81 (Q)
  { B01111000, B01000100, B01000100, B01111000, B01010000, B01001000, B01000100, B00000000 },   // 82 (R)
  { B00111100, B01000000, B01000000, B00111000, B00000100, B00000100, B01111000, B00000000 },   // 83 (S)
  { B01111100, B00010000, B00010000, B00010000, B00010000, B00010000, B00010000, B00000000 },   // 84 (T)
  { B01000100, B01000100, B01000100, B01000100, B01000100, B01000100, B00111000, B00000000 },   // 85 (U)
  { B01000100, B01000100, B01000100, B01000100, B01000100, B00101000, B00010000, B00000000 },   // 86 (V)
  { B01000100, B01000100, B01000100, B01010100, B01010100, B01010100, B00101000, B00000000 },   // 87 (W)
  { B01000100, B01000100, B00101000, B00010000, B00101000, B01000100, B01000100, B00000000 },   // 88 (X)
  { B01000100, B01000100, B01000100, B00101000, B00010000, B00010000, B00010000, B00000000 },   // 89 (Y)
  { B01111100, B00000100, B00001000, B00010000, B00100000, B01000000, B01111100, B00000000 },   // 90 (Z)
  { B00111000, B00100000, B00100000, B00100000, B00100000, B00100000, B00111000, B00000000 },   // 91 ([)
  { B00000000, B01000000, B00100000, B00010000, B00001000, B00000100, B00000000, B00000000 },   // 92 (\)
  { B00111000, B00001000, B00001000, B00001000, B00001000, B00001000, B00111000, B00000000 },   // 93 (])
  { B00010000, B00101000, B01000100, B00000000, B00000000, B00000000, B00000000, B00000000 },   // 94 (^)
  { B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B01111100, B00000000 },   // 95 (_)
  { B00100000, B00010000, B00001000, B00000000, B00000000, B00000000, B00000000, B00000000 },   // 96 (`)
  { B00000000, B00000000, B00111000, B00000100, B00111100, B01000100, B00111100, B00000000 },   // 97 (a)
  { B01000000, B01000000, B01011000, B01100100, B01000100, B01000100, B01111000, B00000000 },   // 98 (b)
  { B00000000, B00000000, B00111000, B01000000, B01000000, B01000100, B00111000, B00000000 },   // 99 (c)
  { B00000100, B00000100, B00110100, B01001100, B01000100, B01000100, B00111100, B00000000 },   // 100 (d)
  { B00000000, B00000000, B00111000, B01000100, B01111100, B01000000, B00111000, B00000000 },   // 101 (e)
  { B00011000, B00100100, B00100000, B01110000, B00100000, B00100000, B00100000, B00000000 },   // 102 (f)
  { B00000000, B00111100, B01000100, B01000100, B00111100, B00000100, B00111000, B00000000 },   // 103 (g)
  { B01000000, B01000000, B01011000, B01100100, B01000100, B01000100, B01000100, B00000000 },   // 104 (h)
  { B00010000, B00000000, B00110000, B00010000, B00010000, B00010000, B00111000, B00000000 },   // 105 (i)
  { B00001000, B00000000, B00011000, B00001000, B00001000, B01001000, B00110000, B00000000 },   // 106 (j)
  { B01000000, B01000000, B01001000, B01010000, B01100000, B01010000, B01001000, B00000000 },   // 107 (k)
  { B00110000, B00010000, B00010000, B00010000, B00010000, B00010000, B00111000, B00000000 },   // 108 (l)
  { B00000000, B00000000, B01101000, B01010100, B01010100, B01000100, B01000100, B00000000 },   // 109 (m)
  { B00000000, B00000000, B01011000, B01100100, B01000100, B01000100, B01000100, B00000000 },   // 110 (n)
  { B00000000, B00000000, B00111000, B01000100, B01000100, B01000100, B00111000, B00000000 },   // 111 (o)
  { B00000000, B00000000, B01111000, B01000100, B01111000, B01000000, B01000000, B00000000 },   // 112 (p)
  { B00000000, B00000000, B00110100, B01001100, B00111100, B00000100, B00000100, B00000000 },   // 113 (q)
  { B00000000, B00000000, B01011000, B01100100, B01000000, B01000000, B01000000, B00000000 },   // 114 (r)
  { B00000000, B00000000, B00111000, B01000000, B00111000, B00000100, B01111000, B00000000 },   // 115 (s)
  { B00100000, B00100000, B01110000, B00100000, B00100000, B00100100, B00011000, B00000000 },   // 116 (t)
  { B00000000, B00000000, B01000100, B01000100, B01000100, B01001100, B00110100, B00000000 },   // 117 (u)
  { B00000000, B00000000, B01000100, B01000100, B01000100, B00101000, B00010000, B00000000 },   // 118 (v)
  { B00000000, B00000000, B01000100, B01000100, B01010100, B01010100, B00101000, B00000000 },   // 119 (w)
  { B00000000, B00000000, B01000100, B00101000, B00010000, B00101000, B01000100, B00000000 },   // 120 (x)
  { B00000000, B00000000, B01000100, B01000100, B00111100, B00000100, B00111000, B00000000 },   // 121 (y)
  { B00000000, B00000000, B01111100, B00001000, B00010000, B00100000, B01111100, B00000000 },   // 122 (z)
  { B00001000, B00010000, B00010000, B00100000, B00010000, B00010000, B00001000, B00000000 },   // 123 ({)
  { B00010000, B00010000, B00010000, B00010000, B00010000, B00010000, B00010000, B00000000 },   // 124 (|)
  { B00100000, B00010000, B00010000, B00001000, B00010000, B00010000, B00100000, B00000000 },   // 125 (})
  { B00000000, B00000000, B00000000, B00110100, B01001000, B00000000, B00000000, B00000000 },   // 126 (~)
  { B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000 }    // 127 ( )
};


Hi outbackhut,

In the UNO I can use SPI at 10MHz, but in the Nano, only 5MHz and below. At 10Mhz the Nano doesn't work at all.

DIN and CLK are D11 and D13. Same on both.

Hi b707

UNO - Sorry about the dust. It's 5+ years old and have been through a lot :grinning:
Nano Generic

It is very strange because the Uno and Nano uses the same controller. The only difference between the both is a bootloader, but it shouldn't affect the SPI frequency.
I think that one of your board is fake or damaged.

One difference on a Nano is the way the onboard LED is wired. On an UNO is it driven through a buffer chip, on the Nano it is wired directly to D13 (D13 > resistor > LED > GND).

Do you have a Nano with an 8MHz crystal/resonator, or did you at any time change the clock frequency?

please link to that exact library. The "original" LedControl doesn't support Hardware SPI.

If you want to try my Arduino Noiasca LED Control with print method (MAX7219/MAX7221) - that should support Hardware SPI.

A Nano should be able to do everything a Uno does. Are you absolutely totally sure that you've got the Nano wired properly?

The subtlety lies in the Nano Vs Uno wiring for D13, which unfortunately is used in the SPI implementation. So that's where the OP must focus, as @david_2018 quite correctly identified.

My attention has:

  • The boards do not have the same USB cable, so one cable could be cheap and with more voltage drop.
  • The Nano board has a diode from USB power to 5V. It runs at 4.5V.
  • The led on the Nano board nibbles a little from the current at pin 13, which is not the case on the Uno, but I don't see that as a problem.
  • The Nano board could be a clone, with missing decoupling capacitors.

Thanks everyone for your sugestions.

[noiasca] I used LedControl_HW_SPI for SPI

[Koepel] Tried with several USB cables and ports. It behave diferent from one to another. So part of the problem can be that. When power with an external power it behaves better, but not perfect.

[Koepel] I didn't know the nano has a diode from USB power to 5V. That is a major issue since the Max7219 works on 5V and adding some voltage drop from the cables makes the diference.

Just mesaured the voltage, and at the end of the series and it drops about 1.2v depending on the number a powered on leds. With 5v it ends with 3.9, but powered only with USB it drops to 3.7v. way to low.

So I restarted all tests with external power, a short cable to the led matrix and a new nano. The problem is even worse. Now I only have on digit working.

Meanwhile tried noiasca library and it seems to work well in all conditions, but only for 8 matrix. I'll try to change the library to support more leds.

In a simulation, the result is the same for a Uno or a Nano. I have updated the Wokwi project with the "LedControl_HW_SPI" library. Is it supposed to work like this ? garbage-on-nano.ino - Wokwi Arduino and ESP32 Simulator
That library is old. The compiler has changed to a newer version and more optimizations are turned on. Perhaps the library does not work well with the newest compiler optimizations.

If you have the noiasca library working, please show the sketch and the (changed) library. If I have time, then I can try to make that in Wokwi as well. Then we have something to compare.

The Wokwi simulator has examples here: wokwi-max7219-matrix Dot Matrix Reference | Wokwi Docs
You could check those examples to see which libraries they use.

Doesn't make sense. It can't be 10Mhz or 5Mhz.
SPI of an Uno can only be half it's 16Mhz clock, or a divisor of that, so 8Mhz, 4Mhz, etc.
The classic Nano uses the same chip and crystal, so should be exactly the same.
The default SPI frequency is AFAIK 4Mhz.

An Uno and a Nano can't power that many LEDs through USB, so you always have to use external power. Question is if you did connect external 5volt to V-in of the Nano (wrong) or to the 5volt pin of the Nano. You should have added a wiring diagram.
Leo..

After a lot more hours trying to undestand the problem, following your sugestions I think I understand better the problem. Unfortunally I don't have a solution yet.

  1. Like [Koepel] stated, in the UNO it works better because in the Nano there is a diode from the USB to the 5v, making the VCC only 4.5 and not 5v. In the Mega or UNO we have 5V so it runs better.

  2. The second problem is with the MAX7129 itself. When it receives garbage (from the lack of voltage for example) it seems that it goes into some error state that dosent disapear even with the power off. It takes a while to recover.

  3. One of the tests I made was to switch a working Nano with another one with se same code. Nano 1 always work while Nano 2 never work. Exactly the same thing that append with the Uno.
    Could it be that the SPI remains on an error state even after power off?

  4. I think the above problem can probably be fixed with proper reset procedures from the libraries. The ledcontrol.h library doesn't recover from the errors. MD_MAX72xx seems to do that better. Going to do some tests.

I don't see yet how 4.5volt or 5volt or the LED on pin13 could make a difference.
How do you power the Nano and the display.
Link to the exact supply please (voltage, current), and how/where you have connected it to the Nano. Breadboard, wires, etc. Details matter.

IIRC, from a similar build I did, each display block could draw 150-200mA.
I saw 28 blocks, so your supply should be capable of delivering more than 5Amp
Leo..

The SCK line is also the onboard LED, and is driving the LED AND all the 7219 device inputs; one 7219 is no big deal, but 29, that adds up. That's what I was getting at, the Nano doesn't have a buffer for the LED, the Uno does, so the electrical performance is going to be quite different.
But the bigger picture is the power demand of 28 LED matrices and their 7219s. Surprised the OP isn't searching for a new Arduino.

Yes, I know that the Nano doesn't have a LED buffer, and that the LED draws about 3mA.
Not convinced yet that the LED causes the problem until I have seen the whole setup.
In another project I controlled 24 TPIC chips with SPI from a Nano, without issues.
Leo..