Projects / Two-player Pong on OLED

Two-player Pong on OLED

Beginner1 hGuided buildReviewed

Build it step by step

0 of 6 done
  1. NANO

    Wire the display

    SDA to A4, SCL to A5, VCC to 5V, GND to GND.

  2. NANO

    Wire the pots

    Each pot's outer pins to 5V and GND. Middle pins to A0 (left) and A1 (right).

  3. NANO

    Add the buzzer

    Positive to D3, negative to GND.

  4. NANO10.3 mASSD1306

    Install libraries

    Add Adafruit SSD1306 and Adafruit GFX.

  5. NANO

    Upload

    You should see two paddles and a moving ball.

  6. NANO

    Play

    Turn the pots to move the paddles. First to five wins.

In short

How do you build a two-player Pong on OLED?

Two pots move the paddles, a ball bounces around a 128 × 64 OLED, and the game keeps score to five with sound effects. You need Arduino Nano, SSD1306 OLED Display, Potentiometer 10 kΩ × 2, Passive buzzer, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 6 steps and about 60 minutes.

Level
Beginner
Time
About 60 min
Parts
6

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
OLED SDA / SCLNano A4 / A5I2C display
OLED VCC / GNDNano 5V (or 3.3V) / GNDPower
Pot 1 middle pin (left player)Nano A0Left paddle, outer pins to 5V and GND
Pot 2 middle pin (right player)Nano A1Right paddle, outer pins to 5V and GND
Passive buzzer + / −Nano D3 / GNDHit sounds

Code

Arduino IDE, board "Arduino Nano" (processor ATmega328P, or ATmega328P (Old Bootloader) for clones). Install "Adafruit SSD1306" and "Adafruit GFX Library" from the Library Manager. If nothing shows, try the address 0x3D.

Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

Adafruit_SSD1306 d(128, 64, &Wire, -1);
const byte BUZ = 3, PH = 14;
int bx = 64, by = 32, vx = 2, vy = 1, s1 = 0, s2 = 0;
int p1 = 25, p2 = 25;

void serve() { bx = 64; by = 32; vx = (random(2) ? 2 : -2); vy = random(-2, 3); if (vy == 0) vy = 1; }

void setup() {
  d.begin(SSD1306_SWITCHCAPVCC, 0x3C);        // some modules are 0x3D
  randomSeed(analogRead(A2));
  serve();
}

void loop() {
  p1 = map(analogRead(A0), 0, 1023, 0, 64 - PH);
  p2 = map(analogRead(A1), 0, 1023, 0, 64 - PH);
  bx += vx; by += vy;

  if (by <= 0 || by >= 61) { vy = -vy; tone(BUZ, 600, 20); }               // top and bottom walls
  if (bx <= 6 && bx >= 2 && by + 3 >= p1 && by <= p1 + PH) { vx = abs(vx); tone(BUZ, 900, 20); }
  if (bx >= 119 && bx <= 123 && by + 3 >= p2 && by <= p2 + PH) { vx = -abs(vx); tone(BUZ, 900, 20); }
  if (bx < 0)   { s2++; tone(BUZ, 200, 200); serve(); delay(500); }       // right player scores
  if (bx > 125) { s1++; tone(BUZ, 200, 200); serve(); delay(500); }

  d.clearDisplay();
  d.fillRect(2, p1, 3, PH, WHITE);
  d.fillRect(123, p2, 3, PH, WHITE);
  d.fillRect(bx, by, 3, 3, WHITE);
  for (byte y = 0; y < 64; y += 8) d.drawFastVLine(64, y, 4, WHITE);        // dashed centre line
  d.setTextSize(2); d.setTextColor(WHITE);
  d.setCursor(40, 0); d.print(s1);
  d.setCursor(80, 0); d.print(s2);
  d.display();

  if (s1 == 5 || s2 == 5) {                                                  // game over
    d.clearDisplay(); d.setCursor(10, 24);
    d.print(s1 == 5 ? "P1 WINS" : "P2 WINS");
    d.display(); delay(3000);
    s1 = s2 = 0; serve();
  }
  delay(20);
}

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Turn a potThat player's paddle moves smoothly from top to bottom
Ball hits a paddleIt bounces back with a beep
Ball passes a paddleA low tone, the other score goes up, and a new serve
First to 5Winner message for 3 seconds, then the game restarts

If it does not work

The screen is blank

Try the address 0x3D, check SDA and SCL, and install both Adafruit libraries.

The ball goes through paddles

Adjust the collision limits: the ball is fast, so lower vx to 1 or check the paddle edge values.

The paddles jitter

Add a 100 nF capacitor from each pot's middle pin to GND.

A paddle moves backwards

Swap the pot's outer wires.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the two-player Pong on OLED?

Arduino Nano, SSD1306 OLED Display, Potentiometer 10 kΩ × 2, Passive buzzer, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).

How long does the two-player Pong on OLED take to build?

About 60 minutes for a beginner maker, in 6 steps.

How do I know the two-player Pong on OLED works?

Turn a pot: That player's paddle moves smoothly from top to bottom. Ball hits a paddle: It bounces back with a beep. Ball passes a paddle: A low tone, the other score goes up, and a new serve.

The screen is blank: what should I check?

Try the address 0x3D, check SDA and SCL, and install both Adafruit libraries.

The ball goes through paddles: what should I check?

Adjust the collision limits: the ball is fast, so lower vx to 1 or check the paddle edge values.

The paddles jitter: what should I check?

Add a 100 nF capacitor from each pot's middle pin to GND.

A paddle moves backwards: what should I check?

Swap the pot's outer wires.