How To Make Snake Game Using Arduino Uno & LCD - Arduino Projects ?? - 4 Steps - Instructables
How To Make Snake Game Using Arduino Uno & LCD - Arduino Projects ?? - 4 Steps - Instructables
https://www.instructables.com/How-to-Make-Snake-Game-Using-Arduino-Uno-Lcd-Ardui/ 1/13
7/1/25, 6:42 p.m. How to Make Snake Game Using Arduino Uno & Lcd ? - Arduino Projects 🐍🤔 : 4 Steps - Instructables
Supplies
1> ARDUINO UNO (ANY TYPE)
2>16x2 LCD
4>BREADBOARD
5>TWO PUSHBUTTONS
Step 2:
INSTALL "LiquidCrystal_I2C.h", if you haven't done yet!
https://www.instructables.com/How-to-Make-Snake-Game-Using-Arduino-Uno-Lcd-Ardui/ 2/13
7/1/25, 6:42 p.m. How to Make Snake Game Using Arduino Uno & Lcd ? - Arduino Projects 🐍🤔 : 4 Steps - Instructables
// VISIT YouTube.com/HoXDipannew
GRAPHIC_ITEM_NUM};
#define GRAPHIC_WIDTH 16
#define GRAPHIC_HEIGHT 4
LiquidCrystal_I2C lcd(0x27,16,2);
byte block[3] = {
B01110,
B01110,
B01110,
};
byte apple[3] = {
B00100,
B01010,
B00100,
};
#define DEBOUNCE_DURATION 20
if(*debounceStart == 0)
https://www.instructables.com/How-to-Make-Snake-Game-Using-Arduino-Uno-Lcd-Ardui/ 3/13
7/1/25, 6:42 p.m. How to Make Snake Game Using Arduino Uno & Lcd ? - Arduino Projects 🐍🤔 : 4 Steps - Instructables
*debounceStart = millis();
else if(millis()-*debounceStart>DEBOUNCE_DURATION)
return true;
return false;
if(*debounceStart == ULONG_MAX){
return false;
*debounceStart = millis();
}else if(millis()-*debounceStart>DEBOUNCE_DURATION){
*debounceStart = ULONG_MAX;
return true;
return false;
*debounceStart = 0;
struct Pos {
};
https://www.instructables.com/How-to-Make-Snake-Game-Using-Arduino-Uno-Lcd-Ardui/ 4/13
7/1/25, 6:42 p.m. How to Make Snake Game Using Arduino Uno & Lcd ? - Arduino Projects 🐍🤔 : 4 Steps - Instructables
struct Pos snakePosHistory[GRAPHIC_HEIGHT*GRAPHIC_WIDTH];
size_t snakeLength = 0;
uint8_t graphicRam[GRAPHIC_WIDTH*2/8][GRAPHIC_HEIGHT];
void graphic_generate_characters()
/*
space: 0 0
0: 0 A
1: 0 B
2: A 0
3: A A
4: A B
5: B 0
6: B A
7: B B
*/
byte glyph[8];
memset(glyph, 0, sizeof(glyph));
https://www.instructables.com/How-to-Make-Snake-Game-Using-Arduino-Uno-Lcd-Ardui/ 5/13
7/1/25, 6:42 p.m. How to Make Snake Game Using Arduino Uno & Lcd ? - Arduino Projects 🐍🤔 : 4 Steps - Instructables
if(upperIcon==1)
else if(upperIcon==2)
if(lowerIcon==1)
else if(lowerIcon==2)
lcd.createChar(i, glyph);
delay(1);
void graphic_clear(){
memset(graphicRam, 0, sizeof(graphicRam));
graphicRam[x/(8/2)][y] |= (uint8_t)item*(1<<((x%(8/2))*2));
void graphic_flush(){
lcd.clear();
(DisplayItem)((graphicRam[x/(8/2)][y*2]>>((x%(8/2))*2))&0x3);
(DisplayItem)((graphicRam[x/(8/2)][y*2+1]>>((x%(8/2))*2))&0x3);
if(upperItem>=GRAPHIC_ITEM_NUM)
upperItem = GRAPHIC_ITEM_B;
https://www.instructables.com/How-to-Make-Snake-Game-Using-Arduino-Uno-Lcd-Ardui/ 6/13
7/1/25, 6:42 p.m. How to Make Snake Game Using Arduino Uno & Lcd ? - Arduino Projects 🐍🤔 : 4 Steps - Instructables
if(lowerItem>=GRAPHIC_ITEM_NUM)
lowerItem = GRAPHIC_ITEM_B;
lcd.setCursor(x, y);
lcd.write(' ');
else
lcd.write(byte((uint8_t)upperItem*3+(uint8_t)lowerItem-1));
void game_new_apple_pos()
do{
applePos.x = rand()%GRAPHIC_WIDTH;
applePos.y = rand()%GRAPHIC_HEIGHT;
validApple = true;
validApple = false;
break;
} while(!validApple);
void game_init(){
https://www.instructables.com/How-to-Make-Snake-Game-Using-Arduino-Uno-Lcd-Ardui/ 7/13
7/1/25, 6:42 p.m. How to Make Snake Game Using Arduino Uno & Lcd ? - Arduino Projects 🐍🤔 : 4 Steps - Instructables
srand(micros());
gameUpdateInterval = 1000;
gameState = GAME_PLAY;
snakePosHistory[0].x=3; snakePosHistory[0].y=1;
snakePosHistory[1].x=2; snakePosHistory[1].y=1;
snakePosHistory[2].x=1; snakePosHistory[2].y=1;
snakePosHistory[3].x=0; snakePosHistory[3].y=1;
snakeLength = 4;
snakeDirection = SNAKE_RIGHT;
game_new_apple_pos();
thisFrameControlUpdated = false;
void game_calculate_logic() {
if(gameState!=GAME_PLAY)
return;
snakePosHistory[i] = snakePosHistory[i-1];
snakePosHistory[0]=snakePosHistory[1];
switch(snakeDirection){
if(snakePosHistory[0].x<0||snakePosHistory[0].x>=GRAPHIC_WIDTH||snakePosHistory[0].y
<0||snakePosHistory[0].y>=GRAPHIC_HEIGHT){
gameState = GAME_LOSE;
return;
if(snakePosHistory[0].x==snakePosHistory[i].x &&
snakePosHistory[0].y==snakePosHistory[i].y){
gameState = GAME_LOSE;
return;
snakeLength++;
gameUpdateInterval = gameUpdateInterval*9/10;
if(snakeLength>=sizeof(snakePosHistory)/sizeof(*snakePosHistory))
gameState = GAME_WIN;
else
game_new_apple_pos();
void game_calculate_display() {
graphic_clear();
switch(gameState){
https://www.instructables.com/How-to-Make-Snake-Game-Using-Arduino-Uno-Lcd-Ardui/ 9/13
7/1/25, 6:42 p.m. How to Make Snake Game Using Arduino Uno & Lcd ? - Arduino Projects 🐍🤔 : 4 Steps - Instructables
case GAME_LOSE:
lcd.clear();
lcd.setCursor(0, 0);
lcd.setCursor(0, 1);
lcd.print("Length: ");
lcd.setCursor(8, 1);
lcd.print(snakeLength);
break;
case GAME_WIN:
lcd.clear();
lcd.setCursor(0, 0);
lcd.setCursor(0, 1);
lcd.print("Length: ");
lcd.setCursor(8, 1);
lcd.print(snakeLength);
break;
case GAME_PLAY:
graphic_flush();
break;
case GAME_MENU:
//Do nothing
break;
}
https://www.instructables.com/How-to-Make-Snake-Game-Using-Arduino-Uno-Lcd-Ardui/ 10/13
7/1/25, 6:42 p.m. How to Make Snake Game Using Arduino Uno & Lcd ? - Arduino Projects 🐍🤔 : 4 Steps - Instructables
void setup() {
lcd.init();
lcd.backlight();
lcd.setCursor(0, 1);
// lcd.print("- Sadale.net");
pinMode(6, INPUT);
pinMode(7, INPUT);
graphic_generate_characters();
gameState = GAME_MENU;
void loop() {
lcd.setCursor(0, 0);
if(digitalRead(BUTTON_LEFT)==HIGH){
if(debounce_activate_edge(&debounceCounterButtonLeft)&&!thisFrameControlUpdated){
switch(gameState){
case GAME_PLAY:
switch(snakeDirection){
thisFrameControlUpdated = true;
break;
https://www.instructables.com/How-to-Make-Snake-Game-Using-Arduino-Uno-Lcd-Ardui/ 11/13
7/1/25, 6:42 p.m. How to Make Snake Game Using Arduino Uno & Lcd ? - Arduino Projects 🐍🤔 : 4 Steps - Instructables
case GAME_MENU:
game_init();
break;
}else{
debounce_deactivate(&debounceCounterButtonLeft);
lcd.setCursor(8, 0);
if(digitalRead(BUTTON_RIGHT)==HIGH){
if(debounce_activate_edge(&debounceCounterButtonRight)&&!thisFrameControlUpdated){
switch(gameState){
case GAME_PLAY:
switch(snakeDirection){
thisFrameControlUpdated = true;
break;
case GAME_MENU:
game_init();
break;
}else{
debounce_deactivate(&debounceCounterButtonRight);
}
https://www.instructables.com/How-to-Make-Snake-Game-Using-Arduino-Uno-Lcd-Ardui/ 12/13
7/1/25, 6:42 p.m. How to Make Snake Game Using Arduino Uno & Lcd ? - Arduino Projects 🐍🤔 : 4 Steps - Instructables
if(millis()-lastGameUpdateTick>gameUpdateInterval){
game_calculate_logic();
game_calculate_display();
lastGameUpdateTick = millis();
thisFrameControlUpdated = false;
Step 4: DONE!
JUST POWER IT !
https://www.instructables.com/How-to-Make-Snake-Game-Using-Arduino-Uno-Lcd-Ardui/ 13/13