Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe

Building Ping Pong Game on Arduino UNO

By Praveen Kumar, Chennai, India April 8, 2017

Ping Pong has been a popular game. Sometimes back the game was available on game consoles, the desktop and mobile versions of the game also saw the light of day as the desktop computers and smartphones became rage age after the age. In this project, a desktop version of the game will be developed with the difference that the paddles of the game will be controlled through switches instead of by dragging them through the mouse.
 
The desktop versions of the game usually allow a player to play against the computer and the player could control a paddle with the help of mouse. Since only a single mouse device can interface with the computer at a time, a multiplayer desktop version of the game cannot be realized using the mouse. In the project, an Arduino based gamepad will be designed so that two players could play the game against each other. The desktop game itself will be designed using PROCESSING language.
 
In the ping pong game, a ball strikes between two opposite walls. There are paddles given at the walls which are usually controlled by a player and the computer. A player has to avoid the ball to collide with the wall by striking it back by the paddle by moving it either horizontally or vertically. If the ball collides with the wall, the opposite player or computer gains a point. There is a game over once one of the players (usually either a computer or human player) gains a certain number of points. The player who gains a certain number of points first by more successfully avoiding the ball to collide with its wall is declared a winner when the game is over.
     
Screenshot of Arduino based Ping Pong Game
 
Fig. 1: Screenshot of Arduino based Ping Pong Game
 
Processing is an open source programming language for computers with an IDE available for its communities. The development of the language started in the year 2001 in MIT Media Lab USA, by Casey and Benjamin Fry. In 2012, the language along with its IDE was launched, credits to Daniel Shiffman, who joined the open-source project and turned around to be main lead. The language is developed with an aim to teach computer programming in a visual context for electronics and media arts along with strengthening the foundation of electronics projects. The open-source software package includes a sketchbook and an add-on for organizing projects.
 
The language is based on a java class which implements most of the processing language’s features and allows programmers to make user-defined classes within the PApplet sketch. The game designed using Processing Language runs on desktop and could communicate with the Arduino based gamepad through serial communication. The game developed in the project has the ball striking in a horizontal direction with the provision of paddle’s movement in vertical direction.
 
Prototype of Arduino based Ping Pong Game
 
Fig. 2: Prototype of Arduino based Ping Pong Game
 
The Arduino based gamepad will interface with the computer using a virtual serial port. The gamepad has four switches, two each for both players to move the paddle either up or down. The pad will have four LEDs as well to give a visual hint of the button pressed by the user.
 
Components Required – 
1. Arduino UNO
2. Push-to-ON switches -4pcs
3. LED’s
4. 1K ohm Resistors
 
Software Tools Required – 
1. PROCESSING language and IDE
2. Arduino IDE
3. AVR Dude

Block Diagram – 

Block Diagram of Arduino based Ping Pong Game
 
Fig. 3: Block Diagram of Arduino based Ping Pong Game

Circuit Connections – 

The circuit is built on Arduino UNO. The project device work as 2-player gamepad. There are four tactile switches connected to the Arduino board that serve as the gamepad buttons. The switches are connected at following Arduino pins with stated functions assigned to them – 
 
Table listing pin functions of Arduino Uno for Ping Pong Game
 
Fig. 4: Table listing pin functions of Arduino Uno for Ping Pong Game
 
The Arduino buttons by default are connected to VCC through 1K Ω pull-up resistors and short circuit to ground via tactile switches interfaced to them. So, on pressing a switch, the Arduino sketch detects a LOW logic from the respective pin. There are four LEDs connected to pins 3, 4, 5 and 6 of the Arduino board which indicate the following player actions – 
 
Table listing pin functions of Arduino Uno for Ping Pong Game
 
Fig. 5: Table listing pin functions of Arduino Uno for Ping Pong Game
 
The LEDs are interfaced with their anode connected the Arduino pins and cathode connected to ground. Therefore, when a HIGH logic is an output by an Arduino pin, the respective LED glows.  
 
