Skip to main content

Creating an automated scoreboard for a basketball hoop using a Raspberry Pi is a fantastic project that combines hardware interfacing, software programming, and a touch of physical computing. This project aims to automatically detect when a ball passes through a hoop and update the score on a digital scoreboard. Here’s how you can build one yourself.

Project Overview

This project involves setting up a sensor to detect when a ball goes through a hoop, using a Raspberry Pi to process the sensor data, and then updating the score on a digital display. We will use an infrared (IR) break-beam sensor for detection, a Raspberry Pi as the central processing unit, and an LED display or a web-based scoreboard for displaying the score.

Materials Needed

  • Raspberry Pi (any model with GPIO pins will work, but a Raspberry Pi 3 or 4 is recommended for better performance)
  • Infrared (IR) break-beam sensor or Ultrasonic sensor
  • Jumper wires
  • Breadboard
  • LED display (optional, for a physical scoreboard)
  • Power supply for the Raspberry Pi
  • USB keyboard, mouse, and monitor for initial setup (if not using SSH)
  • Internet connection for downloading libraries and tools

Step 1: Setting Up Your Raspberry Pi

  1. Install the Operating System: Download the latest version of Raspberry Pi OS and flash it onto your SD card using software like Raspberry Pi Imager.
  2. Initial Setup: Insert the SD card into your Raspberry Pi, connect the monitor, keyboard, and mouse, and finally, power it up. Go through the initial setup process, including connecting to Wi-Fi.
  3. Enable SSH (Optional): If you prefer to work remotely on your Pi, enable SSH through the Raspberry Pi configuration menu.

Step 2: Connecting the IR Break-Beam Sensor

  1. Understand the Sensor: An IR break-beam sensor consists of two parts: an emitter that sends out an infrared light and a receiver that detects the light. When the beam is broken, the sensor triggers an event.
  2. Wiring the Sensor: Connect the emitter and receiver to the Raspberry Pi GPIO pins. Typically, you’ll connect the power pins to the 5V and ground on the Pi and the signal pin of the receiver to a GPIO pin. Refer to the sensor’s datasheet for exact wiring instructions.
  3. Test the Connection: Write a simple script to test if the setup works. Use a library like RPi.GPIO in Python to listen for changes in the GPIO pin state, indicating a break in the beam.

Step 3: Programming the Raspberry Pi

  • Setting Up the Environment: Open a terminal on the Raspberry Pi and install Python if it’s not already installed. Also, install the RPi.GPIO library using pip.
sudo apt update
sudo apt install python3 python3-pip
pip3 install RPi.GPIO
  • Writing the Script: Create a Python script that continuously monitors the GPIO pin connected to the IR sensor. When a break is detected, increment a score variable and print the score to the console or update a display.
import RPi.GPIO as GPIO
import time

SENSOR_PIN = 17 # Replace with your GPIO pin number
GPIO.setmode(GPIO.BCM)
GPIO.setup(SENSOR_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)

score = 0

def beam_break(channel):
    global score
    score += 1
    print("Score:", score)

GPIO.add_event_detect(SENSOR_PIN, GPIO.FALLING, callback=beam_break, bouncetime=300)

try:
    while True:
        time.sleep(0.1)
except KeyboardInterrupt:
    GPIO.cleanup()

Step 4: Displaying the Score

  • Using an LED Display: If you have an LED display, you can connect it to your Raspberry Pi via GPIO or I2C, depending on the display type. Update the beam_break function to display the score on the LED display.
  • Creating a Web-based Scoreboard: Alternatively, you can create a simple web server on the Raspberry Pi to display the score. Use Flask or Django to set up the server and update the webpage every time the score changes.

Step 5: Finalizing the Setup

  • Mount the IR Sensor: Securely mount the IR sensor on the basketball hoop in such a way that the ball will break the beam whenever it passes through the hoop.
  • Test the Entire System: With everything set up, test the system by throwing a ball through the hoop and checking if the score updates correctly.

Conclusion

This project not only provides a fun addition to your basketball games but also offers a hands-on experience with Raspberry Pi, sensors, and programming. The possibilities are endless; you can enhance the project by adding features like sound effects, different game modes, or even integrating it with a mobile app for score tracking. Enjoy building and playing!

Using an ultrasonic sensor. That displays the score on a webpage using WebSockets and nodeJS

Using an ultrasonic sensor and displaying the score on a webpage via WebSockets and Node.js adds an interesting twist to the automated scoreboard project. This approach offers real-time score updates on a webpage, which can be viewed on any device connected to the same network as the Raspberry Pi. Let’s dive into the details of implementing these features.

Materials Needed (Additional)

  • Ultrasonic sensor (HC-SR04 is a popular choice)
  • Node.js installed on the Raspberry Pi
  • npm (Node Package Manager)
  • Basic knowledge of JavaScript and Node.js

