I have coded a Display-Class to drive a X by Y LED-Multiplexing-Matrix with pi4j. However, I when driving a 4-by-4-Matrix I cannot get the framerate beyond roughly 30 fps, which still produces a noticeable flicker.

When running the benchmark included in the Git-Repository (please see results below), I notice that the framerate INCREASES when I have less GPIO-Pins to switch. Thus, I assume that there should be a more efficient way to switch those pins on and off. Also, I am pretty sure that more LEDs will DECREASE performace even more. In my final project I want to drive a 10 x 20 LED-Matrix, thus the performance is not acceptable!

I assume that the following code-segment needs improvement:

public void draw() {        
    while (isAlive) {            
        for (int x = 0; x < bitmap.length; x++) {
            //pull colPin to GND
            if (verbose) System.out.println(colOutPins[x].getName() + " turned on! Pin: " + colOutPins[x].getPin() + ", State: " + colOutPins[x].getState());

            LinkedList<GpioPinDigitalOutput> switchPinsList = new LinkedList<GpioPinDigitalOutput>();
            for (int y = 0; y < bitmap[0].length; y++) {
                if (bitmap[y][x]) {
                    //set rowPin high
                    //if (verbose) System.out.println(rowOutPins[y].getName() + " turned on! Pin: " + rowOutPins[y].getPin() + ", State: " + rowOutPins[y].getState());

                    //add pins to list

            //create array of pins to switch
            GpioPinDigitalOutput[] switchPinsArray = switchPinsList.toArray(new GpioPinDigitalOutput[switchPinsList.size()]);

            //switch pins on
            if (switchPinsArray.length > 0) {
                gpio.setState(PinState.HIGH, switchPinsArray);

                try {
                    if (verbose) System.out.println("Sleeping for " + scanDelay + " ms...");
                } catch (InterruptedException e) {

                //switch pins off
                gpio.setState(PinState.LOW, switchPinsArray);

            //push rowPin to HIGH

            if (verbose) System.out.println("=== RESET FOR NEXT SCAN ===");

        synchronized (this) {
            lastFrameTimeStamp = System.currentTimeMillis();

Any ideas how to code my class LedMatrixDisplayPi4j more efficient in order to increase performance? My next step will be to go for WiringPi without the pi4j layers in between, but I'd rather see a better performance with pi4j on top!

==> Source Code On GitHub

Starting Benchmark... Stand by!
All on, 1 fps: 
Target Framerate: 1.0fps 
Actual Framerate: 0.9528851244044468 fps
Total Frames: 9 frames 
Total Time: 9445 ms

Bitmap, 1 fps: 
Target Framerate: 1.0fps 
Actual Framerate: 1.023436700440078 fps
Total Frames: 10 frames 
Total Time: 9771 ms

Bitmap, 10 fps: 
Target Framerate: 10.0fps 
Actual Framerate: 7.21659817580435 fps
Total Frames: 72 frames 
Total Time: 9977 ms

Bitmap, 30 fps: 
Target Framerate: 30.0fps 
Actual Framerate: 15.543521861211392 fps
Total Frames: 155 frames 
Total Time: 9972 ms

Bitmap, 30 fps: 
Target Framerate: 60.0fps 
Actual Framerate: 22.61583108175723 fps
Total Frames: 226 frames 
Total Time: 9993 ms

Bitmap, 120 fps: 
Target Framerate: 120.0fps 
Actual Framerate: 27.56892230576441 fps
Total Frames: 275 frames 
Total Time: 9975 ms

All on, 120 fps: 
Target Framerate: 120.0fps 
Actual Framerate: 24.99498092752459 fps
Total Frames: 249 frames 
Total Time: 9962 ms

All Off, 120 fps: 
Target Framerate: 120.0fps 
Actual Framerate: 36.15061085519727 fps
Total Frames: 361 frames 
Total Time: 9986 ms


==> Source Code On GitHub

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.