AI Desktop Buddy is an interactive handheld gaming device built around an ESP32 microcontroller. It features a tiny 0.66-inch OLED display for crisp graphics and a PS2 joystick module for responsive control, creating a compact entertainment system that fits on any desk.
This guide provides everything needed to assemble and program the device, including a wiring diagram showing how to connect the display and joystick to the ESP32, a complete parts list, and firmware with multiple games like Snake, Blocks, Dodge, and Catch. Follow the step-by-step assembly instructions to get the buddy up and running.
Wiring diagram

Gather all the parts
Assemble it in 2 steps
1. Connect the face screen
Unplug USB. Use a Grove-to-female jumper cable to connect SCL to GPIO22 (clock), SDA to GPIO21 (data), VCC to 3V3 (power), and GND to GND (ground). Match the labels before plugging USB back in.
- Do not connect VCC to VIN or 5V: the screen's data lines are pulled up to its supply voltage.
2. Connect the thumb joystick
With USB unplugged, connect the KY-023 joystick pin labeled +5V to 3V3, GND to GND, VRX to GPIO32 (horizontal), VRY to GPIO33 (vertical), and SW to GPIO27 (press).
- Use 3V3 even though the joystick pin says +5V. Powering it from 5V could damage the ESP32 inputs.
Review all connections
1. Connections between "face" and "ESP32"
2. Connections between "stick" and "ESP32"
Deploy the firmware
#include <Arduino.h>
#include <Wire.h>
#include <U8g2lib.h>
U8G2_SSD1306_64X48_ER_F_HW_I2C oled(U8G2_R0, U8X8_PIN_NONE, 22, 21);
enum Screen { MENU, FACE, DODGE, CATCH, TEST, BLOCKS, SNAKE, EMO, OVER } screen = MENU;
const char* names[] = {"Yuz", "Kacis", "Yakala", "Kol testi", "Bloklar", "Yilan", "Emo"};
int selected, score, rawX, rawY, cx, cy, stable, candidate;
int itemX, itemY;
float x, y, player;
uint32_t changed, pressed, nextFall;
uint8_t previous[384];
bool first = true, held = false;
uint8_t neutralFrames = 0, tiltFrames = 0;
bool menuArmed = false;
int lastDirection = 0;
int sample(int pin) {
int sum = 0;
for (int i=0;i<8;i++) sum += analogRead(pin);
return sum/8;
}
float axis(int raw, int center) {
int d = raw-center;
if (abs(d)<=95) return 0;
return copysignf(constrain((abs(d)-95)/800.0f,0.0f,1.0f),d);
}
void spawn() { itemX=random(4,60); itemY=12; }
// Four-cell pieces on an eight-column, twelve-row board.
const uint16_t pieces[]={0x00f0,0x0660,0x0270,0x0360,0x0470};
uint16_t pile[12], piece;
int bx, by, gameDir, sx, sy, food, length;
uint8_t snake[192];
uint32_t sceneStart, nextMove;
bool turnReady;
bool fits(uint16_t mask,int px,int py){
for(int i=0;i<16;++i) if(mask&(1<<i)){
int a=px+i%4,b=py+i/4;
if(a<0||a>=8||b<0||b>=12||(pile[b]&(1<<a))) return false;
}
return true;
}
void newPiece(){piece=pieces[random(5)];bx=2;by=0;if(!fits(piece,bx,by))screen=OVER;}
void newFood(){
int start=random(192);
for(int i=0;i<192;++i){
int cell=(start+i)%192;
if(!memchr(snake,cell,length)){food=cell;return;}
}
screen=OVER;
}
void startExtras(uint32_t now){
sceneStart=now;nextMove=nextFall=now+450;turnReady=false;
if(screen==BLOCKS){memset(pile,0,sizeof pile);newPiece();}
if(screen==SNAKE){length=3;snake[0]=101;snake[1]=100;snake[2]=99;sx=1;sy=0;newFood();}
}
void extraGames(uint32_t now,bool tap){
if(screen==BLOCKS){
if(tap){
uint16_t rotated=0;
for(int i=0;i<16;++i) if(piece&(1<<i)) rotated|=1<<((i%4)*4+3-i/4);
for(int kick : {0,-1,1,-2,2}) if(fits(rotated,bx+kick,by)){piece=rotated;bx+=kick;break;}
}
gameDir=x>0.6f?1:x< -0.6f?-1:0;
if(!gameDir) nextMove=now;
else if(int32_t(now-nextMove)>=0){if(fits(piece,bx+gameDir,by))bx+=gameDir;nextMove=now+135;}
if(int32_t(now-nextFall)>=0 || (fabsf(y)>0.75f && int32_t(nextFall-now)>380)){
nextFall=now+(fabsf(y)>0.75f?75:max(120,460-score*20));
if(fits(piece,bx,by+1)) ++by;
else {
for(int i=0;i<16;++i) if(piece&(1<<i)) pile[by+i/4]|=1<<(bx+i%4);
for(int r=11;r>=0;--r) if(pile[r]==255){for(int j=r;j>0;--j)pile[j]=pile[j-1];pile[0]=0;++score;++r;}
newPiece();
}
}
}
if(screen==SNAKE){
if(fabsf(x)<0.25f && fabsf(y)<0.25f) turnReady=true;
if(int32_t(now-nextMove)<0)return;
nextMove=now+max(90,245-score*8);
if(turnReady){
if(!sx && fabsf(x)>0.55f){sx=x>0?1:-1;sy=0;}
else if(!sy && fabsf(y)>0.55f){sy=y>0?1:-1;sx=0;}
}
int a=snake[0]%16+sx,b=snake[0]/16+sy,cell=b*16+a;
bool eat=cell==food;
if(a<0||a>=16||b<0||b>=12||memchr(snake,cell,length-!eat)){screen=OVER;return;}
if(eat){++length;++score;}
memmove(snake+1,snake,length-1);snake[0]=cell;
if(eat)newFood();
}
}
void littleBot(int px,int py,bool blink){
oled.drawRFrame(px-10,py-8,21,17,4);
oled.drawVLine(px,py-11,3);oled.drawDisc(px,py-12,1);
for(int d : {-4,4}) oled.drawRBox(px+d-1,py-3,3,blink?1:5,1);
oled.drawHLine(px-2,py+5,5);
}
void setup() {
analogReadResolution(12);
pinMode(27, INPUT_PULLUP);
oled.setI2CAddress(0x3C << 1);
oled.begin();
// Clear all 128 columns, including those outside the 64-pixel viewport.
for(int page=0;page<8;++page){
for(int col=0;col<128;col+=16){
Wire.beginTransmission(0x3C);
Wire.write(0x00); Wire.write(0xB0|page);
Wire.write(0x10|(col>>4)); Wire.write(col&15);
Wire.endTransmission();
Wire.beginTransmission(0x3C);
Wire.write(0x40);
for(int i=0;i<16;++i) Wire.write(uint8_t(0));
Wire.endTransmission();
}
}
oled.setFont(u8g2_font_5x7_tf);
// Average the resting position after the module has settled.
delay(200);
for(int i=0;i<32;++i){cx+=sample(32);cy+=sample(33);delay(4);}
cx/=32; cy/=32;
}
void loop() {
static uint32_t frame;
uint32_t now=millis();
if(now-frame<25) return;
frame=now;
rawX=sample(32); rawY=sample(33);
x+=(axis(rawX,cx)-x)*0.45f;
y+=(axis(rawY,cy)-y)*0.45f;
int down=digitalRead(27)==LOW;
if(down!=candidate){candidate=down;changed=now;}
bool click=false, faceTap=false, rotateTap=false;
if(stable!=candidate && now-changed>=25){
stable=candidate;
if(stable){
pressed=now;
held=screen==MENU || screen==OVER;
click=held; // Select immediately on a debounced press.
} else if(!held && now-pressed<650){faceTap=screen==FACE;rotateTap=screen==BLOCKS;}
}
if(stable && !held && now-pressed>=650){screen=MENU;held=true;menuArmed=false;neutralFrames=tiltFrames=0;}
if(screen==MENU){
// One step per tilt; require a steady return to center to re-arm.
int delta=rawX-cx; // Only left/right moves the carousel.
if(abs(delta)<300){
if(neutralFrames<8) ++neutralFrames;
if(neutralFrames==8) menuArmed=true;
} else neutralFrames=0;
int direction=abs(delta)>1050 ? (delta>0?1:-1) : 0;
if(direction!=lastDirection){tiltFrames=0;lastDirection=direction;}
if(!direction) tiltFrames=0;
if(!candidate && !click && menuArmed && direction && ++tiltFrames>=4){
selected=(selected+direction+7)%7;
menuArmed=false;
tiltFrames=0;
}
if(click){screen=Screen(selected+1);player=32;score=0;spawn();nextFall=now+75;startExtras(now);}
} else if(click && screen==OVER){screen=MENU;menuArmed=false;neutralFrames=tiltFrames=0;}
if(screen==DODGE || screen==CATCH){
player=constrain(player+x*2.2f,5.0f,58.0f);
if(int32_t(now-nextFall)>=0){
nextFall=now+max(25,75-score*2);
itemY+=2;
if(itemY>=40){
bool hit=abs(int(player)-itemX)<=7;
if((screen==DODGE && hit)||(screen==CATCH && !hit)) screen=OVER;
else {++score;spawn();}
}
}
}
extraGames(now,rotateTap);
static uint32_t lastTouch, emotionStart, nextEmotion;
static int emotion;
static float sleepy, shy, curious, delighted;
static Screen lastScreen=MENU;
if(screen==FACE){
if(lastScreen!=FACE){
lastTouch=now; nextEmotion=now+2200; emotion=0;
sleepy=shy=curious=delighted=0;
}
if(stable || faceTap || abs(rawX-cx)>240 || abs(rawY-cy)>240) lastTouch=now;
if(faceTap){emotion=1;emotionStart=now;nextEmotion=now+4300;}
else if(int32_t(now-nextEmotion)>=0 && now-lastTouch<15000){
emotion=random(2,4); emotionStart=now; nextEmotion=now+random(3000,5400);
}
bool sleep=now-lastTouch>=15000;
if(now-emotionStart>=1550 || sleep) emotion=0;
sleepy+=(float(sleep)-sleepy)*0.13f;
shy+=(float(emotion==1)-shy)*0.19f;
curious+=(float(emotion==2)-curious)*0.16f;
delighted+=(float(emotion==3)-delighted)*0.16f;
}
lastScreen=screen;
oled.clearBuffer();
switch(screen){
case MENU:
oled.drawStr(10,7,"BAS: SEC");
oled.drawRFrame(0,10,64,29,5);
oled.drawTriangle(4,24,9,20,9,28);
oled.drawTriangle(59,24,54,20,54,28);
if(selected==0){
oled.drawRBox(24,14,5,8,2); oled.drawRBox(35,14,5,8,2);
oled.drawCircle(32,21,3,U8G2_DRAW_LOWER_LEFT|U8G2_DRAW_LOWER_RIGHT);
} else if(selected==1){
oled.drawBox(29,13,6,5); oled.drawTriangle(25,24,32,18,39,24);
} else if(selected==2){
oled.drawDisc(32,16,3); oled.drawFrame(25,21,14,4);
} else if(selected==4){
oled.drawBox(26,15,11,4);oled.drawBox(30,19,4,5);
} else if(selected==5){
oled.drawLine(24,23,33,23);oled.drawVLine(33,15,9);oled.drawDisc(39,15,2);
} else if(selected==6){
oled.drawRFrame(23,14,18,11,3);oled.drawBox(27,17,3,4);oled.drawBox(34,17,3,4);
} else {
oled.drawDisc(32,15,3); oled.drawVLine(32,17,6); oled.drawRBox(25,22,14,3,1);
}
oled.drawRBox(7,27,50,10,3);
oled.setDrawColor(0);
oled.drawStr((64-oled.getStrWidth(names[selected]))/2,35,names[selected]);
oled.setDrawColor(1);
for(int i=0;i<7;++i){
if(i==selected) oled.drawDisc(8+i*8,44,2);
else oled.drawCircle(8+i*8,44,2);
}
break;
case FACE: {
int phase=now%3600;
float blink=phase<200?max(0.1f,abs(phase-100)/100.0f):1.0f;
float breath=sinf(now%2600*TWO_PI/2600);
int fx=lroundf(x*3*(1-sleepy)), fy=lroundf(breath*(1+sleepy)+y*2*(1-sleepy)+shy*3+sleepy*5);
for(int side : {-1,1}){
int h=max(2,(int)lroundf((20-shy*9-delighted*6+side*curious*5)*blink*(1-sleepy)+sleepy*2));
oled.drawRBox(32+side*16+fx-6,21+fy-h/2,12,h,min(5,(h-1)/2));
if(shy>0.35f) for(int i=0;i<3;++i){
int px=32+side*20+fx+i*2;
oled.drawLine(px,30+fy,px-1,32+fy);
}
}
if(sleepy>0.8f){
oled.drawEllipse(32,34+fy,2,1);
int z=5-int((now%1600)/320);
oled.drawHLine(53,z,5); oled.drawLine(57,z,53,z+4); oled.drawHLine(53,z+4,5);
} else {
int w=lroundf(5-shy*2+delighted*2-curious*3);
for(int d=-w;d<=w;++d)
oled.drawDisc(32+fx+d,33+fy+lroundf((1.0f-d*d/float(w*w))*(3+delighted*2)*(1-sleepy)),1);
}
break;
}
case DODGE: case CATCH:
oled.setCursor(0,8);oled.print(screen==DODGE?"Kacis ":"Yakala ");oled.print(score);
oled.drawHLine(0,10,64);
oled.drawBox(int(player)-5,42,11,4);
if(screen==DODGE) oled.drawBox(itemX-2,itemY-2,5,5);
else oled.drawDisc(itemX,itemY,2);
break;
case BLOCKS:
oled.drawVLine(33,0,48);
oled.drawStr(37,9,"Sira");oled.setCursor(37,20);oled.print(score);
oled.drawStr(37,34,"BAS");oled.drawStr(37,45,"Don");
for(int row=0;row<12;++row)for(int col=0;col<8;++col)
if(pile[row]&(1<<col))oled.drawBox(col*4,row*4,3,3);
for(int i=0;i<16;++i)if(piece&(1<<i))oled.drawBox((bx+i%4)*4,(by+i/4)*4,3,3);
break;
case SNAKE:
for(int i=0;i<length;++i)oled.drawBox(snake[i]%16*4,snake[i]/16*4,i?3:4,i?3:4);
oled.drawDisc(food%16*4+1,food/16*4+1,1);
break;
case EMO: {
uint32_t t=(now-sceneStart)%11000;
int bob=lroundf(2*sinf(t*TWO_PI/850));
int emoX=t<2000?16+lroundf(9*sinf(t*TWO_PI/2000)):16;
littleBot(emoX,30+bob,t%3000<130);
if(t>=2000 && t<9500){
float f=t<4000?(t-2000)/2000.f:t>7500?(9500-t)/2000.f:1.f;
f=f*f*(3-2*f);
int friendX=76-lroundf(27*f);
if(friendX<=53)littleBot(friendX,30-bob,t%3100<130);
else if(friendX<74){ // Clip the partially visible friend at the right edge.
oled.setClipWindow(0,0,64,48);littleBot(friendX,30-bob,false);oled.setMaxClipWindow();
}
if(t>=4000 && t<7500){
bool reply=t>=5700;
int a=reply?49:16,d=reply?-1:1;
oled.drawLine(a+d*10,31,a+d*14,24+bob);
oled.drawDisc(a+d*14,24+bob,1);
oled.drawRFrame(a-13,2,27,12,3);oled.drawStr(a-10,11,"Selam");
}
}
break;
}
case OVER:
oled.drawStr(2,12,"Oyun bitti");
oled.setCursor(2,26);oled.print("Puan: ");oled.print(score);
oled.drawStr(2,42,"Bas: Menu");
break;
case TEST:
oled.setCursor(2,9);oled.print("X: ");oled.print(rawX);
oled.setCursor(2,20);oled.print("Y: ");oled.print(rawY);
oled.drawStr(2,31,stable?"Tus: BASILI":"Tus: BOS");
oled.drawStr(2,45,"Tut: Menu");
break;
}
auto* buf=oled.getBufferPtr();
if(first || memcmp(previous,buf,sizeof previous)!=0){
memcpy(previous,buf,sizeof previous);oled.sendBuffer();first=false;
}
}Download project files
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.




