PWM

In the event of the Raspberry Pi’s 4th (1st) birthday, I’ve been working on a tic tac toe arcade game using the Raspberry Pi’s GPIO with Python. Basically, a human player battles against the computer on a 3×3 LED grid. The player uses a pushbutton 4-way keypad with a ‘select’ button to play. I also added a speaker and coded some neat sound FX!

Below is a demo of the game:

Hardware

Parts used for the tic tac toe game

The hardware for this project is pretty basic:

Raspberry Pi

9x green LEDs

5x pushbuttons

Speaker

Trim potentiometer

9x 330Ω resistor

5x 10kΩ resistor

Adafruit Pi Cobbler with ribbon cable

Wiring schematic for tic tac toe made with Fritzing

LED Grid Diffuser

In order to make the playing experience more pleasant, I designed a 3×3 grid LED light diffuser and 3D printed it so as to make the light from the LEDs less blinding. It also keeps the LEDs lined up nice and covers some of the wires up too!

Computer AI

This was actually my first experience dealing with any computer AI, and I discovered that it is quite fun! I designed it so it has four logical steps:

First, if there’s any move where the computer will win (i.e. the computer already has two in a row), the computer will play that move.

Second, if the player could win on the next turn, the computer will block it.

Third, if the center of the board hasn’t been played, the computer will play the center.

Finally, if none of the options above are applicable, the computer will play a random move not already played.

Software

For the software, click here. It’s written in Python 3 with the RPi.GPIO library, and it’s actually the longest bit of code that I’ve written as of yet. 🙂

Here’s an extra video with more info about the project:

Building the final prototype for the tic tac toe gameCloseup of LED grid and buttons

I spent a bit of last weekend experimenting with the Raspberry Pi, and I came up with a fake candle! I used the cap of an old Christmas light, a yellow LED, some small dowels drilled with holes, and a lot of superglue. Then I programmed it in Python using the software PWM so it flickers realistically. I didn’t use any solder, I just kind of twisted the wires together. I also made an Arduino version as well.

#The Pi Candle Code

#Amanda Cole 2015

import RPi.GPIO as GPIO, time, random
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(26, GPIO.OUT) #26 is the LED