Game space

Build a Simple Handheld Video Game Using an Arduino

Learning to write code can be a long journey. Not only do you need to find resources to help you learn, but you also need to find ideas for exciting projects that keep your education on track.

Video games provide a great opportunity to program something you can enjoy. And they can also teach you the basics you need to be a good programmer. This article explores a DIY handheld Arduino video game project to get you started.

What do we manufacture and why?

A close up of an Arduino circuit board, focused on a small screen with the word

Before diving into the guide portion of this article, it makes sense to explore what we make and why we chose it for this project. Several challenges come with creating games for devices like Arduinos.

  • Storage space: Most Arduino boards come with very little RAM or flash memory. Not only does this limit the amount of code they can run, but it also presents challenges when it comes to working with variables.
  • Processing power: The Arduino we used has an underpowered processor, so it’s impossible to create resource-intensive applications.
  • Limited Components: Arduinos have plenty of pins available, but many components need more than one. This limits us when it comes to commands and display outputs.

With these challenges in mind, we set out to create a handheld device that runs a very simple reaction-based game. You could easily create other games for the hardware we work with, and we encourage you to get creative.


A close up of an Arduino circuit board, with a small screen showing a game

The game itself is very simple. Three blocks appear on the screen, two underlined and one filled, each with a corresponding physical button below. The player must press the correct button each turn to continue playing, and if they press the wrong button or run out of time, the game is over. Each successful press of a button earns points. The game slowly increases its difficulty by reducing the available reaction time.

Wiring the OLED display, Arduino and buttons

A simplified circuit diagram showing an Arduino board connected to a display and four buttons.

Wiring up your portable game is the first step in this process. As you can see in the circuit diagram above, you don’t need to make many connections to get started, and you can even use a breadboard for this project.

We have already published a guide to help you wire push buttons to an Arduino. Our Arduino Aquarium Monitor project shows you how to use an OLED display with a microcontroller like this. As such, we’ll focus on the code from this project for the remainder of this article.

Arduino handheld game code

A list of four files hosted on GitHub

Proper formatting and tidy file structures are very important for both novice and experienced programmers. You’ll thank yourself for taking the time to carefully organize your code if you ever had to go through it.

The code uses a main INO file to form the basis of our project. There’s also a C++ file to control our display, a C++ file to run the main game code, and an Arduino library file to hook it all up.

We’ll dive deeper into this code below, but we encourage you to take a look at the commented code in our Arduino Handheld Game project on GitHub. It will help you to refer to it next to this article.

The library file (library.h)

Our library file plays a crucial role, tying our other project files together so they can work as one application. This file contains declarations to include the Arduino.h library and all the functions in our game code that need to work between our files. Without it, our code simply wouldn’t work.

The main INO file (main.ino)

Like many Arduino projects, this one started with the default template provided by the Arduino IDE. This model gives it implement and loop functions that we use to call functions in our other files. This file also has a statement to include the library.h file.

The setup function is great for initializing our screen and buttons because it only runs once when the Arduino is reset or powered on. To do this, we call the disSetup() function in our display file and the butSetup() function in our game file.

Our loop() function is even simpler than the setup() function, with just one call to the gameState() function found in our game file. We’ll explore this in more detail later in the article.

The display code file (display.cpp)

A close up of an Arduino circuit board, with a small screen displaying a message

We’re using an SSD1306 OLED display for this project, although you can use a different display type if you modify the code accordingly. This file starts with the inclusion of the library file, library.h. It then includes declarations for the SPI, Wire, Adafruit_GX, and Adafruit_SSD1306 libraries. Then it defines some constants to provide parameters for the display.

The first function, uninstallation, initializes the display and displays a logo splash screen. It then clears the display after waiting about 2 seconds. Our main file, main.ino, calls disSetup in its implement a function.

The rest of the functions in this file, with the exception of timerBar(), form the various screens seen throughout the game. The game file, game.cppcalls each of these functions.

  • startGame(): this function controls the first screen the player sees. He simply asks if they are ready by calling the switchInstance function found in the game file.
  • startingGame(): Once the player has started the game, this screen displays a short countdown. It then changes the game state to match.
  • inGame(): This is the most complex of our display functions, using three variables to load each round of the game. It starts with an if statement that determines which of the three tiles to fill, followed by displaying the tiles and the player’s level and score.
  • timerBar(): this function uses a variable to display a timer bar at the bottom of the screen. It shows the player how much time he has for each round.
  • successScreen(): This is a simple function that displays a message each time the player completes a round successfully.
  • endGame(): this last function displays a game on the screen with the player’s score and the option to restart the game.

The game code file (game.cpp)

A close up of an Arduino circuit board, with a small screen showing a game

Finally, as the last file to explore, it’s time to look at the game’s code. This file, like the others, starts with the inclusion of the library.h file. It also contains a long list of different integer variables that we use during the game.

You will find the butSetup() function before anything else. The setup() function in our main file calls butSetup. This function uses variables to configure our buttons as inputs that we can read later.

  • switchInstance(): This function switches between instances of the game, from the splash screen to the game and game screens. It receives a variable to tell it which game state to switch to. An if statement waits for a button to be pressed to start the game from the startGame() state. Another if statement restarts the game from the endGame() state.
  • gameState(): this function sets the difficulty of the game based on the player’s score and calls the correct function based on the game state. It starts with a series of if statements that set variables based on the player’s score , followed by other if statements to check the current instance.
  • tileSelector(): This function generates a random number between 0 and 2 that tells the game which of the three tiles to fill at any given time.
  • theGame(): This function is arguably the most important of all. He calls the tileSelector and Stake functions, choosing a new thumbnail and displaying it on the screen. Next you will find a for loop which acts as a timer for each round. It contains a set of if statements that detect button presses and determine if they are correct.

Build your own portable Arduino game

This article is a companion to project code found on GitHub. You can find more information about the specific lines of code we used in the comments for these files. But you can also just load it on your Arduino and enjoy the fun.

Creativity is key in the world of game development, and we encourage you to work on your own game ideas alongside this one.

Arduino circuit board

The 10 Best Arduino Radio Projects

Read more

About the Author