Uno R4 Built-in LED Matrix Randomly Flickering

I'm working on a little project that takes the current temperature from a DHT11 temperature/humidity sensor and prints the temperature graphically to the built-in display.

It seems like every few updates, there will be one randomly flickering pixel. It gets super bright for a fraction of a second, and is a pretty unpleasant visual effect. Can anyone take a look at my code and suggest reasons why this might be happening? Or is it a hardware effect?

#include "Arduino_LED_Matrix.h" 
ArduinoLEDMatrix matrix;
#include "DHT.h"
#define DHTPIN A0
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

byte result[8][12] = {
  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};

byte zero[8][6] = {
  {0,0,0,0,0,0},
  {0,1,1,1,1,0},
  {0,1,0,0,1,0},
  {0,1,0,0,1,0},
  {0,1,0,0,1,0},
  {0,1,0,0,1,0},
  {0,1,1,1,1,0},
  {0,0,0,0,0,0}
};

byte one[8][6] = {
  {0,0,0,0,0,0},
  {0,0,1,0,0,0},
  {0,1,1,0,0,0},
  {0,0,1,0,0,0},
  {0,0,1,0,0,0},
  {0,0,1,0,0,0},
  {0,1,1,1,0,0},
  {0,0,0,0,0,0}
};

// Number 2
byte two[8][6] = {
  {0,0,0,0,0,0},
  {0,1,1,1,0,0},
  {0,0,0,1,0,0},
  {0,0,0,1,0,0},
  {0,0,1,0,0,0},
  {0,1,0,0,0,0},
  {0,1,1,1,1,0},
  {0,0,0,0,0,0}
};

// Number 3
byte three[8][6] = {
  {0,0,0,0,0,0},
  {0,1,1,1,0,0},
  {0,0,0,1,0,0},
  {0,0,1,1,0,0},
  {0,0,0,0,1,0},
  {0,0,0,0,1,0},
  {0,1,1,1,0,0},
  {0,0,0,0,0,0},
};

// Number 4
byte four[8][6] = {
  {0,0,0,0,0,0},
  {0,1,0,1,0,0},
  {0,1,0,1,0,0},
  {0,1,1,1,0,0},
  {0,0,0,1,0,0},
  {0,0,0,1,0,0},
  {0,0,0,1,0,0},
  {0,0,0,0,0,0}
};

// Number 5
byte five[8][6] = {
  {0,0,0,0,0,0},
  {0,1,1,1,1,0},
  {0,1,0,0,0,0},
  {0,1,1,1,0,0},
  {0,0,0,0,1,0},
  {0,0,0,0,1,0},
  {0,1,1,1,0,0},
  {0,0,0,0,0,0}
};

// Number 6
byte six[8][6] = {
  {0,0,0,0,0,0},
  {0,1,1,1,1,0},
  {0,1,0,0,0,0},
  {0,1,1,1,0,0},
  {0,1,0,0,1,0},
  {0,1,0,0,1,0},
  {0,1,1,1,0,0},
  {0,0,0,0,0,0}
};

// Number 7
byte seven[8][6] = {
  {0,0,0,0,0,0},
  {0,1,1,1,1,0},
  {0,0,0,0,1,0},
  {0,0,0,1,0,0},
  {0,0,1,0,0,0},
  {0,0,1,0,0,0},
  {0,0,1,0,0,0},
  {0,0,0,0,0,0}
};

// Number 8
byte eight[8][6] = {
  {0,0,0,0,0,0},
  {0,1,1,1,1,0},
  {0,1,0,0,1,0},
  {0,1,1,1,1,0},
  {0,1,0,0,1,0},
  {0,1,0,0,1,0},
  {0,1,1,1,1,0},
  {0,0,0,0,0,0}
};

// Number 9
byte nine[8][6] = {
  {0,0,0,0,0,0},
  {0,1,1,1,1,0},
  {0,1,0,0,1,0},
  {0,1,1,1,1,0},
  {0,0,0,0,1,0},
  {0,0,0,0,1,0},
  {0,1,1,1,0,0},
  {0,0,0,0,0,0}
};


byte blank[8][6] = {
  {0,0,0,0,0,0},
  {0,0,0,0,0,0},
  {0,0,0,0,0,0},
  {0,0,0,0,0,0},
  {0,0,0,0,0,0},
  {0,0,0,0,0,0},
  {0,0,0,0,0,0},
  {0,0,0,0,0,0}
};

// Subroutine to take two 8x6 bitmaps and combine them into an 8x12 frame
void combineDigits(byte result[8][12], byte digit1[8][6], byte digit2[8][6]) {
    for (int i = 0; i < 8; ++i) {
        for (int j = 0; j < 6; ++j) {
            result[i][j] = digit1[i][j];
        }
    }

    for (int i = 0; i < 8; ++i) {
        for (int j = 0; j < 6; ++j) {
            result[i][j + 6] = digit2[i][j];
        }
    }
}


// Define an arbitrary lastTemperature
int lastTemperature = 70;
// Subroutine to take a temperature and show it on the matrix
void displayTemperature(int temperature) {
  // Don't do anything unless the temperature has changed
  if (lastTemperature != temperature){
    // Separate the temperature digits
    int digit1 = temperature / 10;
    int digit2 = temperature % 10;

    // Define arrays for the digits
    byte digit1Array[8][6];
    byte digit2Array[8][6];

    // Array to index what digit corresponds to what array
    byte (*digitArrays[])[6] = {zero, one, two, three, four, five, six, seven, eight, nine};

    // Ensure digit1 and digit2 are within the valid range
    if (digit1 >= 0 && digit1 <= 9 && digit2 >= 0 && digit2 <= 9) {
      // Copy the digit arrays to digit1Array and digit2Array
      byte digit1Array[8][6];
      byte digit2Array[8][6];

      for (int i = 0; i < 8; ++i) {
          for (int j = 0; j < 6; ++j) {
              digit1Array[i][j] = digitArrays[digit1][i][j];
              digit2Array[i][j] = digitArrays[digit2][i][j];
          }
      }

    combineDigits(result, digit1Array, digit2Array);
    matrix.renderBitmap(result, 8, 12);
    }
  }
  lastTemperature = temperature;
}
  
void setup() {
    Serial.begin(9600);
    Serial.println(F("DHTxx test!"));
    dht.begin();
    matrix.begin();
}

void loop() {
    float h = dht.readHumidity();
    int t = dht.readTemperature(true); // The true parameter passed is to read as Fahrenheit, the default is Celsius

    // Display the temperature on the matrix
    displayTemperature(t);
    Serial.println(t);
    delay(2000);
}


I am not very familiar with the UNO R4 but I am inclined to think the problem is your code is being interrupted while writing the pixels. I am not sure if this is a good idea but try disabling the interrupts while writing the display. This could mess up tasks such as timers etc. This will allow you to test if this is correct.

Disabling interrupts made the R4 LED matrix not work at all... weird.

Here's a video of what I'm describing:

There is an interaction between the DHT library and the matrix display

Don't call dht.readHumidity() or dht.readTemperture() and set t to a random number and the problem does not occur.

However, if you do this

    int t = dht.readTemperature(true);  // The true parameter passed is to read as Fahrenheit, the default is Celsius
    t = random(100);

then the problem occurs. There must be a clash of resources or timing

That sounds very plausible

It sounds to me then, that this problem isn't easily alleviated without delving into the code for the DHT library? Any possible workarounds to try? Thanks Delta_G and UKHeliBob for your input.

Hello! Same issue here using a dht22..

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.