Memory Game

by 921000 in Circuits > Arduino

17 Views, 0 Favorites, 0 Comments

Memory Game

IMG_5693.JPG
IMG_5694.JPG
IMG_5695.JPG

In this project, we will build an interactive Arduino-based memory game that challenges the player's ability to remember a growing sequence of button presses. The idea is inspired by the classic Simon Says game, but with an innovative twist: as the player progresses through increasingly difficult levels, a timer restricts how long they have to press each button based on the selected difficulty. Additionally, a visually captivating line chaser animation lights up when the player completes all levels, giving a satisfying conclusion to the game.

The memory game combines software logic programmed into the Arduino with hardware circuits, including an RGB LED for status feedback, a buzzer for sound effects, and a 555 Timer + CD4017-based line chaser for a celebratory animation. This combination of analog electronics and digital programming makes it a perfect project to explore real-world applications of electrical components, timing, programming, and logic circuits.

The schematic of this project is divided into two main parts:

  1. The Memory Game System (Arduino-controlled): Includes LEDs, buttons, RGB status LED, and a piezo buzzer. It is fully controlled by the Arduino.
  2. The Line Chaser Animation (IC-controlled): Built using a 555 Timer in astable mode connected to a CD4017 Decade Counter, which lights up 8 white LEDs in sequence.

We'll begin with a comprehensive list of components and then walk step-by-step through the physical setup, circuit connections, and code explanation.

đź”§ Schematic Note:

This project includes two main circuit sections:

  1. Arduino-Controlled Memory Game – includes 4 colored LEDs, 4 buttons with pull-down resistors, RGB LED, and a buzzer.
  2. Line Chaser – built using a 555 timer and CD4017 IC with 8 white LEDs.
  3. You should draw or capture each schematic separately for clarity and understanding, labeling connections to Arduino pins, resistor values, and IC pin numbers.


Supplies

For this project, you will need the following components:

Memory Game Section (Arduino-based):

  1. 4 LEDs (Red, Green, Blue, Yellow)
  2. 4 × 330Ω resistors for the LEDs
  3. 4 push buttons
  4. 4 × 10kΩ resistors for pull-down on push buttons
  5. 1 RGB LED
  6. 1 × 560Ω resistor for RGB LED
  7. 1 Piezo buzzer
  8. Jumper wires
  9. Breadboard
  10. Arduino Uno

Line Chaser Section (IC-based):

  1. 8 white LEDs
  2. 2 × 10kΩ resistors (pull-down for outputs)
  3. 1 Ă— 10ÎĽF capacitor (for 555 timer control)
  4. 1 Ă— 555 Timer IC
  5. 1 Ă— CD4017 Decade Counter
  6. Additional jumper wires

Initial Setup

Begin by collecting all the required components and placing them neatly on your workspace. Set your Arduino Uno board and a single large breadboard on a flat, clean, and well-lit surface. This breadboard will house both the memory game circuit and the line chaser circuit, so make sure it's large enough to accommodate everything with some breathing room for jumper wires and spacing. If you only have a smaller breadboard, consider splitting the build across two phases or working in sections to avoid clutter.

Sort all your components by type — group the LEDs (red, green, blue, yellow, RGB, and white) together, line up the resistors by value (330Ω, 10kΩ, 560Ω), and place the push buttons, piezo buzzer, capacitor, 555 timer IC, and CD4017 counter IC in a visible, accessible arrangement. Take a moment to double-check that you have all the necessary parts before moving forward, as this will save time during assembly.

Wiring the Colored LEDs and Push Buttons

IMG_5672.jpg

Place the four colored LEDs — red, green, blue, and yellow — onto the breadboard spaced out evenly for wiring convenience. Connect the anode (longer leg) of each LED through a 330Ω resistor to the Arduino’s designated digital output pins: red LED to pin 13, green LED to pin 7, blue LED to pin 11, and yellow LED to pin 9. Connect all the cathodes (shorter legs) of the LEDs to the breadboard’s ground rail.

Now, set up the push buttons. Insert each push button so that it straddles the middle gap of the breadboard, ensuring two legs are on one side and two legs on the other. For each button, connect one leg on the first side directly to the Arduino digital input pin assigned for that button: red button to pin 12, green button to pin 4, blue button to pin 10, and yellow button to pin 8. The opposite leg of each push button should be connected to the breadboard’s +5V power rail.

