Tetris Clone With OLED SSD1306(I2C) for Arduino Nano / Uno

13,500

34

15

This Instructable is about getting Tiny Tetris running on a 128x64 OLED display using an Arduino Nano or Uno.

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: What You Need.

Arduino Nano or Arduino Uno:

Nano

Uno

Display:

OLED I2C SSD1306

Keypad:

AD Analog Keyboard Module

Note: other key pads and switch configurations could be used but you would need to change the code.

Some cables to connect things:

Cables

Step 2: Uno Wiring

Uno wiring:

Note: the key pad shown represents the pad in the parts list, I have not built the one shown but if you did it would be easy to get it working, only a few small changes would be needed in the code.

Fritzing Part

Step 3: Nano Wiring

Nano wiring:

Note: the key pad shown represents the pad in the parts list, I have not built the one shown but if you did it would be easy to get it working, only a few small changes would be needed in the code.

Fritzing Part

Step 4: The Code

If you are new to Arduino have a look at this to get started:

How to have fun with Arduino

Code attached and on GitHub below:

Code on GitHub - this has the most up to date code including Art work and Sound!

There is still a lot to be done with the code, see below:

High score functionality.

[Done] Better key pad control code.

Decent random number generator.

[Done] Start thinking about adding sound.

Create a letter font, create a proper system for rendering numbers and letters.

Tidy up code and optimize for memory, sort out the globals and types.

Create defines for all the magic numbers but they are useful for now.

Step 5: Have Fun!

2 People Made This Project!

Recommendations

  • CNC Contest

    CNC Contest
  • Make it Move

    Make it Move
  • Teacher Contest

    Teacher Contest

15 Discussions

0
None
big daddy 69

1 year ago

my right button and my spin button make really fast movements but overall it works well

0
None
AivarasC1

1 year ago

Hi, why right key doesn't work ? It doesn't even react in serial monitor when I am using

analoginSerialout example.

0
None
Dj0ntleman

1 year ago

I got the AD Analog Module, but for some reason only one of the buttons actually works in the game. Do you think this is a code issue or is it the module?

1 reply
0
None
BADFEEDDj0ntleman

Reply 1 year ago

Hi,

In the Arduino IDE if you go to file -examples - analog - analoginSerialout

Upload the sketch to your board and start the serial monitor - press the various buttons on your pad and make sure the values change.

If all is good make a note of the values for each button.

In the processKeys() function of the Tetris code you can set the limits for each key:

else if (analogKey < 510) && (analogKey > 500)) {

....

...

uiKeyCode = KEY_RIGHT;

so if your right key value is 300 you would change values above from 510 and 500 to something like 310 and 290

Hope this explains and it works out for you.

best regards,

0
None
owen.huston

1 year ago

I have a 6 pin OLED expansion with a res pin and a DC pin. Would it be possible to share the wire configuration for something like that.

1 reply
0
None
BADFEEDowen.huston

Reply 1 year ago

Hi Owen,

is it one of these types?

https://www.dzduino.store/module-daffichage-oled-0...

I have never used one but according the the instruction in the above link its possible to put it into I2C mode although some soldering is required, not sure if you can do that?

After that I would imagine it would work.

I keep meaning to get an SPI version of the screen and make the code changes for that type, as long as the screen is based on the SSD1306 it should work fine.

Let me know if you reconfigure your board and get it working.

Best of luck!

0
None
jbc8

1 year ago

Can you make a version that does SPI?

1 reply
0
None
BADFEEDjbc8

Reply 1 year ago

Hi,

I don't have a SPI version of the display, I think it would be a small change in the code though.

Ill order some and experiment but honesty it could be some time, might be quicker ordering a I2C version of the display yourself.

0
None
iHayri

1 year ago

Hi,

Love your Tetris game, great work. Could you give me a hint how to make it work with a 1.3" display that uses the SH1106 chip? I know that only the lower column address needs to be changed from 0x00 to 0x02. This shifts the display to make it work with SH1106 chips. But I could not find that line in your code. You do have this: #define OLED_SET_COLUMN 0x21 but the 0x21 throws me off and changing it messes up the display. Your help would be greatly appreciated. Thanks a lot.

2 replies
0
None
BADFEEDiHayri

Reply 1 year ago

Hi,

I don’t think I can help you I’m afraid.
I had a quick look over the SH1106 data sheet and from what I can see it’s not compatible with the SSD1306.

There is no Arduino display library used for the Tetris game, it talks directly to the display, this was done for speed so unless the SH1106 has the exact same command set it won’t work as far as I know.

For instance, Tetris uses the vertical addressing mode on the SSD1306 to quickly draw the blocks, I don’t see any reference to addressing modes in SH1106 data sheet.

Note: I have not studied the SH1106 data sheet very well so I could be wrong, please let me know if you have any info on this.

0
None
iHayriBADFEED

Reply 1 year ago

Hi,

Thank you so much for your reply. I did take a look at the libraries and read about the differences about the SDD1306 and SH1106. The main difference is that the SSD1306 has a 128x64 pixel driver and the SH1106 has a 132x64 pixel driver. And when using the command mode all that is need is to shift the pixels by the amount of 2 so it is centered on the display as I wrote above. I did edit my GPS code that uses also command lines because it is over 6 years old and it works without any problems. But drawing the blocks might be a different matter although displaying converted bitmaps works well to with the SH1106. By looking at the Adafruit_SH1106.cpp files the commands are the same. Anyway I will continue to look into this as I would love to make Tetris work on 1.3" displays too. I let you know here if I find a solution, thanks again.

0
None
D6equj5

2 years ago

Hi,

I've stuck a 1306 oled disp on a Mega and loaded your code however all I get is like a star screen (random points not moving). Do I need to have the JoyStick actually plugged for it to work or have I missed something else?

Thanks

D6

1 reply
0
None
BADFEEDD6equj5

Reply 2 years ago

Hi,

you don't need the joystick plugged in for it to work.

I have never tried it on a Mega but it should work.

Have you tried other display library's with the 1306 on the Mega?

I assume the 1306 you have is I2C interface?

regards,

0
None
SarahH25

2 years ago

another christmas present idea!

0
None

Fun project for teaching people about Arduinos and displays.