Introduction: Infrared Laser Tag With Raspberry Pi Zero
This Instructable will walk through the process for creating an Infrared Laser Tag game using a base server computer and a Raspberry Pi zero for each player. The project relies heavily on a Wifi connection to communicate with the server which makes the Pi a great candidate.
The server used in this project was an old desktop computer with Linux. The computer does not need to be anything special, and could probably even be run from a Raspberry Pi 3. The server and each of the pi zero's must be connected to the same network during game play.
Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.
Step 1: Materials
The description and some links for the needed materials are shown below. The material list below is for 3 guns.
- Server Computer (1)
- Raspberry Pi Zero W (3) Adafruit
- At least 4GB SD Card (3) Amazon
- IR LED Transmitter (3)
- IR Receiver (6) Amazon
- Red LED (3)
- Blue LED (3)
- Green LED (3)
- Passive Buzzer (3) Amazon
- Push Button (6)
- LCD 16x2 Screen with I2C Adapter (3) Amazon
- Portable Battery Pack (3) Amazon
- Micro to Regular USB Cable (3) Amazon
- PN2222 Transistor (3)
- 100Ω Resistor (3)
- 1kΩ Resistor (9)
In this project, we ended up taking the IR LED Transmitter from an old set of laser tag guns that had a black cone around the transmitter to help narrow the shot of each gun. However, any general transmitter should work.
In addition to the items listed above, the laser guns themselves were 3D printed. This project would therefore also require access to a 3D printer and filament. Overall, for three guns the total came to about $350.
Step 2: Server Setup
The first thing needed to setup the server is to install the Mosquitto MQTT Broker Service. Mosquitto is the service that provides a framework for communication between each of the devices in the game. This allows the server to send messages to each of the Pis connected to the service. In the terminal, run the following commands.
sudo apt-get update sudo apt-get upgrade sudo apt-get install mosquitto -y sudo apt-get install python3-pip -y sudo pip3 install paho-mqtt
Some of the GUI's for the server were created using a GUI designer called Pygubu. This can be installed by running:
pip3 install pygubu
More information on pygubu can be found at https://github.com/alejandroautalan/pygubu
Once MQTT and Pygubu are installed, create a new directory and copy the attached files. The directory should include:
Note: The attached images used in this project were not created by the development team and therefore does not claim authorship.
Step 3: Raspberry Pi Setup
This step will need to be repeated on each of the Raspberry Pis.
1. Install Operating System
First, start with a fresh install of Raspbian. We would recommend using the Lite version as it is less for the Pi to handle, but either version should work fine. The download can be found at https://www.raspberrypi.org/downloads/raspbian/
2. Install MQTT
Next we need to Install the MQTT broker service. We will be using Mosquitto for this. In the terminal, run the following commands.
sudo apt-get update sudo apt-get upgrade sudo apt-get install mosquitto -y sudo apt-get install python3-pip -y sudo pip3 install paho-mqtt<br>
Mosquitto is the service that provides a framework for communication between each of the devices in the game. This allows the server to send messages to each of the Pis connected to the service.
3. Install I2C Tools
The following command will install libraries that are used for the LCD screen.
sudo apt-get install -y python3-smbus i2c-tools sudo apt-get install rpi.gpio -y
The i2c address may need to be changed in the lcddriver.py file. The address can be found by entering the following command.
i2cdetect -y 1
4. Install and configure LIRC
Create a new directory and download the attached files to this location.
Most Internet browsers will not download files without extensions. To get around this, two of the files were uploaded with temporary extensions. Both the "lircrc.deleteExtension" and "modules.deleteExtension" should in fact be extension-less and the files should be renamed to "lircrc" and "modules" after they have successfully been downloaded.
This step installs and configures the dependencies for the Linux Infrared Remote Control (LIRC) package. For more information see the tutorial for setting up LIRC at: http://alexba.in/blog/2013/01/06/setting-up-lirc-on-the-raspberrypi/
First install the library, then copy the included files to their respective directories as shown in the commands below. Finally, restart the lircd service.
sudo apt-get install python3-lirc -y
From the newly created directory execute the following commands to move the configuration files to their correct locations.
sudo mv lircd.conf hardware.conf lircrc lirc_options.conf /etc/lirc/ sudo mv modules /etc/
Then restart the lircd service by running:
sudo /etc/init.d/lircd restart
Next, edit the /boot/config.txt file and add the following line
Reboot your pi to allow the changes to take effect.
5. Enable I2C and edit CLIENT for each player
Next, we will enable the I2C interface. This can be done using
and enabling the I2C in the "interface options" menu.
6. Edit Player CLIENT and LTSERVER
The game directory should now include the four remaining files.
The last step in configuring the pi is assigning each pi a CLIENT number and adding the location of the server. This is done by editing the included "player.py" file for each pi so that they all have a different CLIENT number. The CLIENT number is assigned on line 3 of player.py. Assign the first pi to be client "1", the second to be "2", and the third to be client "3".
The LTSERVER line should be changed to the IP address of the server. This is found by typing 'ifconfig | grep "inet addr" ' in the terminal of the server computer.
Step 4: Gun Assembly
Proceed to wire each of the guns according to the wiring diagram and schematic above.
Each of the peripherals are connected to the following GPIO pins on the Pi Zero:
- Buzzer: GPIO5
- Trigger: GPIO26
- Reload: GPIO12
- IR Transmitter: GPIO25
- IR Receivers: GPIO18
- RED LED: GPIO17
- GREEN LED: GPIO27
- BLUE LED: GPIO22
- I2C_SDA: GPIO2
- I2C_SCL: GPIO3
See the schematic for more details.
If desired, the laser guns can be 3D printed using the included step model files. Keep in mind that two of the "front1STL.STL" files must be printed.
Step 5: Game Play
The game is started by running the "ltag.py" file on the server. Once this is done, each of the players can connect to the server by running their respective "player.py" file.
Note: After plugging in the battery pack, it could take up to a minute for the pi to boot.
It may also be useful to add a cron job that automatically runs the player.py file once the pi starts. We had a hard time getting this to work and ended up adding a line to the "/etc/rc.local" file on each of the Pis to run the "player.py" file. This allows the game to be started without having to SSH into the Pi's to run the player script.
Once the game has been started with the players ready, a GUI will appear that allows a few of the game settings to be configured. The game begins after the Start button is pressed.
After each game, an ending GUI will appear with statistics about the prior game including tags, global percentages and game duration.
Note: Due to limitations in software libraries, the accuracy report tag locations are not representative of actual laser tags. In the current version, the Player Accuracy Report image is purely for aesthetics in hopes of a future version with actual tag location implementation.
Step 6: Future Improvements
Overall, the project was a major success. Along the way, we thought of a few of additional features that may be added to a future edition.
- Sturdier trigger design for the 3D printed guns
- Ending GUI drop-down menu to display statistics from past games
- More Infrared receivers that could be attached to the players' vests
- Additional game modes that can be chosen in the Pregame GUI
- More accurate tag location algorithm on the players stats page