Power Supply – The gamepad can be powered by any battery. The DC voltage of the used battery is regulated to 5V DC using 7805 IC. The IC has three pins – pin 1 should be connected to the anode of the battery, pin 2 and 3 with the cathode (common ground). The 5V DC should be drawn from the pin 3 of the IC. An LED along with a 10K Ω pull-up resistor can also be connected between common ground and output pin to get a visual hint of supply continuity.

How the circuit works

Once the gamepad is connected to a battery, the Arduino sketch loads the required libraries and wait for a key press which is detected by the input of LOW logic at the respective Arduino pins. On detecting a key press, the Arduino sketch sends an integer between 1 and 4 to the computer through the virtual serial port and set a HIGH logic at the corresponding LED connected pin to indicate user action. The Arduino sends the following numeric values on virtual serial port on the key press at the respective pins – 
 
Table listing Arduino pins and corresponding serial messages
 
Fig. 6: Table listing Arduino pins and corresponding serial messages 
 
The processing language code on desktop creates the GUI of the game and the application created waits for reception of an integer value from the virtual serial port. On receiving a numeric value between 1 and 4, it moves the either paddle on the screen up or down. The processing language code detects if the ping pong ball collides with a wall and increases a point of the opposite player. If any one of the players gets 4 points first, the game is terminated and the player to gain the points first is declared the winner.

Programming Guide –

Arduino Sketch
 
The Arduino code is burnt to the Arduino UNO. It detects the user actions by tactile switches and operates LEDs and send a number on virtual serial port for gamepad functioning. First the required libraries need to be imported for sending data on virtual serial port. 
 
#include <SoftwareSerial.h>// import the serial library
 
Integer variables are declared and assigned to pins having tactile switches and LEDs interfaced to them.  
 
//Set Pin Numbers
const int button1 = 10; 
const int button2 = 11; 
const int button3 = 8; 
const int button4 = 9;
const int led1 = 3;
const int led2 = 4;
const int led3 = 5; 
const int led4 = 6;
 
A setup() function is called in which the serial transmission rate is set to 9600 bits per seconds using Serial.begin() function. The LED connected pins are set to digital output and switch connected pins are set to digital input using pinMode() function.  
 
Void setup() {
//Initialize Serial Communication at 9600 bits per second
Serial.begin(9600); 
//Initialize the LED pin as an output
pinMode(led1, OUTPUT); 
pinMode(led2, OUTPUT); 
pinMode(led3, OUTPUT); 
pinMode(led4, OUTPUT);
//Initialize the pushbutton pin as an input
pinMode(button1, INPUT);  
pinMode(button2, INPUT); 
pinMode(button3, INPUT);  
pinMode(button4, INPUT);   
}
 
A loop() function is called in which the user input through pushbuttons is detected using digitalRead() function and compared to LOW logic. On comparison from each switch, respective number is serially transmitted using Serial.Write() function and respective LED is set to ON by sending a HIGH logic using digitalWrite() function. The default case is set to send number 0 on serial port and set all the LEDs OFF. 
 
Void loop() {
if(digitalRead(button1)==0)
Serial.write(1); 
digitalWrite(led1, HIGH); 
}
else if(digitalRead(button2)==0)
Serial.write(2); 
digitalWrite(led2, HIGH); 
}
else if(digitalRead(button3)==0)
Serial.write(3); 
digitalWrite(led3, HIGH); 
}
else if(digitalRead(button4)==0)
Serial.write(4); 
digitalWrite(led4, HIGH); 
}
else {
Serial.write(0); 
digitalWrite(led1, LOW); 
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
}
delay(400);
}
 
Processing Language Sketch 
In the processing language code first, the serial library is imported and an object of a serial class is declared. The variables are declared to hold the paddle dimensions and position on the screen. Variables to hold player scores and color values for left and right paddle in the RGB format are declared. 
 
Screenshot of initialization in Processing Language Code for Arduino based Ping Pong Game
 
Fig. 7: Screenshot of initialization in Processing Language Code for Arduino based Ping Pong Game 
 
