Skip to content

Raspberry Pi · Tutorial

Raspberry Pi LED Ping Pong Project

Build a two-player ping pong game using Scratch on Raspberry Pi with GPIO-controlled LEDs for scoring.

ScratchGPIOLED Project

Project Overview

In this activity, we are going to build a two-player ping pong-like game using Scratch on the Raspberry Pi. There will be two paddles and a ball. The paddles will be user-controlled to try to hit the ball back in the other direction to the other player. An LED will light up when either player scores.

To begin, open Scratch and delete the Cat sprite on the right-hand side.

Note: To interact with the GPIO pins from the Raspberry Pi, you must install "nuscratch" if not already. To do this, open a terminal and type sudo apt-get install nuscratch (no quotes) and answer "y" to any questions.

Materials Needed

  • (1) Breadboard (small one is fine)
  • (2) LED - Any color
  • Jumper wires
  • Raspberry Pi with Scratch installed

Game Elements

Backdrop

To create the backdrop, click the "Stage" section in the bottom right-hand portion of the window. Then, in the main window, click the Backgrounds tab. Click the edit button and choose your background of choice (like the brick wall background).

Paddles

Click the "Paint New Sprite" button beside the text "New Sprite" and draw a vertical line in the middle of the grid. Rename it to Paddle1 at the top of the main window.

Under the scripts tab, add instructions for paddle movement. Fill in the values as seen and select the buttons to move the paddle up and down. For player 1, use "W" and "S" keys.

Right-click on Paddle1 in the lower right window and select duplicate. Name the new sprite Paddle2. Change the values in the script. For player 2, use the up arrow and down arrow keys.

Variables

Click the Variables button in the upper left window. Create variables named Score1 and Score2 (leave "For all Sprites" selected).

Ball

Click the "Choose a new sprite file" button to import a ball sprite. Choose any ball you'd like (beach ball works well).

Insert the script parts as shown in the tutorial. Take your time and make sure you get everything. Also, go to "EDIT > Start GPIO Server" to allow interaction with the GPIO pins.

Wiring Setup

First, place a jumper from GPIO 14 to a spot on the breadboard, then do the same from GPIO 15.

Next, place a jumper from the ground pin on the Raspberry Pi to the "-" rail on the breadboard.

Add your two LEDs with the "hot" leg connected to the GPIO jumpers and the negative leg connected to the ground rail. The "hot" leg is the longer leg on the LED.

Now you're ready to play! The LEDs will light up when each player scores.