Building a 4x4x4 LED cube with PIC16F18877


PIC16F18877 LED cube code

The code is quite long, over 850 lines of code.

This article is more than two years old and might contain obsolete information; it is still kept here for informational purposes.

Or you can simply download the whole project.

Some explanations regarding the code

In MCC, the PIC16F18877 is configured to run on the internal clock, with a clock frequency of 4MHz and clock divider set to 1:4. This will give a 1µs execution time. Then, pins RB0 to RB3, RC0-RC7 and RD0-RD7 are set as digital outputs.


For each level of the cube, we have two uint_8 variables, one for the lower eight LEDs, the other for the upper eight LEDs. By the way I have made the connections, PORTC drives the lower LEDs, and the upper LEDs are driven by PORT D. The trick here is that I can do some fancy bit masking and then I can write directly to LATC and LATD registers, thus updating eight LEDs with only one code line.

As I said before, we will turn on only two LEDs at one time. In total, we will need 32 cycles to go through all the LEDs. But we have a constraint here; to obtain a flicker-free LED cube we need to update all the LEDs with a frequency of at least 25Hz. Thus, in the code, I have 100µs between each update. I could have done this with timers too, but I prefer to keep things simple.

Different LED effects are obtained by writing different values to the variables corresponding to each cube level, then we run the scan routine 50 times, which displays each pattern for about 0.5 seconds. Several pattern sequences are implemented by me, and, of course, one can add its own contribution.

In the meantime, you can see the cube in action:

Some final thoughts

  • A lot of time needed to do the soldering part. Very boring. It might be the only LED cube I’ll ever make.
  • I wonder if those LED cube kits are easier to make…
  • LEDs are not as bright as expected, mostly because I can’t turn on a whole level as I have to keep the current flowing through RB0-RB3 pins withing the electrical specifications of the microcontroller.
  • Writing the software is easy. It’s almost as simple as with Arduino IDE
1 2


  1. Avatar
    Vijay Muppidwar on

    Sir, please send me 5x5x5 led cube Proteus simulation circuit diagram of video using pic 16f877a and its source code on my email. [email removed by admin]
    Thank you.

Leave A Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.