🏀 Arduino Mini Basketball Game with Ultrasonic Sensor, 7-Segment & LCD

Introduction



Have you ever wanted to make your own electronic mini basketball game? Using an Arduino UNO, an ultrasonic sensor, a 16x2 LCD, and a 2-digit 7-segment display, you can build a fun project where every time you throw a ball into the hoop, the system automatically counts the goal.

The 7-segment display shows the score, while the LCD displays messages such as the game title, "Game Start", and "Game Over". A reset button allows restarting the game anytime.

This project is perfect for Arduino beginners and can be used as a mini project for school, college, or DIY fun.

Components Required

  • Arduino UNO (or compatible)

  • Ultrasonic Sensor (HC-SR04)

  • 16x2 LCD (without I2C module, parallel mode)

  • 2-digit 7-segment display (common cathode)

  • Push button (for reset)

  • 10k Potentiometer (for LCD contrast)

  • Resistors (220Ω for LCD backlight & button pull-down if needed)

  • Breadboard and jumper wires

  • Buzzer

Circuit Diagram (Connections)

I2C LCD (16x2)

  • VCC → 5V

  • GND → GND

  • SDA → A4

  • SCL → A5

Ultrasonic Sensor

  • VCC → 5V

  • GND → GND

  • TRIG → D2

  • ECHO → D3

Buzzer

  • vcc→ D8
  • GND → GND

Reset Button

  • One side → D13

  • Other side → GND

2-digit 7-Segment Display (Common Cathode)

  • Digit1 → D6

  • Digit2 → D7

  • Segments a–g → A0, A1, A2, A3, A4, A5, D12

Arduino Code

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SevSeg.h>

// LCD I2C
LiquidCrystal_I2C lcd(0x27, 16, 2);

// 7-segment
SevSeg sevseg;

// Ultrasonic Sensor
const int trigPin = 2;
const int echoPin = 3;

// Buzzer
const int buzzerPin = 8;

// Reset Button
const int resetPin = 13

long duration;
int distance;
int score = 0;
bool gameOver = false;

void setup() {
  // LCD Init
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0,0);
  lcd.print("AMHAR HIZBULLAH.CC");
  lcd.setCursor(0,1);
  lcd.print("BASKET BALL GAME");
  delay(3000);
  lcd.clear();

  // 7-segment Setup
  byte numDigits = 2;
  byte digitPins[] = {6, 7};
  byte segmentPins[] = {A0, A1, A2, A3, A4, A5, 12};
  bool resistorsOnSegments = true;
  byte hardwareConfig = COMMON_CATHODE;
  bool updateWithDelays = false;
  bool leadingZeros = true;
  bool disableDecPoint = true;

  sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins,
               resistorsOnSegments, updateWithDelays,
               leadingZeros, disableDecPoint);
  sevseg.setBrightness(90);

  // Pins
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(buzzerPin, OUTPUT);
  pinMode(resetPin, INPUT_PULLUP);

  lcd.setCursor(0,0);
  lcd.print("GAME START!");
  delay(2000);
  lcd.clear();
}

void loop() {
  // Reset
  if(digitalRead(resetPin) == LOW){
    score = 0;
    gameOver = false;
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("NEW GAME!");
    delay(1500);
    lcd.clear();
  }

  // Ultrasonic distance
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH, 20000);
  distance = duration * 0.034 / 2;

  // Goal detection
  if(distance > 2 && distance < 10 && !gameOver){
    score++;

    // Buzzer
    tone(buzzerPin, 1000, 200);

    delay(1000);
  }

  // 7-segment
  sevseg.setNumber(score);
  sevseg.refreshDisplay();

  // Game over
  if(score >= 20 && !gameOver){
    gameOver = true;
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("GAME OVER!");
    lcd.setCursor(0,1);
    lcd.print("Final Score: ");
    lcd.print(score);
  }
}

 How the Game Works

  1. LCD shows AMHAR HIZBULLAH.CC BASKET BALL GAME at startup.

  2. Game starts → LCD shows GAME START!

  3. Ball passes hoop → Ultrasonic detects → score increases

  4. Buzzer sounds for 200ms on each goal

  5. Score displayed only on 7-segment

  6. Score reaches 20 → LCD shows GAME OVER + final score

  7. Press Reset button → Score resets and new game begins

  8. .

 Final Output

  • LCD: Game title, Start, Game Over messages

  • 7-Segment: Real-time score

  • Ultrasonic: Detects each basket

  • Button: Restart game anytime

Comments