Similar to Arduino code, a setup function is declared in which baud rate for serial communication is set and functions defining screen size, text size, text alignment and text position are called. The size and position of the paddle are declared with respect to the screen size and speed of the ball in X and Y axis is pre-defined. 
 
Screenshot of Setup Function in Processing Language Code for Arduino based Ping Pong Game
 
Fig. 8: Screenshot of Setup Function in Processing Language Code for Arduino based Ping Pong Game
 
A draw() function is called in which the functions creating the graphic interface of the game and functions detecting user action and implementing a graphical and logical change accordingly are called.
 
 
  Screenshot of Draw Function in Processing Language Code for Arduino based Ping Pong Game
 
Fig. 9: Screenshot of Draw Function in Processing Language Code for Arduino based Ping Pong Game
 
The movingPaddle() function fill the paddles with assigned colors and detects number received on the virtual serial port to change the position of paddles vertically. 
 
Screenshot of Moving Paddle Function in Processing Language Code for Arduino based Ping Pong Game
 
Fig. 10: Screenshot of Moving Paddle Function in Processing Language Code for Arduino based Ping Pong Game
 
The paddleCONCT() function detects the contact of the ball with the paddle by manipulating paddle and ball position on the screen. A similar way, the bounceBack() function detects contact of the ball with the screen wall.
 
Screenshot of PaddleCONCT Function in Processing Language Code for Arduino based Ping Pong Game
 
Fig. 11: Screenshot of PaddleCONCT Function in Processing Language Code for Arduino based Ping Pong Game
 
The Score() function updates the score on screen by displaying the values of variables ScoreL and ScoreR. The gameOver() function detects if either player has got the winning score. The gameOverPage() function initializes the variables holding the position of the ball and the paddle to default values and displays the winner on screen. The function initiates a game restart on mouse click.  
 
Screenshot of Score Function in Processing Language Code for Arduino based Ping Pong Game
 
Fig. 12: Screenshot of Score Function in Processing Language Code for Arduino based Ping Pong Game
 
The movingBall() function moves the ball (circle) at a speed specified in speedX and speedY variables. The restart() function sets the variables to initial values for a game restart. 
 
Screenshot of movingBall Function in Processing Language Code for Arduino based Ping Pong Game
 
Fig. 13: Screenshot of movingBall Function in Processing Language Code for Arduino based Ping Pong Game

Project Source Code

###

//Program to
​/*

 * Program for filtering the X-axis values of accel and transmitting it serially

 * Programmed by B.Aswinth Raj

 * Dated: 21-08-206

 */

#include <SoftwareSerial.h>// import the serial library 

#include <LiquidCrystal.h>//import the LCD library

SoftwareSerial testserial(10, 11); // RX, TX

LiquidCrystal lcd(13, 12, 5, 4, 3, 2);// Pins used for RS,E,D4,D5,D6,D7

#define AccelPin      A0         // A0 is connected to X-axis of Accel

#define Samplesize   13         // filterSample number

int BTData; // the data received from the app store here

int Array1 [Samplesize];           // array for holding raw sensor values for sensor

int rawData1, smoothData1;      // variables for sensor data

int toSend;

void setup(){

  Serial.begin(9600);

  testserial.begin(9600); //Software serial initialization

  lcd.begin(16,2);//LCD 16x2 initialization

  lcd.setCursor(0,0); //Initially set the cursor position of LCD to 1st Columb 1st row.

  lcd.print("Engineers Garage");//After initialising print data

  lcd.setCursor(0,1); //Initially set the cursor position of LCD to 1st Columb 2nd row.

   lcd.print("               ");  //print blank to clear all the data on LCD

   delay(3000);

   lcd.setCursor(0,0);

   lcd.print(" BlueTooth Game ");

   lcd.setCursor(0,1);

   lcd.print(" Accelerometer  ");  

   delay(3000);

}

void loop()