To prevent the Arduino input pins from floating (which could cause unpredictable readings), connect a 10kΩ resistor from each Arduino input pin to ground. This pull-down resistor ensures that when the button is not pressed, the input pin reads a stable LOW signal. When the button is pressed, it closes the circuit between +5V and the input pin, causing the Arduino to read a HIGH signal.

This wiring means each button press results in a HIGH input reading, and releasing the button returns it to LOW. This straightforward setup simplifies the logic in your Arduino code.

Wiring the Common Anode RGB LED and Piezo Buzzer

IMG_5673.jpg
IMG_5674.jpg

Next, place the common anode RGB LED on the breadboard. For this LED type, the common pin (anode) connects to +5V power on the breadboard.

Then, connect each color cathode through a 560Ω resistor to Arduino digital output pins that will control the colors. Connect the red cathode through a resistor to pin 6 and the green cathode through a resistor to pin 5. Since this is a common anode LED, the colors will light up when their respective pins are set to LOW by the Arduino (this is because the current flows from +5V through the LED cathode pins to ground).

The blue pin of the RGB LED should also be connected directly to ground if it is unused in your code.

Finally, connect the piezo buzzer’s positive terminal to Arduino pin 3 and its negative terminal to ground. The buzzer will provide audible feedback for game events such as button presses, mistakes, and wins.

Building the 8-LED Line Chaser Circuit With 555 Timer and 4017 Decade Counter

IMG_5675.jpg

To create the animated LED chaser feature of your Arduino memory game, we will build a simple yet powerful line chaser circuit using a 555 timer IC and a CD4017 decade counter IC, controlling 8 white LEDs in a smooth, sequential pattern. Begin by placing the 555 timer on your breadboard with the notch or dot facing left, which helps correctly identify the pinout layout.

Now begin the wiring:

  1. Connect pin 1 of the 555 timer to GND (the ground rail) and pin 8 to +5V.
  2. Join pins 2 and 6 with a jumper wire, and also connect pin 4 to pin 8 (this disables the reset pin so the timer runs continuously).
  3. Place a 1ÎĽF capacitor on the board: its negative leg goes to pin 1, and its positive leg to pin 2.
  4. Place a 1kΩ resistor between pin 7 and pin 8, and a 47kΩ resistor between pins 6 and 7.

These components form an astable multivibrator configuration, which causes the 555 timer to output a steady square wave (clock signal) on pin 3, the output pin.

Next, place the CD4017 decade counter on the breadboard, again with the notch facing left. Wire pin 16 to +5V, and pin 8 to GND. Connect both pin 13 (disable) and pin 15 (reset) to GND to ensure continuous counting without interference or resets.

Using a jumper wire, connect pin 3 (the output of the 555 timer) to pin 14 of the 4017 — this is the clock input, which will trigger the IC to cycle through its outputs sequentially. Each clock pulse from the timer advances the "high" output to the next pin on the 4017 IC.

Now, wire up your 8 LEDs to the 4017’s first 8 output pins. These will light up one at a time in a continuous loop, giving the iconic chasing effect. The pin mapping for your 8 LEDs is:

  1. LED 1 → pin 3
  2. LED 2 → pin 2
  3. LED 3 → pin 4
  4. LED 4 → pin 7
  5. LED 5 → pin 10
  6. LED 6 → pin 1
  7. LED 7 → pin 5
  8. LED 8 → pin 6

Connect each LED’s anode (long leg) to one of these output pins. Then connect each cathode (short leg) to GND through a 470Ω resistor. You can use a shared negative rail and place the resistors either inline before the GND connection or directly between each LED and the ground rail.

Note: We are only using 8 outputs of the 4017 (even though it can go up to 10). To make the sequence loop after the 8th LED, you must connect pin 9 (Q8) to pin 15 (reset) using a jumper wire. This way, the 4017 resets itself once it reaches the 9th output, effectively limiting the cycle to 8 LEDs.

Once you’ve completed these steps, power the circuit using the 5V and GND pins from your Arduino. The LEDs should begin to “chase” in a loop, with the speed determined by the 555 timer’s resistor and capacitor values. If you want to fine-tune the speed later, you can replace the 47kΩ resistor with a 100kΩ potentiometer, allowing you to adjust the chase rate dynamically.

Power Distribution and Final Wiring Check

Use the Arduino’s 5V and ground pins to power the breadboard rails, supplying both the memory game components and the line chaser circuit.

Double-check all connections carefully: ensure LED polarities are correct, resistors are properly connected, push buttons are wired for HIGH detection when pressed, and that the RGB LED common anode is connected to +5V.

