Memory Game
This project is a memory game built with an Arduino. It tests how good your memory is by showing you a random sequence of 3 colored LEDs and making you repeat it back using buttons.
Each round gets one color longer, so it gets harder as you go. There are 5 rounds total. The 7-segment display shows what round you're on.
If you press the wrong button, the RGB LED flashes red and a losing sound plays and then the game restarts. If you make it through all 5 rounds, the RGB LED turns purple and a win sound plays.
I picked this project because it uses a lot of the stuff we learned in class, LEDs, buttons, a buzzer, a display, and a transistor, all working together as an actual game you can play.
Supplies
- Arduino Uno
- Breadboard - Full-Size
- RDG LED
- LED (Green, Red, Yellow)
- Piezo buzzer
- 7 Segment Display (The one used in this project is Common Anode (CA))
- 3 Buttons (one for each LED Green, Red, Yellow)
- 3x 10k resistors (used for the buttons)
- 3x 330 ohm resistors (used for the LEDs)
- 4x 560 ohm resistors (used for the RGB LED, Transistor and 7 Segment Display)
- Wires(Green, Yellow, Black, Red)
- 1x NPN Transistor (2N3904)
Connect Power and Ground
First plug a red wire from the Arduino's 5V pin to the + rail on the breadboard, and a black wire from GND to the - rail. Then bridge the top and bottom rails together so both sides of the breadboard get power and ground. Now every part you plug in can access power and ground.
Connect Wires From Arduino Pins to Buttons
Put the 3 buttons on the breadboard. Connect one side of each button to pins 8, 10, and 12 on the Arduino using green and yellow wires. Connect the other side of each button to the ground rail through a 10k resistor. The extra green and yellow wires going down are for the LEDs added in the next step (leave those in place). Note: the 330 ohm resistors are also placed on the breadboard at this stage just to save space for later, but they get used in Step 3 for the LEDs.
Connect the LEDs
Plug the red, yellow, and green LEDs into the breadboard next to their buttons. The long leg of the LED connects to the Arduino pin through a wire. The short leg connects to the 330 ohm resistor, and the other end of the resistor goes to the ground rail. This stops too much current from flowing and burning the LED out. Yellow is pin 11, green is pin 9, red is pin 13.
Connect the RGB LED
Plug the RGB LED into the breadboard. Connect the red pin of the RGB LED to pin 6 using a green wire and the green pin to pin 5 using a green wire. The cathode pin connects through a 560 ohm resistor then to the negative rail. A red wire connects the blue pin to the positive power rail. Blue means watch, red means you lost, and when both are on at the same time it makes purple which means you won.
Connect the Buzzer
Plug the buzzer into the breadboard. For now just lay out a wire on the negative side of the buzzer which connnects to the negative rail. The full wiring will be shown in Step 8 with the transistor. The buzzer beeps a different sound for each color and plays a win or lose tune at the end of the game.
Connect the 7-Segment Display
Plug the 7-segment display into the breadboard. Connect each segment pin to the Arduino using yellow wires. The picture on the right shows exactly which segment goes to which pin. There are two 560 ohm resistors on the common pins, one on top and one on the bottom, which connect to the power rail since this display is common anode. The display shows the round number 1 to 5 and a smiley face at the start of the game.
Add a Transistor for the Buzzer
Now we add the NPN transistor to control the buzzer. Connect the C pin to pin 3 on the Arduino using a green wire. Connect the B pin through a 560 ohm resistor to pin 6 which is the same pin used for the RGB red pin. Connect the E pin to the positive power rail using a green wire. Pin 3 just sends a small signal to the transistor and the transistor switches the buzzer on and off.
CODE
The code gives every pin a name so it's easy to read. seg is a list of the 7 display pins. nums is a table of which segments to turn on to draw each number or smiley. 1 means on and 0 means off.
setup() runs once at the start and sets all pins as input or output.
showNum() draws a number on the display. showColor() lights up an LED and plays its sound. allOff() turns everything off between flashes. checkButton() checks which button is pressed and returns -1 if nothing is pressed.
loop() is the actual game. It shows a smiley and waits for a button press to start. Then it picks a random pattern of 5 colors and goes round by round showing the pattern and waiting for the player to copy it. If you press the wrong button the RGB goes red and a lose sound plays. If you get all 5 rounds right the RGB turns purple and a win sound plays.
Credit for original code and inspiration HERE
Play and Enjoy
Upload the code to your Arduino and press any button to start. The display shows a smiley face. Watch the LEDs carefully and remember the order they flash. Then press the buttons in the same order. Each round adds one more color to remember. Get all 5 rounds right and the RGB turns purple with a win sound. Press the wrong button and it flashes red with a lose sound and the game restarts.
Research Section
When I started this project I looked at the rubric which had a list of Arduino project links. One of them was the Arduino Memory Game on Instructables and it immediately grabbed my attention because I always wanted to know how a memory game was built. I clicked on the link which took me to a project by TarundeepK. It used 4 LEDs, 4 buttons, a buzzer, a 7-segment display, and an RGB LED which was pretty close to what I wanted to build.
I used that project as my starting point and reference for how to wire everything up and how the game should work. But I made several changes. I used only 3 LEDs and 3 buttons instead of 4. I also rewrote the code from scratch using arrays and for loops to make it shorter and cleaner. On top of that I added an NPN transistor to control the buzzer which the original project did not have.
HELP Center/Works Cited
TarundeepK. "Arduino Memory Game." Instructables, 4 Feb. 2021, www.instructables.com/ARDUINO-MEMORY-GAME-2/
Arduino Reference. tone(). https://www.arduino.cc/reference/en/language/functions/advanced-io/tone/
Arduino Reference. digitalWrite(). https://www.arduino.cc/reference/en/language/functions/digital-io/digitalwrite/
Common Mistakes Section
Here are some mistakes to watch out for when building this project.
- Transistor pins wrong — the 2N3904 has 3 pins C, B, and E. Mix them up and the buzzer won't work. Always look up the pinout first.
- Buzzer positive leg not connected to 5V — if it's floating the buzzer won't make any sound at all.
- LED in backwards — the longer leg is positive and goes to the Arduino pin. Shorter leg goes to ground.
- Wrong resistor value — use 330 ohm for LEDs, 560 ohm for RGB, display, and transistor base.
- 7-segment pins wrong — wrong pin means wrong segments light up and wrong numbers show. Check which segment matches which pin before wiring.
Theory
Resistors limit shows much current flows through a component. Without them the LED would burn out or the Arduino pin would get damaged. In this project 330 ohm resistors are used for the LEDs and 560 ohm for the RGB and display.
Current sinking is when current flows into a pin instead of out of it. The transistor handles this for the buzzer. Pin 3 sends a small signal to the base and the transistor does the rest, the Arduino never carries the full buzzer current.
PWM means the pin switches on and off really fast to control power levels. Pins 5 and 6 use PWM for the RGB LED which is why they have the ~ symbol on the Arduino. The buzzer also uses PWM through tone() to play different pitches.