Recently I’ve been helping out with the local library’s Arduino workshop, which is one of the events for their summer Maker Days program. It’s been a lot of fun watching kids and adults learn to build circuits and program microcontrollers. Doing so has also inspired me to spend some more time playing with Arduinos on my own. I recently purchased a small OLED display from Adafruit, and while I was getting the hang of working with it, I decided to put together a simple Pong game. Here it is in action. If you’re interested in seeing how to build your own, read on!

Much of the display work involved adapting the Adafruit-provided example code. The Arduino communicates with the display using SPI, which can apparently be implemented in software or by hardware built into the microcontroller. I decided to try using the hardware version (hardware’s always better, right?), which meant I had to change some pins and such. It was a little tricky to find the right pins because different guides name them different things. For example, the Data pin on the display board should connect to the MOSI pin on your Arduino. This makes some sense if you know that MOSI stands for master output, slave input. It turns out I only needed to change two pins. After consulting the Arduino SPI page, I decided on the following pin mapping for my Arduino Mega.

  • Data → 51 (MOSI)
  • Clk → 52 (SCK)
  • SA0 / DC → 11
  • RST → 13
  • CS → 12
  • 3v3 → 3.3V
  • Vin → 5V
  • Gnd → GND

This was enough to start writing the code to draw the game board, make the ball bounce, and control the computer player. To add the human player, we need to add a couple of buttons. To keep things simple, I just added one up button and one down button.

Wiring switches is a little more complicated than I would expect. You’d think you could just run a wire from 5 volts to one side of the switch and then from the other side of the switch to one of the Arduino’s digital inputs. The problem with this is that when the switch is not being pushed there is no way of knowing what value we should expect to read from the digital pins. Instead, we use a pull-down resistor (although I got the ground and +5 backwards on mine, so I have a pull-up resistor instead). The Wikipedia page has a good diagram of what this circuit looks like. Basically, the idea is that we tie the digital pin to a known value with a fairly big resistor (I used 10k ohms), while the switch connects the pin to a different value with a much lower resistance. In this case, when you push the switch you read the correct voltage, but the large resistor keeps a lot of current from flowing. At least, that’s my best understanding of it; perhaps someone else can explain it better.

Once I had everything wired up, all that was left was to finish writing the code and then have some fun playing.

You can see the code I wrote on GitHub:

I also drew up a schematic for the circuit using 123D Circuits:

If you decide to tinker with this, there are some obvious improvements. For example, the game could restart when the ball hits one of the side walls, and the game could keep score. The microcontroller gets kind of warm after a while, which means the sketch could use some changes to reduce power consumption. This circuit could also work for entirely different games, such as Frogger! Whatever you decide to do, have fun!