0

16x16 DotStar maximum SPI rate
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

16x16 DotStar maximum SPI rate

by kla_kretzsche on Wed Nov 28, 2018 3:40 pm

Hey guys,

I'm trying to control a 16x16 DotStar matrix row by row very fast so the human eye sees steady picture. As µC I've changed from an Arduino Mega to an Arduino Due because there was a clearly visible flicker using the Mega. The Due seemed to be the solution, but I've got flicker as well. One run turning 16 rows on and off one after another takes about 33ms on the Due and 38ms on the Mega. So I think the µC is not the limiting factor here, but the SPI rate. Do you have any information or tip for me to speed the whole thing up?

Greets, kla_kretzsche

kla_kretzsche
 
Posts: 8
Joined: Wed Nov 28, 2018 3:26 pm

Re: 16x16 DotStar maximum SPI rate

by adafruit_support_carter on Wed Nov 28, 2018 4:07 pm

Are you using hardware or software SPI in your code?

adafruit_support_carter
 
Posts: 9792
Joined: Tue Nov 29, 2016 2:45 pm

Re: 16x16 DotStar maximum SPI rate

by kla_kretzsche on Thu Nov 29, 2018 8:08 am

I'm using hardware SPI.

kla_kretzsche
 
Posts: 8
Joined: Wed Nov 28, 2018 3:26 pm

Re: 16x16 DotStar maximum SPI rate

by adafruit_support_carter on Thu Nov 29, 2018 1:27 pm

The Due seemed to be the solution, but I've got flicker as well.

Can you clarify what you mean here. It sounds like it worked, but also didnt' work? What about this working was making you think it was the solution?

Can you link to what code you are currently try to use?

adafruit_support_carter
 
Posts: 9792
Joined: Tue Nov 29, 2016 2:45 pm

Re: 16x16 DotStar maximum SPI rate

by kla_kretzsche on Fri Nov 30, 2018 5:35 am

Sorry for the confusion, I thought it would be the solution because the clock frequency of the Due is much higher than the Mega. But the problem seems to be the speed of SPI and not the speed of executing the code.
Code: Select all | TOGGLE FULL SIZE
#include "Arduino.h"
#include "SPI.h"

#ifndef PSTR
#define PSTR // Make Arduino Due happy
#endif

#define nLEDS 256
#define MATRIXW 16
#define MATRIXH 16

byte colorStart = B11100000;
byte Brightness = 1; //0..31
byte globalBrightness = colorStart + Brightness;
int i = 0;

struct LED {
   byte r;
   byte g;
   byte b;
};
LED screen[nLEDS];

//----------------------------------------

void setup() {
   SPI.begin();
}

//----------------------------------------

void loop() {
   for (int l = 0; l < MATRIXW; l++) {
      setPixelColor((MATRIXW * i) + l, 255, 0, 0);
      if (i == 0) {
         setPixelColor((MATRIXW * 15) + l, 0, 0, 0);
      } else {
         setPixelColor((MATRIXW * (i - 1) + l), 0, 0, 0);
      }
   }
   sendScreen();
   i++;
   if (i == 16) {
      i = 0;
   }
}

//----------------------------------------

void APA102_Start() {
   SPI.transfer(0);
   SPI.transfer(0);
   SPI.transfer(0);
   SPI.transfer(0);
}

void APA102_Color(byte gb, byte r, byte g, byte b) {
   SPI.transfer(gb);
   SPI.transfer(b);
   SPI.transfer(g);
   SPI.transfer(r);
}

void APA102_Stop(int n) {
   for (int e = 0; e < n; e += 16) {
      SPI.transfer(B11111111);
   }
}

void setPixelColor(int index, byte r, byte g, byte b) {
   screen[index].r = r;
   screen[index].g = g;
   screen[index].b = b;
}

void sendScreen() {
   APA102_Start();
   for (int i = 0; i < nLEDS; i++) {
      APA102_Color(globalBrightness, screen[i].r, screen[i].g, screen[i].b);
   }
   APA102_Stop(nLEDS);
}

kla_kretzsche
 
Posts: 8
Joined: Wed Nov 28, 2018 3:26 pm

Re: 16x16 DotStar maximum SPI rate

by millercommamatt on Fri Nov 30, 2018 10:39 am

Turn up the SPI speeds. The default is nowhere near the max.

millercommamatt
 
Posts: 249
Joined: Tue Jul 31, 2018 4:57 pm

Re: 16x16 DotStar maximum SPI rate

by adafruit_support_carter on Fri Nov 30, 2018 12:23 pm

Try that and see what happens.

adafruit_support_carter
 
Posts: 9792
Joined: Tue Nov 29, 2016 2:45 pm

Re: 16x16 DotStar maximum SPI rate

by kla_kretzsche on Fri Nov 30, 2018 12:31 pm

I have already tried that with
Code: Select all | TOGGLE FULL SIZE
SPISettings settings = SPISettings(20000000, MSBFIRST, SPI_MODE0);

as declaration, and changing the sendScreen()-function as follows
Code: Select all | TOGGLE FULL SIZE
void sendScreen() {
  SPI.beginTransaction(settings);
  APA102_Start();
  for (int i = 0; i < nLEDS; i++) {
    APA102_Color(globalBrightness, screen[i].r, screen[i].g, screen[i].b);
  }
  APA102_Stop(nLEDS);
  SPI.endTransaction();
}

But if I go for more than 20000000 (20MHz) the LEDs just go wild.

kla_kretzsche
 
Posts: 8
Joined: Wed Nov 28, 2018 3:26 pm

Re: 16x16 DotStar maximum SPI rate

by adafruit_support_carter on Mon Dec 03, 2018 2:54 pm

They can only go so fast. Just to make sure, you are working with one of these?
https://www.adafruit.com/product/2735