{     

  rawData1 = analogRead(AccelPin); // read X-axis of accelerometer

  //BTData=testserial.read(); // Read the dat store it in the variable.

  smoothData1 = digitalSmooth(BTData, Array1);  

  toSend = map (smoothData1, 193, 280, 0, 255);  // the data from accelerometer mapped to form a byte

  Serial.write (toSend);

  delay(100);

}

  int digitalSmooth(int rawIn, int *sensSmoothArray){   

// "int *sensSmoothArray" passes an array to the function - the asterisk indicates the array name is a pointer

  int j, k, temp, top, bottom;

  long total;

  static int i;

  static int sorted[Samplesize];

  boolean done;

  i = (i + 1) % Samplesize;  // increment counter and roll over if necc. -  % (modulo operator) rolls over variable

  sensSmoothArray[i] = rawIn;   // input new data into the oldest slot

  for (j=0; j<Samplesize; j++){  // transfer data array into anther array for sorting and averaging

    sorted[j] = sensSmoothArray[j];

  }

  done = 0;                    // flag to know when we're done sorting              

  while(done != 1){        // simple swap sort, sorts numbers from lowest to highest

    done = 1;

    for (j = 0; j < (Samplesize - 1); j++){

      if (sorted[j] > sorted[j + 1]){        // numbers are out of order - swap

        temp = sorted[j + 1];

        sorted [j+1] =  sorted[j] ;

        sorted [j] = temp;

        done = 0;

      }

    }

  }

  bottom = max(((Samplesize * 15)  / 100), 1); 

  top = min((((Samplesize * 85) / 100) + 1  ), (Samplesize - 1));   

// the + 1 is to make up for asymmetry caused by integer rounding

  k = 0;

  total = 0;

  for ( j = bottom; j< top; j++){

    total += sorted[j];  // total remaining indices

    k++; 

  }

  return total / k;   // divide by number of samples

}

###


Circuit Diagrams

Circuit-Diagram-Arduino-Based-Ping-Pong-Game

Project Video


Filed Under: Electronic Projects

 

Next Article

← Previous Article
Next Article →

Questions related to this article?
👉Ask and discuss on EDAboard.com and Electro-Tech-Online.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

EE TECH TOOLBOX

“ee
Tech Toolbox: 5G Technology
This Tech Toolbox covers the basics of 5G technology plus a story about how engineers designed and built a prototype DSL router mostly from old cellphone parts. Download this first 5G/wired/wireless communications Tech Toolbox to learn more!

EE Learning Center

EE Learning Center
“engineers
EXPAND YOUR KNOWLEDGE AND STAY CONNECTED
Get the latest info on technologies, tools and strategies for EE professionals.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


RSS EDABOARD.com Discussions

  • Finding past posts on edaboard?
  • Industrial Relay Board Design for Motorcycle Use
  • I think i have devised a new type of "super_transformer" for the Electricity grid?
  • What is the purpose of this relay?
  • mosfet driver problem in regeneration mode

RSS Electro-Tech-Online.com Discussions

  • Pic18f25q10 osccon1 settings swordfish basic
  • Sump pit water alarm - Kicad 9
  • Anyone jumped from Easyeda std to Easyeda pro?
  • turbo jet fan - feedback appreciated.
  • More fun with ws2812 this time XC8 and CLC

Featured – LoRa/LoRaWan Series

  • What is the LoRaWAN network and how does it work?
  • Understanding LoRa architecture: nodes, gateways, and servers
  • Revolutionizing RF: LoRa applications and advantages
  • How to build a LoRa gateway using Raspberry Pi
  • How LoRa enables long-range communication
  • How communication works between two LoRa end-node devices

Recent Articles

  • How IoT network topologies work
  • The top five AI startups to watch in 2025
  • STMicroelectronics unveils SoC based on secure MCU
  • Nexperia’s 48 V ESD diodes support higher data rates with ultra-low capacitance design
  • Taoglas releases Patriot antenna with 18 integrated elements covering 600 to 6000 MHz

EE ENGINEERING TRAINING DAYS

engineering

Submit a Guest Post

submit a guest post
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • Battery Power Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • EV Engineering
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • Subscribe to our newsletter
  • About Us
  • Contact Us
  • Advertise

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy

Search Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe