Introduction: Arduino VGA Console With Five Games
In my previous Instructables, I have reproduced simplified versions of some of the most popular classical arcade games, by means of a bare Arduino and few other components. Later on I joined five of them together in a single sketch. Here I will show how you can build a simple console that can to play Pong, Breakout, Bomber, Tetris and a drawing toy inspired to Etch-a-Sketch. This console can be used also to play Snake, and two more games written by other authors: Bit Ninja by Sandro Maffiodo aka "Smaffer" and Stacker by Nikita Kurylev.
The main feature is the generation of a VGA signal, thanks to the VGAx library, thus the console needs a VGA monitor. My goal, as usual, is to avoid any "special component" to build it, you don't need then any supporting IC or shields! The only components are two potentiometers, five buttons, few resistors and a DSUB15 (VGA) connector. A piezo speaker is optional. You can see how these games look in the pictures in this page.
The VGAx library allows to use four color with a resolution of 120 x 60 pixels, not many but enough for this retro-game console. The graphic is raw but, thanks to the use of the potentiometers, the games run smoothly. Simple sound effects are available too.
Step 1: How to Build Your Own Arduino VGA Console
First download ArduinoVGAgame.ino and/or Snake.ino codes at the bottom of this page and copy them in your PC in a directory with the same name. Download the VGAx library from this link on GitHub. The easiest way is to copy it in the Arduino software subfolder named "libraries", to be immediately recognized.
IMPORTANT: this library works for Arduno IDE 1.6.4 but it is not fully compatible with elder or newer versions.
Upload the code in your Arduino board (I tested both Uno and Nano). A warning for low available memory is normal. If you do not have other errors everything is ok and you can immediately start to build your own console.
For this you need:
- an Arduino Uno Rev. 3 or Arduino Nano 3.x (ATmega328)
- a DSUB15 Connector, i.e. a VGA female connector or a VGA cable to be cut.
- resistors: 2 x 68 Ohm and 2 x 470 Ohm and 5 x 1 to 2 kOhm
- two 10 kOhm linear potentiometers (similar values are fine too)
- five buttons
- some piece of cable
- one or two nice boxes to put all the components.
- a breadboard or a strip board
- a piezo speaker
The schematic is reported at the top of this step, together with an example of a finished “console”.
the schematic shows how to connect a button and a potentiometer. More specifically, you need to connect five buttons to pins 5, 10, 11, 12 and 13 respectively. The action performed by each button is described in the upper right table in the schematic. On the left it is shown how to connect a potentiometer (you need two potentiometers to pins A1 and A2). The speaker must be connected to analog pin A0.
I placed the Arduino board with the VGA connector in a wood box, which holds also the first player potentiometer and four buttons, while the second player potentiometer and his start button are in a separate and smaller box.
If you like this toy and you decide to reproduce it, I appreciate if you write a comment or send a picture in the comment section below.
Step 2: Games From Other Authors
Sandro Maffiodo has recently pubilshed the game BitNinja. Youn can find more informations here and download the code here.
To use my console, you just have to remap the button in his code as follows:
#define BTN_UP 11 (instead of 13)
#define BTN_LEFT 10 (instead of 12)
#define BTN_RIGHT 12 (instead of 11)
Stacker, from Nikita Kurylev, is available here. More information here.
Again, you have to remap one button, in different part of the code: just replace digitalRead(2) with digitalRead(13)
Step 3: Appendix 1: More Details About How to Build the Controllers
You can realize the controller in many different ways, depending on the available material and your taste.
I like to realize them with wood boxes (see the pictures in this page). A main box for the Arduino, the VGA connector and the first player buttons and potenziometer; a second (smaller) one just for the second player button and wheel (needed for Pong and the drawing toy). Another possibility is to put everything in a single larger box.
First I reccomand to connect the VGA port. In the first and second pictures you can see some details: note the two 470 Ohm resistors for Red and Green connected to pins 6 and 7 respectively, and two 68 Ohm to pins 3 and 9 for horizontal and vertical synchronization signal.
You can choose different color combinations depending on wich pins you connect on the VGA DSUB15 connector, the pins 1, 2 and 3 represent Red, Green, Blue (RGB) respectively. I connected the pins 1 and 2, thus I have the following color combination: (0, 0) = black; (1, 0) = red; (0, 1) = green; (1, 1) = yellow.
For all different possibilities, I suggest to read the details in the page where you download the VGAx libraries.
As soon as the VGA connector is ready, you can prepare all the other cables for the buttons, wheels and speaker (see picture 2).
Now just put everything together: remember that each button pin need to be connectet to ground through a 1 or 2 kOhm resistor, otherwise when the button is open the state of the pin may be undefined. This means that if the pin is left disconnected you can have a random (static) voltage on it that can activate it. See for more deteils the schematic in the second step of this instructable.
The last step is to fix everything in place. I used the hot-glue-gun, but you can use your preferred method.
Step 4: Appendix 2: Some Considerations About the Memory Limits
It is amazing that a simple Arduino is able to generate a VGA signal and all these games together. The real bottle-neck is the lack of SRAM. The AVR microcontroller has only 2048 bytes available to store and manipulate the variables, and the VGAx library stores the screen variables in a 120x60 pixels framebuffer where each pixel needs 2 bits (4 colors), for a total of 1800 bytes. This means that there are only 248 bytes left for the sketch variables. Furthermore, in my experience, one should leave at least 100 bytes free to avoid instabilities. Using above 1950 bytes of dynamic memory, the microcontroller begin to show odd and unpredictable behaviour.
This means that all the variables must be shared among the different games, and this makes the code quite unreadable and hard to debug. It is not just a matter to "add a new game" to the previous sketch, but all the code must be deeply modified and optimized.
Furthermore, I had to use the minimum possible variable format: for instance, for all the coordinates I had to use "byte" instead of "int" or, in other cases, I had to prefer "int" instead of "float".
Finally, my aknowledgement goes to Sandro Maffiodo aka Smaffer, the creator of the VGAx library and the awesome game BitNinja. Without this library, this project could not have been realized.
Thank you also to Nikita Kurylev for the simple but funny game Stacker.
6 weeks ago
I dont see the color blue and i can't see white. What pin does the blue pixle color go on
Red = pin 6
Green = pin 7
Blue = ???
Question 4 months ago
Quand je joue un pong quand la balle frappe le curseur le frame distortionne que pui je faire pour regler ca
Answer 4 months ago
Did you add a low impedence speaker? Try to add 1 kOhm resistor in series with it. In this way you limits its current, which excess could lower the voltages of the VGA, then loosing the syncrony...
Reply 4 months ago
J'ai essayer la resistance de 1k Ohms en série sur un piezo et un haut parleur 8 ohms et autre valeur mais rien de regle mon problême.
Reply 4 months ago
Strange! it still looks like an interference between sound and VGA signal.
Try to comment the line 796 and 805, i.e. replace
In this way when you hit the left or right paddle there is no sound at all...
If it works, you can try to put the sound back with a different frequencie/legth, for instance:
2 years ago
Nice job creating the code! When I download the code and upload it to the Arduino, on the start screen. I can only see "Choose an Arduino Game " and the moving x but not the game names. Any tips about what happened.
Reply 1 year ago
your display looks really red, and the games names are in blue, so i think your blue connection has a problem.
3 years ago
Wow i love this idea.Aee the 68 ohm and 470 ohm resistors needed?
Reply 3 years ago
Hi, as far
as I understand, the VGA input has an internal resistance of 75 Ohm, and for
the RGB, it accepts input value between 0 and 0.7 V. I suppose 68 Ohm for H and
V synchrony is just a close value to 75 Ohm (for impedance matching), and 470
Ohm is to have 0.7 V for RGB (75/(470+75)*5V = 0.69 V). If you exceed 0.7 V, the
VGA is just saturated. Not a big deal, I think VGA can handle some overvoltage,
but I have no idea of the limits, which may depend from monitor to monitor.
Thus, a direct connection (which means 5V as input signal) may work on some
monitor and not on others and, in the worst case, cause a damage. This is just
a feeling; I have no direct experience BTW.
3 years ago
Questo è fantastico, può gestire una serie di giochi, tra cui uno dei miei preferiti, Tetris, due mesi fa sono stato in grado di giocare lo stesso gioco per ore senza perdere una sola volta.
3 years ago
would it be possible to use the vga hdmi adapter
5 years ago
Awesome project. Could you share a little more about how you made the controllers.
Reply 5 years ago
Thank you very much for the comment. I have added appendix 1 with more details.