What if you use the library?
https://learn.adafruit.com/adafruit-dot ... ix-library
instead of doing your own SPI transactions.

adafruit_support_carter
 
Posts: 9792
Joined: Tue Nov 29, 2016 2:45 pm

Re: 16x16 DotStar maximum SPI rate

by kla_kretzsche on Tue Dec 04, 2018 9:35 am

Right, that's the matrix I'm using. Do you mean 20MHz is the maximum for the SPI interface of the APA102 LEDs when you say "They can only go so fast."?
When using the DotStarMatrix Library I also get flicker. The time to show one picture (all 16 rows once) is about 41ms (so about 24 frames per second). I used the code bellow:
Code: Select all | TOGGLE FULL SIZE
#include <Adafruit_GFX.h>
#include <Adafruit_DotStarMatrix.h>
#include <Adafruit_DotStar.h>
#ifndef PSTR
#define PSTR // Make Arduino Due happy
#endif

#define MATRIXH 16
#define MATRIXW 16

Adafruit_DotStarMatrix matrix = Adafruit_DotStarMatrix(
  16, 16,
  DS_MATRIX_BOTTOM     + DS_MATRIX_LEFT +
  DS_MATRIX_ROWS + DS_MATRIX_ZIGZAG,
  DOTSTAR_BRG);

uint16_t def = matrix.Color(0, 0, 255);
uint16_t black = matrix.Color(0, 0, 0);

void setup() {
  // Serial.begin(115200);
  matrix.begin();
  matrix.setBrightness(20);
}

int i = 0;
long oldMicros = 0;

void loop() {
 
  for (int l = 0; l < MATRIXW; l++) {
    matrix.setPixelColor((MATRIXW * i) + l, def);
    if (i == 0) {
      matrix.setPixelColor((MATRIXW * 15) + l, black);
    } else {
      matrix.setPixelColor((MATRIXW * (i - 1) + l), black);
    }
  }
  matrix.show();

  i++;
  if (i == 16) {
    i = 0;
    /*
    Serial.println(micros() - oldMicros);
    oldMicros = micros();
   */
  }
}

kla_kretzsche
 
Posts: 8
Joined: Wed Nov 28, 2018 3:26 pm

Re: 16x16 DotStar maximum SPI rate

by adafruit_support_carter on Tue Dec 04, 2018 1:58 pm

Do you mean 20MHz is the maximum for the SPI interface of the APA102 LEDs when you say "They can only go so fast."?

See the data rate footnote here:
https://learn.adafruit.com/adafruit-dot ... pixels-1-3

Can you clarify the nature of the flicker you are seeing. Is it a flicker you can see if you just hold a steady value?

adafruit_support_carter
 
Posts: 9792
Joined: Tue Nov 29, 2016 2:45 pm

Re: 16x16 DotStar maximum SPI rate

by kla_kretzsche on Wed Dec 05, 2018 3:57 am

With flicker i mean, that it is clearly to see that the rows turn on and off individual. If I hold a steady value, like giving all 256 leds the same color value at the same time, there is nothing like that. So I think the problem here is the maximal data rate. Can you clarify which data rate I am getting now with the Arduino Due, because in the link to the specifications you attached the footnote says "8 MHz on Arduino, up to 32 MHz on Raspberry Pi". This means that there is no matter which Arduino Board I use regarding SPI data rate? And moreover, I interpret the Mhz as "bits per second", is that true?

kla_kretzsche
 
Posts: 8
Joined: Wed Nov 28, 2018 3:26 pm

Re: 16x16 DotStar maximum SPI rate

by adafruit2 on Wed Dec 05, 2018 1:42 pm

thats right, you really need an oscilloscope to verify the datarate though, arduino may have picked a different rate without warning you. we haven't ever used the due for this so no idea what the max is there.

adafruit2
Site Admin
 
Posts: 18047
Joined: Fri Mar 11, 2005 7:36 pm

Re: 16x16 DotStar maximum SPI rate

by kla_kretzsche on Tue Dec 11, 2018 5:45 am

Hi,

sorry for the late response. Yesterday I had the time to measure the SPI signal with the oscilloscope. I've measured a signal (10011001) with different SPI settings.

The first with 16MHz
16MHz_10011001.PNG
16MHz_10011001.PNG (28.79 KiB) Viewed 65 times


The second one with 20 MHz
20MHz_10011001_PulseWidth.PNG
20MHz_10011001_PulseWidth.PNG (28.83 KiB) Viewed 65 times


The third one with 32 MHz
32MHz_10011001_PulseWidth.PNG
32MHz_10011001_PulseWidth.PNG (29.11 KiB) Viewed 65 times


Clearly to see, that the output clock is always a divider (lower as the value in the settings) of the Arduino clock (84MHz).
But I really don't understand why more and more of the LEDs go wild (from the last LED to the beginning) if the clock in the settings is higher than 20MHz. The specs say, that the data rate can go up to 32MHz. The only problem I can imagine is the signals that becomes more and more blurred and not as sharp as in lower frequencies.

Thanks for the tips till now, I hope I can speed things up with your help.

kla_kretzsche
 
Posts: 8
Joined: Wed Nov 28, 2018 3:26 pm

Re: 16x16 DotStar maximum SPI rate

by kla_kretzsche on Tue Jan 08, 2019 10:08 am

Sadly no answer so I will push the topic again with this post. Maybe someone has an idea.

Furthermore, can someone explain why the maximum data rate is 8MHz on Arduino and up to 32MHz on Raspberry Pi? Because I am currently trying to light up the matrix with a Raspberry Pi.

kla_kretzsche
 
Posts: 8
Joined: Wed Nov 28, 2018 3:26 pm

Please be positive and constructive with your questions and comments.