Community project

Dois Jogadores Pong Interativo

Photo of Dois Jogadores Pong Interativo
Generated with AI

Eduardo Fraga

Published November 29, 2026

Build a two-player Pong game on an Arduino Uno with dual PS2 joysticks controlling paddles on an OLED display. This interactive project brings classic arcade gameplay to a microcontroller, complete with real-time ball physics, scoring, and audio feedback from a piezo buzzer.

The guide provides a complete wiring diagram showing how to connect both joystick modules, the 128x64 I2C OLED screen, and the buzzer to the Arduino Uno. Assembly takes minutes with straightforward pin connections, and the included firmware handles all game logic, paddle movement, collision detection, and sound effects.

Wiring diagram

Wiring diagram for Dois Jogadores Pong Interativo
Loading diagram…

Gather all the parts

QtyComponent
1

PS2 Joystick Module (KY-023)

Two-axis analog thumb joystick module (KY-023 / PS2 style) with a push switch under the stick. Two 10 kOhm potentiometers give X and Y voltages that sit near half the supply at rest, and pressing the stick operates a digital switch. It runs from 3.3 to 5 V and connects through a five-pin GND, +5V, VRx, VRy, SW right-angle header.

1

PS2 Joystick Module (KY-023)

Two-axis analog thumb joystick module (KY-023 / PS2 style) with a push switch under the stick. Two 10 kOhm potentiometers give X and Y voltages that sit near half the supply at rest, and pressing the stick operates a digital switch. It runs from 3.3 to 5 V and connects through a five-pin GND, +5V, VRx, VRy, SW right-angle header.

1

OLED SSD1306 amarelo/azul 128x64 I2C (5 V)

Módulo do usuário com alimentação e sinais compatíveis com 5 V conforme a ficha informada.

1

Piezo Buzzer

Small passive piezo buzzer (TDK PS1240) for beeps, tones and alerts. It has no built-in oscillator, so drive one lead with a 3-30 V square wave from a microcontroller pin using tone() or PWM and connect the other lead to ground. It is loudest around 4 kHz and works from about 2 to 10 kHz; driving both leads in antiphase roughly doubles the volume.

1

Resistor

220 Ω

Through-hole resistor (current-limiting in series with an LED)

Assemble it in 3 steps

1. Ligue os dois joysticks com o USB desconectado

Nos dois KY-023, ligue +5V a 5V e GND a GND. No jogador 1, ligue VRX a A2, VRY a A0 e SW a D2. No jogador 2, ligue VRX a A3, VRY a A1 e SW a D3. VRX e VRY leem a direção, e SW lê o clique.

2. Ligue a tela OLED diretamente ao Uno

Para o módulo confirmado com alimentação e sinais compatíveis com 5 V, ligue VCC (ou VDD) a 5V, GND a GND, SDA (dados) a A4 e SCL (ou SCK, relógio) a A5. Não é necessário conversor.

3. Ligue o buzzer passivo

Ligue D8 a uma ponta do resistor de 220 Ω e a outra ponta ao positivo do buzzer. Ligue o negativo ao GND. Depois de conferir as ligações, conecte o USB com as duas alavancas no centro.

Review all connections

1. Connections between "j1" and "Arduino"

Functionj1Arduino
power+5V5V
groundGNDGND
analogVRYGPIO 14
digitalSWGPIO 2
analogVRXGPIO 16

2. Connections between "j2" and "Arduino"

Functionj2Arduino
power+5V5V
groundGNDGND
analogVRYGPIO 15
digitalSWGPIO 3
analogVRXGPIO 17

3. Connections between "oled" and "Arduino"

FunctionoledArduino
powerVCC5V
groundGNDGND
i2cSDAGPIO 18
i2cSCLGPIO 19

4. Connections between "piezo" and "Arduino"

FunctionpiezoArduino
pwmSIG → Resistor P2EXT
groundGNDGND

5. Connections between "r1" and "Arduino"

Functionr1Arduino
digitalP1GPIO 8

Deploy the firmware

