ATtiny85 Snake Game

by __cultsauce__ in Circuits > Microcontrollers

12010 Views, 130 Favorites, 0 Comments

ATtiny85 Snake Game

fullsnake.png
score.png
Animated GIF - Find & Share on GIPHY
ATtiny85 SNAKEGAME preview WITH BUZZER

When it comes to electronics, it's the free-form designed ones that always catch my eye. Seeing how all the wires and components are connected just make it so fascinating and nice to look at. I wanted to try building something in a similar style. Being aware my soldering skills are not as good as I'd like them to be, I decided to go for a bit easier but still kind of nice-looking option. The whole build was a process of trial and error that probably cost me a bit more time than I'd like to admit. It was also the first time I ever used a ATtiny (or anything besides an Arduino Uno), which posed many challenges. But I ended up learning a lot throughout the making, so hopefully you'll do too!

Supplies

components.png
supplies.png
  • 1 x 128x64 I²C OLED Display (SSD1306 based)
  • 1 x CR2032 3V Battery
  • 1 x CR2032 Battery Holder
  • 1 x ATtiny85
  • 1 x 8 Pin DIP IC Socket
  • 4 x Push Button
  • 3 x 100k Ohm Resistor
  • 2 x 10k Ohm Resistor
  • 1 x 0.1uF Ceramic Capacitor
  • 1 x Slide Switch
  • 0.6 mm Enameled Copper Wire
  • Arduino Uno, breadboard and some jumper wires (for programming your ATtiny)
  • 2 x 8x11 cm Plywood Piece
  • 4 x 1.6 cm Bolts
  • Drill, Sanding Paper, Saw
  • Soldering tools

Programming Your ATtiny85

uploading.png
Screenshot from 2022-03-04 20-29-57.png

I was using a chip without a development board, so I used my Arduino Uno as an ISP (In-System-Programmer). To keep this part of the tutorial short, I recommend you follow this article on how to do this. For this project, I was using the internal 8MHz oscillator. Please note that to make the ATtiny run at 8 MHz, you need to burn the bootloader first.

The code consists of two parts, first is the "very minimalistic" library I wrote for the display (oled85.h and oled85.cpp). Both of these files have to be in the same folder as the file snakeGame.ino (which you'll be uploading). This library depends on the TinyWireM library developed by Adafruit, so you need to have it in your libraries (if you don't have it already, add it in Sketch->Include Library->Add Zip Library).

Then there is the snakeGame.ino file which contains the sketch you are going to upload to your ATtiny. Open it in your IDE, compile and upload. It will probably take a while. I tried throwing in some comments to make it at least a bit readable in case you'd like to try playing around with it a bit.

NOTE: by default the display assumes a I2C address of
0x3C, in case your display uses a different one, open the oled85.h file and change the #define I2C_ADDR statement.

You can download everything from my Github.

Testing It Out on a Breadboard

breadboard.png
schematic.png

You can skip this part, but I think it's good practice to test everything before moving on to the next step. It'll be harder to identify what's not working correctly when it's all soldered together. One issue that I encountered when building this on a breadboard was that the display had trouble rendering some frames correctly (probably due to the wonky connections). Everything worked like a charm when I soldered it together though.

Here's a bit of explanation how the connections work:

THE DISPLAY

Using the I2C protocol already set restrictions on what pins can be used here, the connections are as follows:

  • SDA to PB0 (physical pin 5)
  • SCL to PB2 (physical pin 7)
  • VCC to +3V
  • GND to GND

THE BUTTONS

The trickiest part of the circuit - the buttons. Since the ATtiny85 comes with a limited amount of digital pins that could be reliably used for this purpose (and two were already occupied by the display), the solution was to make use of the analog input instead. The buttons are connected in pairs to two of the analog pins - A2 (PB4/physical pin 3) reads input from UP and DOWN buttons and A3 (PB3/physical pin 2) reads input from LEFT and RIGHT. They are separated by resistors that form a voltage divider. Comparing the analog readings on each of the pins to the two thresholds (threshold1 and threshold2 in the sketch - can be calculated from the resistor values in the voltage divider) is used to determine which button is pressed. The connections are very chaotic, so I recommend you just follow the schematic carefully.

THE REST

The ground and power rails are connected to all the components in places, where there was spare place on the board. The reset pin of the ATtiny is connected through a 10k resistor to the positive power rail to stop the circuit from resetting randomly. Finally, a decoupling capacitor is placed between the VCC and GND pins of the ATtiny to filter out some noise coming from the power supply.

Preparing You Board

wooden_board.png
screws.png
trimm.png
dots.png
cork.png

I went with a 8x11 cm piece of plywood. It was small enough to still be considered pocket-sized and big enough to fit all the components on it.

STEP 1

Start by cutting out two 8x11 cm plywood rectangles. Continue by grabbing some sanding paper and smooth the surface to avoid splinters and make it look nice.

STEP 2

Drill 4 holes at the sides of both of the boards as shown in the second picture.

STEP 3

Now take one of the boards that you want to use as the front side. First we need to carve out a hole for the slide switch. If you don't have some specialized equipment you can do it as I did with a drill and a razor knife. Start by drilling (with a diameter similar to that of the switch) out two holes close to each other and then cut the rest out with a sharp razor knife.

STEP 4

Now, drill all the holes for the components and wires. For this, I used a 0.8mm drill bit as it was the smallest one I had. The picture above shows the layout I used for my project, but you may need to alter it a bit based on the components you use. Also, if you are using a regular drill with really small drill bits like me, you have to be very careful not to put too much pressure on it - they break very easily.

STEP 5

Finally, I placed a bit of cork around the borders to avoid pressure from the wooden boards on the fragile wired connections (next step).

Connecting Everything

board_scheme.png
back_side.png
make.png

Now you can place all components and wires to their corresponding holes as in the picture above and start soldering everything together. The red lines represent the connections on the back side and the dark blue ones are the ones that are visible from the front.

Do not worry too much about how it looks from the back side, it won't be seen in the final product (just take a look at how ugly mine looks haha). I tried to solder all the wire junctions on the back, so that when I accidentally burn the wood, it won't ruin the front side.

Now try turning it on. If it works, then you did a great job. Now only the last step remains ...

Screw and You're Done

screws3.png
screws2.png
game.png
backside.png

The last thing to do is take the two boards, put the cork border in between and fasten it all together with the bolts.

And that's about where I end this. Hope you found this helpful. It may not be the best game ever made, but at least you can pat yourself on the back for even being able to build something like this yourself. Thank you for sticking by all the way through! c:

UPDATE: Sound Effects!

tinyBuzzer.png
attiny85.png
board_dots.png

Now that the semester ended I had time to upgrade this a bit (I also cleaned up my messy code from the first version haha). I ended up adding a buzzer to complete the retro feel of this entire project and also make use of the one last pin that was left free. The upgraded schematic is above and the code is up on github.