PongXtreme - MCT
Daan Theys, Tijn Sandra and I remade Pong for a school project called TeamProject. In this version of Pong the player plays the game by running back and forth instead of sitting on a chair. I will explain how to do the circuit, casing and code but here is a link to my GitHub page with all my code in case you get stuck or don't feel like doing the code.
https://github.com/RubenVanAsbroeck/TeamProject-PongXtreme.git
Supplies
Here is a list of all the necessary supply's, you can also check the list in the pdf file which contains all the components along with the necessary amount (apologies for the way excel exports to pdf format), the price and where we bought them (there might be places where you can buy them cheaper).
The numbers in brackets are if you want to have multiplayer.
Micro computers:
- 1 Raspberry Pi (pi) – (We use the Raspberry Pi 5) (1)
- 1 ESP32 (2)
Sensors:
- 1 HLK-LD2410C (Human prescence sensor) (2)
Actuators:
- 2 RGB-led's (3)
- 3 Buttons (3)
Regulator:
- 1 100nF condensators (2)
- 6 470 Ohm resistors (9)
- 3 1000 Ohm resistors (3)
Extra parts:
- 1 USB-A to USB-C cables (2)
- 1 Sreen (it doesn't matter how big or small it is as long as it can connect to the Pi using a HDMI cable and show the game) (1)
Downloads
Make the Circuit
To build the curcuit for the game you can simply follow the one I made. Do know that if you use different pins than me on the pi or esp you might have to slightly adjust the code to make it work.
Keep in mind that the ESP circuit should be made twice if you wish to play with 2 people.
Here's how I connected the pins:
Preparing the Pi
After you have created the circuit, we can begin the next part: setting up the RPi.
What you need:
The 16GB or more SD card.
Step 1:
Open Raspberry Pi Imager. Insert the micro SD card into your PC.
Step 2:
Choose your Raspberry Pi model and operating system. I recommend RPi OS 64-bit.
Then select your storage device and continue.
Step 3:
Personalize the settings (such as name and password) and continue.
Step 4:
Insert the SD card into your RPi. Connect your Raspberry Pi to your PC using a LAN cable. Connect through SSH.
Frontend Design
Now it's time to start designing your website (we created ours in Figma) keep in mind that this entire project is made using the library Pygame. If you don't want to design the entire website yourself, you can download my Figma file and make changes as you like. After you have finished the design, you can begin coding the frontend of the page. Once the frontend is ready, you will have a place to display all your data, so you can start coding the sensor integration.
Backend Sensors (ESP)
To begin, I recommend reading the sensor annd sending data to the raspberry pi in seperate files. Once both of them work it's easy to combine them into a working whole.
Here you can find our ESP code: ESP-code/ESP-code_final.ino
These are the libraries I used:
- Wire : we need this librarie to read / send bits to the sensors.
Human Prescence sensor:
For this sensor, we will use a custom library called MyLD2410. Since this is a serial-based sensor, we need to define which ESP32 pins are connected to the sensor's TX and RX lines. In this case, we use GPIO 25 (RX) and GPIO 26 (TX). Initialize the sensor communication with these lines:
- #define RX_PIN 25
- #define TX_PIN 26
- MyLD2410 sensor(Serial1);
In the setup, you start the serial connection at a baud rate of 256000 (which is the speed the LD2410 talks at). Then, you use the function sensor.check() in your loop to see if the radar has new data.
If a person is detected, the code calculates the distance and stores it in a variable. To keep the data clean, we only update the distance if it is more than 15cm away. If the sensor doesn't see anyone for 100ms, it automatically resets the distance to 0 so the Raspberry Pi knows the room is empty.
Sending data to the Raspberry Pi:
To talk to the Raspberry Pi, we use the standard USB Serial port at 115200 baud. Instead of sending text, we send a 4-byte packet every 20ms. This packet starts with a "header" byte (0xAA) so the Pi knows where the data begins, followed by the sensor ID, a presence bit, and two bytes that represent the distance in centimeters. While it's sending, the Blue LED (GPIO 33) will flicker to show you it's working.
Backend
Backend Structure Overview
The backend of your dive computer project is divided into two main responsibilities:
1. Collecting Sensor Data
2. Serving Data to the Frontend
3. Running the game loop
Backend Architecture for PongXtreme: Raspberry Pi & ESP32 Communication
1. Collecting Sensor Data
Your Raspberry Pi acts as the central computer. It gathers information from the 2 sensors that are connected to an ESP microcontroller. To let these two devices "talk" to each other, you simply connect them using a USB-A to USB-C cable.
- How does it work? The ESP sends the incoming sensor data to the Pi. This happens quickly and repeatedly, so the Pi always has up-to-date data.
- Why use serial communication? Serial communication is reliable and fast for short distances, making it ideal for connecting a Pi to an ESP inside the same device. It also doesn’t require a network connection, which is helpful underwater or in remote locations.
- Where is the code? On your GitHub you have two folders: one for the code that runs on the Pi to ask for sensor values esp_communicatie, and one for the code that runs on the ESP to answer those requests communicatie_pi. You simply copy the function for the Pi into your main backend file (such as app.py), so it can regularly collect sensor data.
2. Serving Data to the frontend
Once the Raspberry Pi has the sensor data, it needs to translate this information to a position on the screen.
- How does the frontend know the paddle position? The Pi receives the sensordata and converts is to a position, it does this conversion based on the heigth of the screen. Afterwards the Pi draws a rectangle at that position.
Summary
- The ESP sends the sensor data to the Pi.
- The Pi then receives the data and converts it to a certain position.
- The Pi uses this converted number to figure out where he should draw the paddle of the player.
Building the Case
Now for the last step, we'll be making the actual case.
We decided to 3d print the entire case, you can use the same as us or design your own case.