Arduino Tutorial: Creating Pong
An Arduino tutorial about making a pong game with an LCD and some push buttons. By Felipe Laso-Marsetti.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Arduino Tutorial: Creating Pong
55 mins
- Prerequisites
- Materials required
- Setting up your dev environment
- Soldering the LCD
- Connecting the LCD
- Adding the potentiometer
- Testing the potentiometer (Optional)
- Connecting the potentiometer
- Finishing the LCD’s connections
- Testing the LCD connection to Arduino
- Connections for user input
- Drawing Player 1’s paddle
- Drawing Player 2's paddle
- Coding the ball and collision detection
- Where to go from here?
If you’re a programmer with an interest in electronics, playing around with an Arduino Uno is a great way to learn.
We’ve had several tutorials on the Arduino on this site in our past, that cover topics such as using a button, creating a traffic light, and even making a temperature sensor.
If you’ve done those and are looking for more, we have a great tutorial for you!
In this tutorial you will create a fully functional 2-player Pong game, and learn about more advanced techniques like reading a data sheet, soldering, and more.
So, sit back, grab a nice refreshing beverage and get ready to build your very own console and Pong game! :]
Prerequisites
This tutorial will make use of the following topics:
- C programming language
- Electronic circuits
- Arduino platform (software and hardware)
- Soldering
Technically, you can follow this tutorial without any knowledge about these topics. Understanding any or all of these, however, will make things much easier for you, and help with building your own little console and pong game.
Materials required
Since this tutorial will focus not only on writing the game for Arduino but also on building your own little console, you will need some materials:
Some of the materials required come with the Sparkfun Inventor’s Kit, at $99.95. The kit includes the potentiometer, jumper wires, 1 breadboard, USB cable, 2 of the 4 buttons you will need, and many other cool pieces of hardware that will allow you to make other projects and continue learning with the included tutorials and booklet. The full list of items you need to complete this tutorial are as follows:
-
Arduino Uno R3 (Quantity: 1) at $29.95 [included in Sparkfun Inventor’s Kit]. This is the core piece of your console/circuit. It’s the brains of your project and what you will connect everything to.
-
128×64 Graphic LCD (Quantity: 1) at $19.95. This is the screen you will use to draw everything and to be able to visualize your game. This tutorial uses a display based on the JHD12864E model, but the library you’ll be using will support several models from many different brands, including the KS0108 and JHD12864H models.
-
30-row breadboard (Quantity: 2) at $4.95 each [one is included in Sparkfun Inventor’s Kit]. The breadboards are where you connect each component to interact together; You will be connecting the LCD for displaying the game, your buttons for user input, the potentiometer for controlling the LCD’s contrast, and more. Here’s what the breadboard looks like:
-
Male-to-male jumper wire 30-pack (Quantity: 2) at $4.95 [one is included in Sparkfun Inventor’s Kit]. Jumper wires are what you will use to connect things between your breadboards and the Arduino pins.
-
Jumper wire kit (Quantity: 1) at $6.95. These smaller, fixed-size jumper wires are excellent for jumps within your breadboards without having cables clutter everything. They are also colored so it helps to determine what each one is doing.
-
Break away headers – machine pin (Quantity: 1) at $2.95. The break away pins will be soldered into the LCD to properly connect the LCD to a breadboard.
-
10K potentiometer (Quantity: 1) at $0.95 [included in Sparkfun Inventor’s Kit]. The potentiometer will control the contrast of the LCD.
-
Push button (Quantity: 4) at $0.35 each [two included in Sparkfun Inventor’s Kit]. The push buttons will be used for user input, as up/down buttons for each player’s pong paddle.
-
USB cable A to B (Quantity: 1) at $3.95 [included in Sparkfun Inventor’s Kit]. This is the cable you use to connect the Arduino board to your computer in order to upload sketches and provide it with power.
-
Soldering iron (Quantity: 1) at $9.95. You will use the soldering iron to solder the break away header pins to the LCD holes. This will give you good, solid connection points out of the LCD and will also allow for easy connections to your breadboard.
-
Lead-free solder wire rosin core spool (Quantity: 1) at $7.95. Solder is a metal combination that very easily melts under heat and is fantastic for soldering together components. The rosin core is used to keep the connections clean during the soldering process and facilitates the bond between the metals and the solder. Read more about rosin core here.
-
Digital multimeter (Optional) (Quantity: 1) at $14.95. A multimeter will help you measure DC voltage, resistance, and more.
In total, the cost of all the materials (assuming you buy all of them from scratch) is approximately $104 without a multimeter and $119 with one. If you buy things with the Sparkfun Inventor’s Kit, it will cost about $54 extra ($158 without / $173 with the multimeter).
This may seem rather pricey, but remember you can use these materials more than once. Apart from the header pins (only 20 of them) that you will solder to the LCD, or the solder spool, everything is going to be useful for many, if not all of your projects.
Setting up your dev environment
In order to work with Arduino and upload your sketches (the name of a program in Arduino), go to the Arduino downloads page and get the latest version of the IDE for your operating system. If you are using an Arduino Due or Yún then you will need to download the latest beta version 1.5 or above.
Once downloaded, put the Arduino app in your Applications folder and run it. You may be prompted to install Java, go ahead and do so as it’s required to run the Arduino IDE.
Having run the IDE at least once, you will have a new folder in your user Documents directory named Arduino:
Writing your own code to draw to the LCD would be very time consuming. When you write an Android or iOS app you don’t code everything from scratch; you get APIs for data structures, drawing to the screen, sound, and more. Similarly, Arduino has a vast array of libraries (first and third party) that you can use for your projects.
A library called GLCD, which you can acquire here, provides you with easy-to-use functions to draw to many models and types of LCD displays.
Go ahead and download the source for GLCD and locate the folder. Drag the newly downloaded folder for GLCD and put it inside ~/Documents/Arduino/libraries. This is how you install additional libraries for Arduino:
Relaunch the Arduino IDE and in the menu bar go to Sketch/Import Library…, at the very bottom (underneath the “Contributed” header) you will see GLCD listed:
With that, your development environment is ready. Yaaaay! :]
But before you can do anything you have to solder some header pins to the LCD and connect everything to the Arduino.