Posted on by & filed under Content - Highlights and Reviews, Web Development.

“Simple Simon met a pieman,
Going to the fair;
Says Simple Simon to the pieman,
Let me taste your ware.” – Nursery Rhyme

I have very fond memories of the great electronic games of the 70’s and 80’s. It was an amazing time when you could spend an entire afternoon hunched over a molded plastic case with a friend, pretending 7-seg LEDs were football players. For years if you wanted a portable video game, what you got was something with a few lights and a couple of buttons, but it was enough.

Looking back, there was an elegant simplicity to some of these games. I thought it would be fun to try reproducing one of them using an Arduino, 4 buttons and 4 LEDs. You remember the game, it was a memory test. The game would blink a couple of large colored buttons, and you had to reproduce the pattern (which got longer after every try). Let’s make that game with an Arduino. We’ll call it Pieman.

What We Need

We’ll start by picking out 4 differently colored LEDs. Rooting around in my supplies I can easily find a red, yellow, green and blue one (well, an RGB one in my case, but I’ll make it work). And a few resistors, of course.

We’ll also need 4 buttons. We’re looking for Single Pole Single Throw switches, meaning that one wire goes in, one comes out, and pressing the switch closes the circuit. And since we don’t want the switch to stay on, we want Momentary switches, meaning the circuit is only closed while the switch is depressed. These aren’t hard to find. Even your local “We Used To Be For The Hobbyist But Now We Mostly Sell Phones” electronics store likely has four.

You’ll want to do all of this on a breadboard as well. And of course, you’ll need an Arduino, cable for syncing, IDE, jumpwires, etc. Here’s a look at the initial setup:

You’ll already have most of this if you’ve done an Arduino project before. If this is your first project, you should really go through the Blink tutorial first.

The Game In 50 Words Or Less

Pieman blinks an LED. The player pushes a corresponding button to blink the same LED. Pieman repeats the sequence, adding 1 more random LED at the end. The player repeats. This continues until the player gets the sequence wrong.

Building Pieman In Stages

We’re going to build Pieman gradually. We will start by wiring up all 4 LEDs such that they simply blink on and off at a fixed interval, much like the Blink tutorial.

Next, wire up the LEDs so that pushing one button will turn all four LEDs on. You can do this using the demonstration from the Button tutorial.

Next, wire one button to each LED, so that pressing one button turns on only that one LED. Looking down at your breadboard, Pieman is essentially built.

Here is the code to make these buttons work:

Everything from here on out is code.

Coding The Game Logic

A good first step would be to write some code to add a random LED to a sequence, play the sequence, and pause before repeating. This will give you the first half of the game logic. At this point I realized I’d make my life easier if I made the LED pins 2-5 (to simplify randomizing). You’ll see that in the code.

First let’s set up a sequence:

If you find someone who can get through 100 items in the sequence, they’re a world record holder. We’ll reset the sequence if we hit the max, essentially starting the game over.

Throw this in your setup to seed the random function.

You will want a function to reset Pieman when there’s a bad button press. I ran this during setup as well, to verify the LEDs are all working.

Next you’ll want to add a loop after the sequence, which takes Player button pushes, compares them to the sequence, and throws an error if the wrong button has been pressed.


That’s really all there is to it. You’ve just created a simple memory game using the Arduino, and you probably did it with stuff you had floating around in your parts bin. Want to give Pieman a voice? Check back later this week for some tips on adding sound.

Safari Books Online has the content you need

Here are some good Arduino resources found in Safari Books Online:

Getting Started with Arduino gives you lots of ideas for Arduino projects and helps you get started with them right away. From getting organized to putting the final touches on your prototype, all the information you need is right in the book.
Arduino Cookbook helps you create your own toys, remote controllers, alarms, detectors, robots, and many other projects with the Arduino device.
Building Wireless Sensor Networks helps you build a series of useful projects, including a complete Arduino- and XBee-powered wireless network that delivers remotely-sensed data.
In Beginning Arduino teaches by using an amazing set of 50 cool projects. You’ll progress from a complete beginner regarding Arduino programming and electronics knowledge to intermediate skills and the confidence to create your own amazing Arduino projects.
In Arduino Robotics will show you how to use your Arduino to control a variety of different robots, while providing step-by-step instructions on the entire robot building process. You’ll learn Arduino basics as well as the characteristics of different types of motors used in robotics.

About the Author

  Duane O’Brien is a tired computer scientist. He has written a number of articles on developing web applications and various PHP frameworks. To learn more about Duane, check out his blog or read his tweets.

Tags: Arduino, Blink, Button, electronic games,


  1.  Arduino Pieman Tip: Giving Voice to the Project « Safari Books Online's Official Blog
  2.  Arduino Pieman Tip: Enhancing the Project « Safari Books Online's Official Blog
  3.  Arduino Pieman Tip: Giving Voice to the Project | Safari Books Online's Official Blog