In this Instructables I will show how to build a clone the classic game Tetris, using an ESP32 and generating the output for a VGA monitor.
This game is made possible by the amazing ESP32Lib Arduino library done by bitluni.
You will need the ESP32 board support installed in the Arduino IDE (I am using the last version available now, i.e. the 1.8.9 IDE). You can find it in the Library Manager searching for “bitluni”. Since the last version has been published just a couple of weeks ago, it is still work in progress but it already provides some simple examples.
Compared to my previous projects where I wrote similar vintage game clones for the Arduino or ESP8266, the ESP32 is extremely more powerful, the VGA output with various resolutions (320x200, 320x240, 360x400, 460x480) and up to 14Bit colors. Furthermore you have orders of magnitude more memory.
In this Tetris clone I am just using a resolution of 320x200 with 8 colors. The code is a quick "conversion" of a previous version for the Arduino VGA, thus it is poorly designed and un-optimized. On the other hand, the ESP32 VGA potentiality are much higher, giving the possibility to write more and more sophisticated games comparable, I guess, to the ones from the DOS era. I wish that this project would be of use to other makers to write many other games in the future.
Step 1: ESP32 Boards, Arduino IDE Installation and Configuration.
First of all you need to puchase an ESP32. There are many versions available, but I recommand to choose one with many pins. I bought this version, but you can fiund three other versions in the description of this video.
There are different methods to program the ESP32, but here you need to use the latest Arduino IDE. To install it, and to upload the bitluni ESP32lib VGA library, you can follow this easy tutorial. It also shows how to confugure your specific ESP32 board in the Arduino IDE.
At this point you can start to upload the first example: go to File/Examples/bitluni ESP32Lib/VGA2DFeatures, as shown in the picture at the beginning of this step. If the upload works without any issue, you need to connect the VGA port as shown in the next step.
Step 2: VGA and Buttons Connection
To connect the VGA monitor to the ESP32, you can either use a DSUB15 female connector (i.e. a standard 15 pin VGA one) or just cut a VGA cable.
For Tetris, and the 8 colors library examples, you do not need any resistor, just connect the pins as follow:
- VGA red to ESP32 ping G2
- VGA green to ESP32 ping G15
- VGA blue to ESP32 ping G21
- VGA Hsync to ESP32 ping G32
- VGA Vsync to ESP32 ping G33
- VGA GND to ESP32 GND
At this point you should be able to see the VGA2DFeatures example on your VGA monitor.
To play Tetris, you need four buttons and four resistors of about 1 to 2 kOhm.
Connect the four resistors to from GND to the ESP32 pins G25, G26, G34 and G35.
Connect one side of the button to ESP32 3.3 Volt, and the other side as follow:
- button right to G26
- button left to G34
- button rotate to G25
- button down to G35
Last step: download ESP32_VGA_Tetris_V1.0.ino at the end of this page, and copy it a folder with the same name.
Upload it on the ESP32 microcontroller and you can immediately start to play Tetris!
Step 3: Conclusion and Acknowledgments
I am very grateful to bitluni, the author of the ESP32 VGA library and the tutorials and examples to use and exploit it.
Finally, if you liked this Instructable, consider to vote for it in the Arduino & microcontrollers contest!
This is an entry in the
Arduino Contest 2019
karlt1 made it!