How To Make An Arduino-powered LED Matrix Conway’s Game Of Life – DIY

Share on twitter
Share on facebook
Share on whatsapp

Short Bytes: Every hobby has its own milestone projects like the Hello World introduction in programming. The Arduino community likes to start people off with the blinking LED light. But what could we do with 64 LED lights in a matrix? We can display all sorts of things like numbers, letters, and even simple pictures. In this article, we’ll go through how to do that as well as Conway’s Game of Life.

The Arduino has seen explosive popularity over the last decade. There are thousands of libraries available for it. It’s a very well documented platform and amazing for learning to program and design electronics.

In this article, we’ll lightly cover the use of the MAX7219 and MAX7221 LED drivers with the LedControl library, which is available for download and installation in the Arduino IDE as well as on Github, and we’ll implement Conway’s Game of Life. The two important parts you’ll need for this is one of the above mentioned LED drivers and a compatible LED matrix (in addition to the Arduino and some standard electronics components, of course). You can find many for very cheap (less than $3USD) on different online stores such as eBay, Banggood, and AliExpress. It’s very important to read the description of what you’re purchasing, though, because many of these come as kits that need to be assembled and soldered. Depending on where you live, the shipping time may be a few weeks, so be sure to double check the estimated time of arrival for your area.

I’ve gone ahead and shared the source for this project on my (baron) Github page, so feel free to download it, modify it, re-post it, or whatever you’d like. It will be necessary to follow along and there are a lot of comments in the code on Github that are not included in this article simply because it would be a lengthy article and they’re much more effective in the actual code than trying to squeeze them into this article.

After the header file and the pin definitions, you’ll find a line that might not make sense.

LedControl lc = LedControl(DIN, CLK, CS, 0);

This creates the LedControl object that’s used to interface with our LED driver by providing the digital input, clock, and chip signal pins, as well as specifying the number of devices (they can be daisy-chained together for a large display). The control protocol between the LED driver and the Arduino is SPI (Serial Peripheral Interface), but we don’t have to get into that because we have an object of the LedControl class to do our bidding for us.

Find your dream job

The next portion, in the setup() function, is mostly self-explanatory, but we’ll cover it for clarity.

lc.shutdown(0, false);
lc.setIntensity(0, 8);

The LED driver will be in a shutdown state when first powered on, so we need to tell it to wake up, which is what the first line does. The second sets the brightness, since we’re only using a single display the first parameter, the address of the matrix, is 0, and the intensity should be set to you whatever you prefer, the maximum valid value being 15. Lastly, we clear the display of any possible previous values. This is typically not an issue, but if your Arduino restarts for some reason, it could potentially leave unwanted LEDs illuminated.

Before we get into the programming it’s important to understand what Conway’s Game of Life is and what the rules are. The game is a “zero player” game. All that means is that the player chooses the initial state, which we’ll do at random, and then follows the rules. The Game of Life is very simple and only has four rules:

  1. Any live cell with fewer than two live neighbours dies, as if caused by underpopulation.
  2. Any live cell with two or three live neighbours lives on to the next generation.
  3. Any live cell with more than three live neighbours dies, as if by overpopulation.
  4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.

– Wikipedia

Get Your Own Arduino Board and Start DIYing Today

To represent the grid we use two two-dimensional arrays. Since we’re working with 8×8 matrices, those are the dimensions we apply to our arrays. While byte arrays would be much more efficient in memory (and are in fact used for used often in applications like this), booleans are much easier to modify in an array than bits in a byte. Modifying bits within a byte would require the use of bitwise operations that would complicate things quite a bit compared to the simple boolean arrays. The second array is because we have to compute the next state from the current state, but if we alter the current state, we will not get the desired successive state.

The next two for loops are for initializing the cgrid array with random values and the ngrid array with false values. There is a one in four chance that the cell will be ‘alive’, this is done by generating a random number between 0 and 3, if the number is greater than 2 then the cell is alive.

The rest of the code is either simple enough to not need any further explanation or has an explanation in the code. But, the displayGeneration() function is more interesting. This function, with little modification, is capable of taking byte arrays and pushing them directly to the matrix. The lc.setColumn() method call actually takes a byte as it’s the last argument, the first being the address and the column that’s being set. By using this and setting up as below you can easily create a function that will display an eight-element byte array, such as a letter or image, on the matrix.

void printByte(byte ba[8]){
  for (int i = 0; i < 8; i++){

You can see that the method used is the setRow() method as opposed to the setColumn() method. What this does is simply apply the byte horizontally as opposed to vertically, a simple 90-degree rotation. This is an important point given that your project’s orientation won’t necessarily be the same as mine. Additionally, you can reverse the order that the bytes are placed by using (7 – i) as your second argument, effectively mirroring the output. This is a very easy way to correctly orient the output for your project.

If you intend on displaying text on your matrix, you can find 8×8 matrix fonts online. It is, however, very important to note the total amount of RAM your microcontroller has. I used the Uno for this project and one of the fonts included definitions for the 128-bit configurations beyond the 7-bit ASCII standard. This was too much for my Uno (that’s 8 bytes x 256 = 2048 bytes, the Uno’s entire capacity) and I needed to half the set to the original 128 ASCII definitions in order to make that font work. So, keep in mind, your mileage may vary depending on your microcontroller.

After a short amount of work, you can sit back and watch the mesmerizing effect of the Game of Life on your own little LED matrix display.

Are there any other Arduino projects you’d like to see covered on Fossbytes? Let us know in the comments below.

Devin McElheran

Devin McElheran

IT professional by day and various hobbies by night.

Find your dream job

Work at your dream company with Fossbytes Jobs