#include <Arduino.h>
#include <U8g2lib.h>
U8G2_SSD1306_128X64_NONAME_1_HW_I2C screen(U8G2_R0,U8X8_PIN_NONE);
// Uno: OLED module rated for 5 V, hardware I2C on A4 (SDA) and A5 (SCL).
const int axes[]={A2,A3}, buzzer=8;
int paddle[]={32,32}, center[2], score[2]={};
bool ready[2]={}, upHeld[2]={};
uint32_t since, tick, serveAt, matchAt;
int state=0; // intro, ready, match, result
const uint8_t clicks[]={2,3};
bool hard=false, clicked[2]={}, rawClick[2]={};
uint32_t clickChanged[2]={};
struct Ball {float x,y,dx,dy; uint32_t serveAt;};
Ball balls[2];
void serve(int i,int dir,uint32_t now){balls[i]={64.f,i?46.f:30.f,2.f*dir,i?1.f:-1.f,now+650};}
// 4: menu, 5: duelo, 6: explosao final, 7: trofeu.
const uint8_t vertical[]={A0,A1};
int16_t verticalCenter[2];
struct Ship {float px,py,vx,vy; uint8_t lives, aim, ttl;};
Ship ships[2];
const int8_t aimX[]={1,1,0,-1,-1,-1,0,1};
const int8_t aimY[]={0,1,1,1,0,-1,-1,-1};
float orbX,orbY,orbDX,orbDY;
uint8_t choice, champion, explosionFrame;
uint32_t chordAt;
bool chord, chordUsed, pendingClick;
constexpr int tankY=38, tankRadius=9, barrelTip=9, barrelLift=1;
float aimScale(uint8_t d){return (d&1)?0.70710678f:1.f;}
int shipX(uint8_t i){return i?115:12;}
float orbSpeed;
void resetOrb(uint8_t direction){
 orbX=64;orbY=38;orbSpeed=.65f;
 orbDX=direction?-.46f:.46f;orbDY=direction?.46f:-.46f;
}
void startShips(uint32_t now){
 for(uint8_t i=0;i<2;i++){ships[i]={};ships[i].lives=5;ships[i].aim=i?4:0;}
 resetOrb(0);state=5;tick=now;
}
void burst(int x,int y,int r){
 screen.drawCircle(x,y,r);
 for(int k=-1;k<=1;k++)for(int j=-1;j<=1;j++)if(k||j)
  screen.drawLine(x+k*(r+2),y+j*(r+2),x+k*(r+4),y+j*(r+4));
}
bool updateShips(uint32_t now,const bool hit[]){
 bool changed=false;
 for(uint8_t i=0;i<2;i++){
  Ship &s=ships[i];
  int x=analogRead(axes[i])-center[i], y=analogRead(vertical[i])-verticalCenter[i];
  // Keep the joystick wiring conventions already used by this project.
  int ax=abs(x)>100 && abs(x)*2>=abs(y)?(x>0?1:-1):0;
  int ay=abs(y)>100 && abs(y)*2>=abs(x)?(y>0?1:-1):0;
  for(uint8_t d=0;d<8;d++)if(ax==aimX[d]&&ay==aimY[d]&&s.aim!=d){s.aim=d;changed=true;break;}
  if(hit[i]){
   int dx=aimX[s.aim],dy=aimY[s.aim];
   float unit=aimScale(s.aim);
   s.px=shipX(i)+dx*unit*(barrelTip+3);s.py=tankY-barrelLift+dy*unit*(barrelTip+3);
   float speed=3.5f*unit;
   s.vx=dx*speed;s.vy=dy*speed;s.ttl=90;
   tone(buzzer,1500,30);changed=true;
  }
 }
 if(now-tick<24)return changed;
 tick=now;
 for(auto &s:ships)if(s.ttl){
  --s.ttl;s.px+=s.vx;s.py+=s.vy;changed=true;
  if(s.px<=2||s.px>=125){s.px=constrain(s.px,2.f,125.f);s.vx=-s.vx;}
  if(s.py<=14||s.py>=61){s.py=constrain(s.py,14.f,61.f);s.vy=-s.vy;}
  for(uint8_t i=0;i<2;i++){
   float x=s.px-shipX(i),y=s.py-tankY;
   if(x*x+y*y<=(tankRadius+2)*(tankRadius+2)){s.ttl=0;tone(buzzer,480,40);break;}
  }
  if(!s.ttl)continue;
  float x=s.px-orbX,y=s.py-orbY;
  if(x*x+y*y<=36){
   orbSpeed=min(orbSpeed+.35f,3.8f);
   orbDX=s.vx*(orbSpeed/3.5f);orbDY=s.vy*(orbSpeed/3.5f);
   s.ttl=0;tone(buzzer,720,55);
  }
 }
 if(orbDX!=0||orbDY!=0){
  changed=true;orbX+=orbDX;orbY+=orbDY;
  if(orbY<=16||orbY>=59){orbY=constrain(orbY,16.f,59.f);orbDY=-orbDY;}
  for(uint8_t i=0;i<2;i++){
   float x=orbX-shipX(i),y=orbY-tankY;
   float d2=x*x+y*y, approach=orbDX*x+orbDY*y;
   const int contact=tankRadius+4;
   if(d2>0 && d2<contact*contact && approach<0){
    float push=contact/sqrtf(d2);
    orbX=shipX(i)+x*push;orbY=tankY+y*push;
    orbDX-=2*approach*x/d2;orbDY-=2*approach*y/d2;
    tone(buzzer,510,40);
   }
  }
  if(orbX<=4||orbX>=123){
   uint8_t loser=orbX<=4?0:1;
   if(--ships[loser].lives==0){champion=2-loser;state=6;since=now;explosionFrame=0;tone(buzzer,180,100);}
   else {resetOrb(loser);tone(buzzer,380,120);}
   ships[0].ttl=ships[1].ttl=0;
  }
 }
 return changed;
}
void shipBoard(){
 screen.setCursor(0,9);screen.print(F("V"));screen.print(ships[0].lives);screen.print(F(" P"));screen.print(5-ships[1].lives);
 screen.setCursor(98,9);screen.print(F("V"));screen.print(ships[1].lives);screen.print(F(" P"));screen.print(5-ships[0].lives);
 screen.drawFrame(0,11,128,53);
 for(uint8_t y=15;y<62;y+=6)screen.drawVLine(64,y,2);
 if(state==5)screen.drawCircle((int)orbX,(int)orbY,4);
 for(uint8_t i=0;i<2;i++){
  const Ship &s=ships[i];int x=shipX(i),y=tankY;
  if(!s.lives){
   for(uint8_t k=0;k<4;k++){
    int age=explosionFrame-k*2;
    if(age>=0&&age<7)burst((i?72:8)+(k*17)%47,21+(k*13)%33,2+age);
   }
   continue;
  }
  // Draw only the front half; collisions still use the full circle.
  int dx=aimX[s.aim],dy=aimY[s.aim];
  for(int py=-barrelTip;py<=barrelTip;py++){
   for(int px=-barrelTip;px<=barrelTip;px++){
    int forward=px*dx+py*dy, side=px*dy-py*dx;
    int norm=(s.aim&1)?2:1;
    bool body=forward>=0 && px*px+py*py<=tankRadius*tankRadius;
    bool barrel=forward>=0 && forward*forward<=barrelTip*barrelTip*norm && side*side<=4*norm;
    if(body)screen.drawPixel(x+px,y+py);
    if(barrel)screen.drawPixel(x+px,y+py-barrelLift);
   }
  }
  if(state==5&&s.ttl)screen.drawDisc((int)s.px,(int)s.py,2);
 }
}
void render(){
 screen.firstPage(); do { screen.setFont(u8g2_font_6x10_tf);
 if(state==0){screen.setFont(u8g2_font_ncenB18_tr);screen.drawStr(27,32,"PONG");screen.setFont(u8g2_font_6x10_tf);screen.drawStr(28,51,"2 JOGADORES");}
 if(state==1){screen.drawStr(10,13,"MOVA PARA CIMA");for(int i=0;i<2;i++) {screen.setCursor(13,30+12*i);screen.print(i==0?"J1: ":"J2: ");screen.print(ready[i]?"PRONTO":"AGUARDANDO");}screen.drawStr(4,60,hard?"CLIQUE: MODO NORMAL":"CLIQUE: MODO DIFICIL");}
 if(state==2){screen.setCursor(49,9);screen.print(score[0]);screen.print(" - ");screen.print(score[1]);for(int a=14;a<64;a+=6)screen.drawVLine(64,a,2);screen.drawBox(3,paddle[0]-7,3,15);screen.drawBox(122,paddle[1]-7,3,15);for(int i=0;i<(hard?2:1);i++)screen.drawDisc((int)balls[i].x,(int)balls[i].y,2);}
 if(state==3){screen.drawStr(23,21,score[0]==5?"JOGADOR 1 GANHOU":"JOGADOR 2 GANHOU");screen.drawStr(10,49,"Cima para voltar");}
  if(state==4){screen.drawStr(46,11,"JOGOS");screen.drawStr(16,29,choice?"  PONG":"> PONG");screen.drawStr(16,43,choice?"> NAVES":"  NAVES");screen.drawStr(1,62,"Cima: troca SW: joga");}
 if(state==5||state==6)shipBoard();
 if(state==7){if(champion){screen.setCursor(52,12);screen.print(F("J"));screen.print(champion);screen.drawFrame(51,17,26,9);screen.drawBox(56,17,16,18);screen.drawBox(62,34,4,9);screen.drawBox(54,43,20,4);}else screen.drawStr(43,30,"EMPATE");screen.drawStr(10,62,"Cima: menu Jogos");}
 } while (screen.nextPage());
}
void setup(){for(int i=0;i<2;i++){pinMode(clicks[i],INPUT_PULLUP);center[i]=analogRead(axes[i]);verticalCenter[i]=analogRead(vertical[i]);}screen.setBusClock(400000);screen.begin();render();since=millis();tone(buzzer,880,120);}
void loop(){
 uint32_t now=millis();bool up[2]={};bool redraw=false, press=false, hit[2]={};
 for(int i=0;i<2;i++){
  bool down=digitalRead(clicks[i])==LOW;
  if(down!=rawClick[i]){rawClick[i]=down;clickChanged[i]=now;}
  if(down!=clicked[i] && now-clickChanged[i]>=30){clicked[i]=down;hit[i]=down;press|=down;}
  int d=analogRead(axes[i])-center[i];
  if(d > -80) upHeld[i]=false;
  if(d < -180 && !upHeld[i]){upHeld[i]=true;up[i]=true;}
 }
 // A dupla pressionada tem prioridade sobre cliques individuais.
 if(rawClick[0] && rawClick[1]){
  pendingClick=false;
  if(!chord){chord=true;chordAt=now;}
  if(!chordUsed && now-chordAt>=2000){chordUsed=true;state=4;choice=0;render();}
  if(state!=5)return;
 }else if(chord){
  if(rawClick[0]||rawClick[1]||clicked[0]||clicked[1]){if(state!=5)return;}else{
  chord=false;chordUsed=false;pendingClick=false;
  if(state!=5)return;
  }
 }
 if(state==1||state==4){
  pendingClick|=press;
  press=pendingClick&&!clicked[0]&&!clicked[1];
  if(press)pendingClick=false;
 }
 if(state==4){
  if(up[0]||up[1]){choice^=1;redraw=true;}
  if(press){
   if(choice)startShips(now);
   else {state=1;hard=false;ready[0]=ready[1]=false;}
   redraw=true;
  }
 }else if(state==5){redraw=updateShips(now,hit);}
 else if(state==6){
  uint32_t elapsed=now-since;
  if(elapsed>=1050){state=7;redraw=true;}
  else if(explosionFrame!=elapsed/90){
   explosionFrame=elapsed/90;redraw=true;
   tone(buzzer,90+(explosionFrame*73)%170,72);
  }
 }else if(state==7 && (up[0]||up[1])){state=4;choice=1;redraw=true;}
 if(state==0 && now-since>=1600){state=1;redraw=true;}
 else if(state==1){if(press){hard=!hard;redraw=true;tone(buzzer,hard?1250:750,80);}for(int i=0;i<2;i++)if(up[i]&&!ready[i]){ready[i]=true;redraw=true;tone(buzzer,1050,65);}if(ready[0]&&ready[1]&&!pendingClick&&!rawClick[0]&&!rawClick[1]){state=2;matchAt=now;score[0]=score[1]=0;paddle[0]=paddle[1]=36;serve(0,1,now);if(hard)serve(1,-1,now);redraw=true;}}
 else if(state==3 && now-since>=600 && (up[0]||up[1])){hard=false;ready[0]=ready[1]=false;state=1;redraw=true;}
 if(state==2 && now-tick>=25){tick=now;
  for(int i=0;i<2;i++){int d=analogRead(axes[i])-center[i];int p=constrain(paddle[i]+(abs(d)>75?(d>0?3:-3):0),19,56);redraw|=p!=paddle[i];paddle[i]=p;}
  for(int b=0;b<(hard?2:1)&&state==2;b++){
   Ball &ball=balls[b];
   if((int32_t)(now-ball.serveAt)<0)continue;
   float &x=ball.x,&y=ball.y,&dx=ball.dx,&dy=ball.dy;
   redraw=true;
   // Ramp the ball speed every 10 seconds, without resetting on a point.
   float speed=1.f+0.2f*min((now-matchAt)/10000UL,5UL);
   x+=dx*speed;y+=dy*speed;if(y<=13||y>=61){y=constrain(y,13.f,61.f);dy=-dy;tone(buzzer,540,35);}
   if((dx<0&&x<=8&&x>=3&&fabsf(y-paddle[0])<=9)||(dx>0&&x>=120&&x<=125&&fabsf(y-paddle[1])<=9)){int i=dx<0?0:1;dx=-dx;dy=constrain((y-paddle[i])*0.28f,-2.5f,2.5f);tone(buzzer,780,50);}
   if(x<2||x>125){int winner=x<2?1:0;score[winner]++;tone(buzzer,score[winner]==5?1300:380,160);if(score[winner]==5){state=3;since=now;}else serve(b,winner==0?-1:1,now);}
  }
 }
 if(redraw)render();
}

Remix this project

Make it yours in one click

Open a full copy of this project in your own Schematik workspace, diagram, code, parts, and assembly steps included. Swap the sensor, add features, or redesign the whole thing with AI. The author's original stays untouched.

Open in Schematik