Uploading the Code and Understanding How It Works

p1ittdsbus4nipic18ompik9p44-1.png
p1ittdsbus4nipic18ompik9p44-2.png
p1ittdsbus4nipic18ompik9p44-3.png
p1ittdsbus4nipic18ompik9p44-4.png
p1ittdsbus4nipic18ompik9p44-5.png
p1ittdsbus4nipic18ompik9p44-6.png
p1ittdsbus4nipic18ompik9p44-7.png
p1ittdsbus4nipic18ompik9p44-8.png

Once your physical build is complete, it’s time to bring the project to life by programming the Arduino to handle gameplay logic. Start by connecting your Arduino Uno to your computer using a USB cable. Launch the Arduino IDE on your computer. Make sure your Arduino board is properly selected by going to Tools > Board > Arduino Uno, and ensure the correct port is selected under Tools > Port.

Your code is the heart of the project—it controls how the memory game operates and interacts with the components. When writing the code, you should first define all the necessary pins using #define or constant variables. This includes pin assignments for the four LEDs, four buttons, RGB LED pins, and the buzzer. It’s also a good practice to define constants for difficulty levels and timing thresholds.

The first function you’ll use is setup(), which runs once when the Arduino powers on or resets. In this function, set all LED pins and RGB pins as OUTPUT, and all button pins as INPUT. You may optionally use Serial.begin(9600); to allow player interaction through the Serial Monitor.

Then comes the core game loop defined in loop(). When the game starts, the Arduino displays a welcome message via the Serial Monitor and prompts the player to select a difficulty level—easy, medium, or hard. Each difficulty setting adjusts the amount of time a player has to respond. For example:

  1. Easy might allow 5 seconds per button press,
  2. Medium might allow 3 seconds,
  3. Hard could reduce it to just 1.5 seconds.

This is handled by a selectDifficulty() function that waits for input via Serial.readStringUntil('\n'), storing the response and setting a corresponding delay threshold.

Once a difficulty is selected, the game starts generating a random sequence of LED flashes using random() to choose values between 0 and 3 (corresponding to the four colors). This logic resides in a startLevel() or playSequence() function, which loops through an array of increasing length and flashes the corresponding LEDs using digitalWrite() combined with short delay() commands and buzzer tones. A showPattern() helper function may be used here to modularize code.

The player must then repeat the pattern using the physical buttons. Each button press is detected using digitalRead() on the respective input pins. Since your button wiring treats a HIGH signal as a press (because one leg is connected to 5V), your code should check for digitalRead(buttonPin) == HIGH. Use a small debounce delay (e.g., 50ms) to avoid false readings. You can use a function like waitForButton() that starts a timer, waits for the correct button, and returns true if the input is correct or false if time runs out.

If the input is incorrect or takes too long, a loseLife() function subtracts a life and signals a failure using a buzzer tone or by briefly turning the RGB LED red (both red and green pins LOW since it's a common anode RGB LED, and blue is always grounded). The player is given a total of 3 lives. When lives reach 0, the game ends and offers a reset prompt.

If the player succeeds in completing all rounds, the win() function activates. This function turns the RGB LED green (by setting both red and green pins LOW, since the blue pin and the anode are already grounded). Then, it sends power to the line chaser circuit (which you may do by simply ensuring it's always on and only mentioned when you win, since it's built with discrete hardware). The sequence of 8 LEDs then begins to “chase,” visually celebrating the player's success.

The code should be modular and readable. You can improve clarity by organizing it into reusable functions:

  1. selectDifficulty() – Handles user input from the Serial Monitor.
  2. startLevel() – Randomly generates and displays the LED pattern.
  3. waitForButton() – Waits for button press with timeout.
  4. loseLife() – Handles lives and failure response.
  5. win() – Triggers victory actions like RGB lighting and line chaser.

Once you’ve completed writing or editing the code, click Upload (the right arrow icon) in the Arduino IDE to compile and upload it to the Arduino Uno. If everything is wired correctly, the game should begin running right away.

Demonstration and Gameplay

Power on the Arduino and open the Serial Monitor. Enter your difficulty level. The game begins by flashing a short random LED sequence.

Press the push buttons matching the sequence within the time limits. Since the buttons are wired to send HIGH signals on press, the Arduino reads your input accurately. Correct inputs add to the sequence length; mistakes or timeouts cost lives.

Upon winning, the RGB LED lights green (pins 5 and 6 LOW), the buzzer plays a victory sound, and the line chaser LEDs animate in sequence.