Step 1: Connecting the Ultrasonic Sensor to the Raspberry Pi

  1. Understand the Ultrasonic Sensor: The HC-SR04 ultrasonic sensor has four pins: VCC (power), GND (ground), TRIG (trigger), and ECHO (receive). It works by emitting an ultrasonic sound wave from the TRIG pin and receiving it back through the ECHO pin after it bounces off an object.
  2. Wiring the Sensor: Connect the VCC pin to the 5V pin on the Raspberry Pi, the GND pin to one of the ground pins, the TRIG pin to a GPIO pin, and the ECHO pin to another GPIO pin. A resistor may be needed between the ECHO pin and the Raspberry Pi to protect the GPIO pin from high voltage.
  3. Testing the Sensor: Before moving on to the programming part, ensure your sensor is correctly connected and functioning. You can use Python scripts available online to test the basic functionality of the ultrasonic sensor.

Step 2: Programming the Raspberry Pi for Ultrasonic Sensor Reading

  1. Python Script for Sensor Reading: Write a Python script that uses the GPIO pins to control the ultrasonic sensor, measure the time between sending and receiving the sound wave, and calculate the distance to the nearest object.
  2. Adjust for Score Detection: Modify the script to detect when an object (a basketball) passes through a specific distance range that would correspond to passing through the hoop and then increment the score.
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)

TRIG = 23  # Use your GPIO pin numbers
ECHO = 24

GPIO.setup(TRIG, GPIO.OUT)
GPIO.setup(ECHO, GPIO.IN)

GPIO.output(TRIG, False)
time.sleep(2)  # Settling time for sensor

def get_distance():
    GPIO.output(TRIG, True)
    time.sleep(0.00001)  # Send pulse for 10µs
    GPIO.output(TRIG, False)

    while GPIO.input(ECHO) == 0:
        pulse_start = time.time()

    while GPIO.input(ECHO) == 1:
        pulse_end = time.time()

    pulse_duration = pulse_end - pulse_start
    distance = pulse_duration * 17150  # Distance in cm
    distance = round(distance, 2)
    return distance

try:
    while True:
        dist = get_distance()
        print("Distance:", dist, "cm")
        time.sleep(1)  # Check every 1 second
except KeyboardInterrupt:
    GPIO.cleanup()

Step 3: Setting Up Node.js and WebSockets for the Scoreboard

  1. Install Node.js on Raspberry Pi: If not already installed, install Node.js and npm. curl -sL <https://deb.nodesource.com/setup_14.x> | sudo bash - sudo apt-get install -y nodejs
  2. Create a Node.js Application: Create a new directory for your project, initialize a Node.js application, and install necessary packages. mkdir basketball-scoreboard cd basketball-scoreboard npm init -y npm install express socket.io
  3. Build the Web Application: Create an index.html file for the scoreboard frontend and a server.js file for the backend server. Use Socket.IO for real-time communication between the server and clients.
    • server.js: This file will start a web server that serves your scoreboard page and establishes a WebSocket connection to update the score in real time.
    const express = require('express'); const http = require('http'); const socketIo = require('socket.io'); const app = express(); const server = http.createServer(app); const io = socketIo(server); app.use(express.static('public')); // Serve static files from 'public' directory let score = 0; io.on('connection', (socket) => { console.log('A user connected'); socket.on('incrementScore', () => { score++; io.emit('scoreUpdate', score); }); socket.emit('scoreUpdate', score); // Send current score to newly connected client }); server.listen(3000, () => { console.log('Server listening on port 3000'); });
    • index.html: A simple HTML file with JavaScript to connect to the WebSocket server and display the score.
    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Scoreboard</title> </head> <body> <h1>Score: <span id="score">0</span></h1> <script src="/socket.io/socket.io.js"></script> <script> const socket = io(); socket.on('scoreUpdate', function(score) { document.getElementById('score').innerText = score; }); </script> </body> </html>
  4. Integrate Python and Node.js: Use a Python library like python-socketio to connect the Python script that reads the ultrasonic sensor to the Node.js server. This allows the Python script to send score updates to the Node.js server, which then updates all connected clients in real time.
    • Install python-socketio in your Python environment:
    pip3 install python-socketio
    • Modify the Python script to send an ‘incrementScore’ event to the Node.js server via WebSocket whenever the basketball hoop score condition is met.

Step 4: Running the System

  1. Start the Node.js Server: Run node server.js from your project directory.
  2. Run the Python Script: Execute your Python script to start detecting the ball passes and sending score updates to the Node.js server.
  3. View the Scoreboard: Open a web browser and navigate to http://<raspberry_pi_ip_address>:3000 to view the real-time scoreboard.

Conclusion

You now have a fully functional automated scoreboard for your basketball hoop that displays the score in real-time on a web page. This project not only showcases the versatility of Raspberry Pi but also demonstrates how to integrate different technologies (Python, Node.js, WebSockets) to create a real-world application. Feel free to customize the web interface and add more features, such as team names, game timers, or even player stats, to enhance your basketball experience.