Build a retro handheld game console powered by an ESP32 microcontroller, 128x64 OLED display, and analog joystick. This compact device runs eight classic arcade-style games including Snake, Pong, Dodge, 3D Maze, Flight Sim, Dino, Flappy Bird, and City Driver—all rendered on a crisp monochrome screen with responsive joystick controls.
This guide provides a complete wiring diagram showing how to connect the SSD1306 OLED screen and KY-023 joystick module to the ESP32, a full parts list, step-by-step assembly instructions, and pre-loaded firmware with all eight games ready to play. Follow the assembly outline to wire the components, secure everything in an enclosure, and start gaming immediately.
Wiring diagram

Gather all the parts
Assemble it in 4 steps
1. Connect the OLED screen
Unplug USB. Connect OLED VCC to 3V3 and GND to GND. Connect SDA to GPIO21 (data) and SCL to GPIO22 (clock).
- Read the pin labels; some screens swap VCC and GND. Reversed power can damage the screen.
2. Connect the joystick
Connect the joystick pin marked +5V to ESP32 3V3, and GND to GND. Connect VRX to GPIO32 (left/right), VRY to GPIO33 (up/down), and SW to GPIO27 (stick click).
- Use 3V3, not 5V, despite the joystick label. A 5V joystick output can damage the ESP32 inputs.
3. Secure and check the console
Use a breadboard and jumper wires. Check that both modules share ESP32 ground. Keep exposed pins from touching. Leave the joystick centered when powering the board so it can learn the resting position.
- Orient the joystick so pushing right and down matches the game directions; rotate its mounting if needed.
4. Play City Driver
With USB unplugged, disconnect and remove the MPU-6050 and its five wires. It is no longer needed. In City Driver, push up to accelerate, down to reverse, and left or right to steer. Reach the plus markers for deliveries. Avoid the police cross after your first delivery. Click to pause; hold the button to return to the menu. Flight also uses the joystick.
Review all connections
1. Connections between "oled" and "ESP32"
2. Connections between "joystick" and "ESP32"
Deploy the firmware
#include <Arduino.h>
#include <Wire.h>
#include <GyverOLED.h>
#include <new>
#include <math.h>
constexpr int X_PIN=32,Y_PIN=33,SW_PIN=27;
GyverOLED<SSD1306_128x64, OLED_BUFFER> screen;
struct Cell { int8_t x,y; };
Cell snake[192],food;
int length=3,dx=1,dy=0,nextX=1,nextY=0,cx=2048,cy=2048;
enum Mode {MENU,PLAY,PAUSED,OVER,WIN};
Mode mode=MENU;
int game=0,score=0,paddle=30,enemy=30,player=64;
float bx=64,by=32,vx=2,vy=1;
struct Rock {int x,y;}; Rock rocks[6];
const char* names[]={"SNAKE","PONG","DODGE","MAZE 3D","FLIGHT 3D","DINO","FLAPPY BIRD","CITY DRIVER"};
constexpr int GAME_COUNT=8;
float px=1.5f,py=1.5f,angle=0,roll=0,pitch=0,zeroX=0,zeroY=0;
float flightX=0,flightY=0,gateX=0,gateY=0,gateZ=100;
const char* maze[]={"########","#......#","#.##.#.#","#....#.#","###..#.#","#....#.#","#......#","########"};
bool wall(float x,float y){int a=(int)floorf(x),b=(int)floorf(y);return a<0||b<0||a>=8||b>=8||maze[b][a]=='#';}
void text(int x,int y,const char *s);
void drawMaze(){
for(int col=0;col<128;col+=2){float ray=angle+atanf((col-64)/90.0f),d=0;while(d<12&&!wall(px+cosf(ray)*d,py+sinf(ray)*d))d+=0.035f;
float depth=max(0.08f,d*cosf(ray-angle));int h=min(46,(int)(32/depth));int top=38-h/2,bottom=38+h/2;
screen.line(col,top,col,bottom);if(((int)(d*7))%2==0)screen.line(col+1,top,col+1,bottom);
}
text(0,54,"Find exit at 6,6");
}
struct Obstacle {float x,y,z;};
Obstacle obstacles[6];
float jumpY=0,jumpV=0,birdY=34,birdV=0,pipeX=140;
int gapY=34,menuTop=0;
uint32_t menuRepeat=0, flapRepeat=0;
int menuDirection=0;
bool action=false, jumpStickLatched=false;
// Perspective camera with roll, pitch and clipped display coordinates.
void project(float x,float y,float z,int &sx,int &sy){
float u=x-flightX,v=y-flightY;
float c=cosf(roll),sn=sinf(roll);
sx=(int)(64+70*(u*c-v*sn)/z);
sy=(int)(36+70*(u*sn+v*c)/z+pitch*18);
}
void clippedLine(int x,int y,int xx,int yy){
// Cohen-Sutherland clipping protects the OLED library from off-screen points.
auto code=[](int a,int b){return (a<0?1:a>127?2:0)|(b<12?4:b>63?8:0);};
for(int n=0;n<8;n++){int a=code(x,y),b=code(xx,yy);if(!(a|b)){screen.line(x,y,xx,yy);return;}if(a&b)return;
int o=a?a:b;float nx=x,ny=y;
if(o&8){if(yy==y)return;nx=x+(xx-x)*(63.0f-y)/(yy-y);ny=63;}
else if(o&4){if(yy==y)return;nx=x+(xx-x)*(12.0f-y)/(yy-y);ny=12;}
else if(o&2){if(xx==x)return;ny=y+(yy-y)*(127.0f-x)/(xx-x);nx=127;}
else{if(xx==x)return;ny=y+(yy-y)*(0.0f-x)/(xx-x);nx=0;}
if(a){x=(int)nx;y=(int)ny;}else{xx=(int)nx;yy=(int)ny;}}
}
void drawFlight(){
int x,y,u,v;
// Ground grid recedes into the distance.
for(int i=-4;i<=4;i++){project(i*4,8,2,x,y);project(i*4,8,100,u,v);clippedLine(x,y,u,v);}
for(int i=0;i<7;i++){float z=4+i*12-fmodf(score*0.5f,3);project(-20,8,z,x,y);project(20,8,z,u,v);clippedLine(x,y,u,v);}
for(auto &o:obstacles){int ax[8],ay[8];for(int i=0;i<8;i++)project(o.x+((i&1)?1.8f:-1.8f),o.y+((i&2)?1.8f:-1.8f),o.z+((i&4)?4:0),ax[i],ay[i]);
for(int i=0;i<8;i++)for(int bit=1;bit<=4;bit*=2)if(!(i&bit))clippedLine(ax[i],ay[i],ax[i|bit],ay[i|bit]);}
screen.line(42,60,56,53);screen.line(56,53,72,53);screen.line(72,53,86,60);
screen.line(60,36,68,36);screen.line(64,33,64,39);
}
void drawDino(){
screen.line(0,60,127,60);int y=48-(int)jumpY;
screen.rect(17,y+3,25,y+10,OLED_FILL);screen.rect(23,y,30,y+5,OLED_FILL);
screen.dot(28,y+1,OLED_CLEAR);screen.line(17,y+5,12,y+1);
screen.line(19,y+10,19,y+12);screen.line(24,y+10,26,y+12);
for(auto &r:rocks)if(r.x>=0&&r.x<128){screen.rect(r.x,60-r.y,r.x+4,59,OLED_FILL);screen.line(r.x-3,56,r.x+7,56);}
}
void drawFlappy(){
int y=(int)birdY;screen.rect(23,y-3,31,y+3,OLED_FILL);screen.dot(29,y-2,OLED_CLEAR);screen.line(31,y,34,y);
if(pipeX<128&&pipeX>0){int x=(int)pipeX;screen.rect(x,12,min(127,x+12),gapY-11,OLED_FILL);screen.rect(x,gapY+11,min(127,x+12),63,OLED_FILL);}
}
extern bool dirty;
extern uint32_t tick;
// Original top-down city driving game: roads form a 48-pixel grid.
float carX=24,carY=24,carAngle=0,carSpeed=0,copX=216,copY=216;
int targetX=120,targetY=120,deliveries=0;
bool road(float x,float y){
if(x<8||y<8||x>280||y>280)return false;
float a=fmodf(x,48),b=fmodf(y,48);
return (a>=14&&a<=34)||(b>=14&&b<=34);
}
void cityTarget(){do{targetX=24+48*random(0,6);targetY=24+48*random(0,6);}while(hypotf(targetX-carX,targetY-carY)<60);}
void drawCity(){
int ox=(int)carX-64,oy=(int)carY-38;
for(int gy=0;gy<6;gy++)for(int gx=0;gx<6;gx++){
int x=gx*48+35-ox,y=gy*48+35-oy;
if(x<128&&x+26>=0&&y<64&&y+26>=12){
clippedLine(x,y,x+26,y);clippedLine(x+26,y,x+26,y+26);
clippedLine(x+26,y+26,x,y+26);clippedLine(x,y+26,x,y);}}
int tx=targetX-ox,ty=targetY-oy;
clippedLine(tx-4,ty,tx+4,ty);clippedLine(tx,ty-4,tx,ty+4);
int ex=(int)copX-ox,ey=(int)copY-oy;
clippedLine(ex-3,ey-3,ex+3,ey+3);clippedLine(ex-3,ey+3,ex+3,ey-3);
float c=cosf(carAngle),n=sinf(carAngle);
int xx[4],yy[4];for(int i=0;i<4;i++){float f=(i<2?5:-5),r=(i==0||i==3?-3:3);xx[i]=64+f*c-r*n;yy[i]=38+f*n+r*c;}
for(int i=0;i<4;i++)clippedLine(xx[i],yy[i],xx[(i+1)%4],yy[(i+1)%4]);
// Direction marker stays visible even when the delivery is outside the view.
float bearing=atan2f(targetY-carY,targetX-carX);
screen.line(114,56,114+(int)(8*cosf(bearing)),56+(int)(6*sinf(bearing)));
}
void updateCity(int x,int y,uint32_t now){
if(now-tick<35)return;float dt=min((now-tick)*0.001f,0.08f);tick=now;
// Direct stick steering: center stops immediately, tilt drives that way.
float steerX=abs(x)>350?constrain(x/1700.0f,-1.0f,1.0f):0;
float steerY=abs(y)>350?constrain(y/1700.0f,-1.0f,1.0f):0;
float magnitude=hypotf(steerX,steerY);
carSpeed=42.0f*min(magnitude,1.0f);
if(magnitude>0){
carAngle=atan2f(steerY,steerX);
float nx=carX+steerX/magnitude*carSpeed*dt;
float ny=carY+steerY/magnitude*carSpeed*dt;
if(road(nx,ny)){carX=nx;carY=ny;}
else carSpeed=0;
}
if(hypotf(carX-targetX,carY-targetY)<10){deliveries++;score+=10;cityTarget();}
if(deliveries){float speed=12+min(deliveries,8);float dd=hypotf(carX-copX,carY-copY);
if(dd>0){float ax=copX+(carX-copX)/dd*speed*dt,ay=copY+(carY-copY)/dd*speed*dt;
if(road(ax,copY))copX=ax;if(road(copX,ay))copY=ay;}
if(hypotf(carX-copX,carY-copY)<7)mode=OVER;}
dirty=true;
}
bool dirty=true,ready=false,lastRaw=false,pressed=false,held=false,stickReady=true;
uint32_t debounce=0,tick=0,pressAt=0;
void text(int x,int y,const char *s){screen.setCursorXY(x,y);screen.print(s);}
bool occupied(int x,int y,int n){for(int i=0;i<n;i++)if(snake[i].x==x&&snake[i].y==y)return true;return false;}
void newFood(){int k=random(192-length);for(int y=0;y<12;y++)for(int x=0;x<16;x++)if(!occupied(x,y,length)){if(k--==0){food={(int8_t)x,(int8_t)y};return;}}}
void startGame(){carX=carY=24;carAngle=carSpeed=0;copX=copY=264;deliveries=0;cityTarget();px=py=1.5f;angle=0;flightX=flightY=roll=pitch=0;gateX=gateY=0;gateZ=100;
for(int i=0;i<6;i++)obstacles[i]={(float)random(-10,11),(float)random(-5,6),30.0f+i*18};jumpY=jumpV=0;birdY=34;birdV=0;pipeX=140;gapY=34;action=false;jumpStickLatched=false;flapRepeat=millis();
score=0;length=3;snake[0]={5,6};snake[1]={4,6};snake[2]={3,6};dx=nextX=1;dy=nextY=0;newFood();paddle=enemy=30;bx=64;by=32;vx=-2;vy=1;player=64;for(int i=0;i<6;i++)rocks[i]={(int)random(4,124),-i*12};if(game==5)for(int i=0;i<6;i++)rocks[i]={150+i*65,(int)random(9,18)};mode=PLAY;tick=millis();dirty=true;}
void draw(){screen.clear();screen.setScale(1);
if(mode==MENU){text(22,0,"POCKET ARCADE");menuTop=constrain(menuTop,max(0,game-3),game);for(int row=0;row<4;row++){int i=menuTop+row;if(i>=GAME_COUNT)break;text(6,13+row*11,i==game?">":" ");text(20,13+row*11,names[i]);}text(0,57,"UP/DOWN Click:play");}
else {text(0,0,names[game]);screen.setCursorXY(68,0);screen.print("Pts:");screen.print(score);
if(game==0){for(int i=0;i<length;i++)screen.rect(snake[i].x*8,snake[i].y*4+16,snake[i].x*8+6,snake[i].y*4+18,OLED_FILL);if(mode!=WIN)screen.rect(food.x*8,food.y*4+16,food.x*8+6,food.y*4+18,OLED_STROKE);}
if(game==1){screen.rect(2,paddle,4,paddle+12,OLED_FILL);screen.rect(123,enemy,125,enemy+12,OLED_FILL);screen.rect((int)bx,(int)by,(int)bx+2,(int)by+2,OLED_FILL);}
if(game==2){screen.rect(player-4,57,player+4,62,OLED_FILL);for(auto &r:rocks)if(r.y>=12)screen.rect(r.x-2,r.y,r.x+2,r.y+4,OLED_FILL);}
if(game==3)drawMaze();if(game==4)drawFlight();if(game==5)drawDino();if(game==6)drawFlappy();if(game==7)drawCity();
if(mode!=PLAY){screen.rect(0,20,127,63,OLED_CLEAR);text(30,22,mode==PAUSED?"PAUSED":mode==WIN?"YOU WIN!":"GAME OVER");text(0,36,mode==PAUSED?"Click: resume":"Click: retry");text(0,50,"Hold: game menu");}}
screen.update();dirty=false;}
void updateGame(int x,int y,uint32_t now){
if(game==7){updateCity(x,y,now);return;}
if(game>=5){if(now-tick<35)return;float dt=min((now-tick)*0.001f,0.10f);tick=now;
if(game==5){
// Airtime 810 ms (10 ms longer), with slightly more cactus clearance.
constexpr float dinoGravity=320.0f/0.810f;
if(action&&jumpY<=0)jumpV=160.0f;action=false;
jumpY+=jumpV*dt-0.5f*dinoGravity*dt*dt;jumpV-=dinoGravity*dt;
if(jumpY<0){jumpY=0;jumpV=0;}
for(auto &r:rocks){r.x-=3+min(score/10,3);if(r.x< -8){r.x=390+random(0,35);r.y=random(9,18);score++;}if(r.x<30&&r.x+4>16&&jumpY<r.y-2)mode=OVER;}}
else{
// Faster gravity, with a bounded fall speed; independent of OLED frame time.
if(action){birdV=-65.0f;action=false;}
birdY+=birdV*dt+0.5f*180.0f*dt*dt;birdV=min(birdV+180.0f*dt,80.0f);pipeX-=2;if(pipeX< -14){pipeX=140;gapY=random(27,49);score++;}if(birdY<16||birdY>60||(pipeX<34&&pipeX+12>23&&(birdY-3<gapY-11||birdY+3>gapY+11)))mode=OVER;}
dirty=true;return;}
if(game>=3){if(now-tick<50)return;tick=now;
if(game==3){angle+=x/2048.0f*0.10f;float step=-y/2048.0f*0.10f;float nx=px+cosf(angle)*step,ny=py+sinf(angle)*step;
if(!wall(nx+0.15f,py)&&!wall(nx-0.15f,py))px=nx;
if(!wall(px,ny+0.15f)&&!wall(px,ny-0.15f))py=ny;
if(px>6&&py>6){score=1;mode=WIN;}}
else {
float steerX=abs(x)>350?constrain(x/1700.0f,-1.0f,1.0f):0;
float steerY=abs(y)>350?constrain(y/1700.0f,-1.0f,1.0f):0;
roll=roll*0.8f+steerX*0.09f;pitch=pitch*0.8f+steerY*0.05f;
flightX=constrain(flightX+steerX*0.45f,-12.0f,12.0f);
flightY=constrain(flightY+steerY*0.45f,-12.0f,12.0f);
for(auto &o:obstacles){o.z-=1.1f+min(score*0.02f,1.0f);if(o.z<=2){if(fabsf(o.x-flightX)<2.4f&&fabsf(o.y-flightY)<2.3f)mode=OVER;else score++;o.z=108;o.x=random(-10,11);o.y=random(-5,6);}}
}
dirty=true;return;}
if(game==0){if(abs(x)>700||abs(y)>700){int tx=0,ty=0;if(abs(x)>abs(y))tx=x>0?1:-1;else ty=y>0?1:-1;if(tx!=-dx||ty!=-dy){nextX=tx;nextY=ty;}}
if(now-tick<(uint32_t)max(90,240-score*5))return;tick=now;dx=nextX;dy=nextY;int nx=snake[0].x+dx,ny=snake[0].y+dy;bool eat=nx==food.x&&ny==food.y;
if(nx<0||nx>=16||ny<0||ny>=12||occupied(nx,ny,length-(eat?0:1)))mode=OVER;
else{if(eat){length++;score++;}for(int i=length-1;i>0;i--)snake[i]=snake[i-1];snake[0]={(int8_t)nx,(int8_t)ny};if(length==192)mode=WIN;else if(eat)newFood();}}
else {if(now-tick<35)return;tick=now;
if(game==1){paddle=constrain(paddle+(y>700?3:y< -700?-3:0),12,51);if(by>enemy+8)enemy++;else if(by<enemy+4)enemy--;enemy=constrain(enemy,12,51);bx+=vx;by+=vy;if(by<=12){by=12;vy=abs(vy);}if(by>=61){by=61;vy=-abs(vy);}if(vx<0&&bx<=5&&bx>=1&&by+2>=paddle&&by<=paddle+12){bx=5;vx=abs(vx);score++;vy=(by-(paddle+6))*0.3f;}if(vx>0&&bx>=120&&by+2>=enemy&&by<=enemy+12){bx=120;vx=-abs(vx);}if(bx<0)mode=OVER;if(bx>127){score+=5;bx=64;by=32;vx=-2;vy=1;}}
else {player=constrain(player+(x>700?4:x< -700?-4:0),4,123);for(auto &r:rocks){r.y+=2+min(score/20,2);if(r.y+4>=57&&r.y<=62&&abs(r.x-player)<=6)mode=OVER;if(r.y>63){r.y=-8;r.x=random(4,124);score++;}}}}
dirty=true;
}
void setup(){Serial.begin(115200);pinMode(SW_PIN,INPUT_PULLUP);analogReadResolution(12);Wire.begin(21,22);
uint8_t address=0;for(uint8_t a: {uint8_t(0x3C),uint8_t(0x3D)}){Wire.beginTransmission(a);if(Wire.endTransmission()==0){address=a;break;}}
if(!address){Serial.println("OLED not found: check power, SDA and SCL.");return;}
screen.~GyverOLED();new (&screen) GyverOLED<SSD1306_128x64, OLED_BUFFER>(address);screen.init(21,22);screen.autoPrintln(false);
Serial.println("Keep joystick centered while starting.");long sx=0,sy=0;for(int i=0;i<32;i++){sx+=analogRead(X_PIN);sy+=analogRead(Y_PIN);delay(5);}cx=sx/32;cy=sy/32;
// Ignore an accidental full deflection during startup calibration.
if(cx<700||cx>3395)cx=2048;
if(cy<700||cy>3395)cy=2048;
randomSeed(micros());ready=true;}
void loop(){if(!ready){delay(100);return;}uint32_t now=millis();bool raw=digitalRead(SW_PIN)==LOW;
if(raw!=lastRaw){lastRaw=raw;debounce=now;}
if(now-debounce>=30&&raw!=pressed){pressed=raw;if(pressed){pressAt=now;held=false;if(mode==PLAY&&(game==5||game==6))action=true;}else if(!held){if(mode==MENU||mode==OVER||mode==WIN)startGame();else if(mode==PLAY&&(game<5||game==7)){if(game!=7||(abs(analogRead(X_PIN)-cx)<350&&abs(analogRead(Y_PIN)-cy)<350)){mode=PAUSED;dirty=true;}}else if(mode==PLAY){}else{mode=PLAY;tick=now;dirty=true;}}}
int x=analogRead(X_PIN)-cx,y=analogRead(Y_PIN)-cy;
// City driving: only a centered stick + held click may leave the game.
// Reset the hold timer while steering/throttling to prevent accidental exits.
bool cityMoving=game==7&&mode==PLAY&&(abs(x)>=350||abs(y)>=350);
if(pressed&&cityMoving)pressAt=now;
if(pressed&&!held&&!cityMoving&&now-pressAt>=800){held=true;mode=MENU;dirty=true;stickReady=false;}
if(mode==MENU){
// Either axis can scroll: useful when the module is mounted sideways.
int axis=abs(y)>=abs(x)?y:x;
int direction=abs(axis)>550?(axis>0?1:-1):0;
if(abs(x)<300&&abs(y)<300){menuDirection=0;stickReady=true;}
if(direction&&(stickReady||direction!=menuDirection||
(int32_t)(now-menuRepeat)>=0)){
bool first=stickReady||direction!=menuDirection;
game=(game+direction+GAME_COUNT)%GAME_COUNT;
menuDirection=direction;stickReady=false;
menuRepeat=now+(first?450:220);dirty=true;
}
}
// Dino jumps on a new deflection; Flappy repeats while the stick is held.
if(mode==PLAY&&(game==5||game==6)){
bool centered=abs(x)<350&&abs(y)<350;
bool deflected=abs(x)>450||abs(y)>450;
if(centered)jumpStickLatched=false;
if(deflected&&(!jumpStickLatched||
(game==6&&(int32_t)(now-flapRepeat)>=0))){
action=true;jumpStickLatched=true;flapRepeat=now+400;
}
}
if(mode==PLAY)updateGame(x,y,now);
if(dirty)draw();delay(2);
}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.




