Build a Complete AVR System and Play Mastermind Using Microcontrollers

The game Mastermind has been around a long time, and I remember getting a board version with colored pegs when I was a kid. I love this game, as it is solvable simply by pure logic. One player (or a computer/microcontroller) chooses a sequence of colors from a pool of available colors and then the other player attempts to figure out what that sequence is.

This is the AVR version of that famous logic game Mastermind. In this game, the MCU will choose four colors out of seven and you will try to deduce what colors the MCU picked. The MCU’s color choices may include duplicates. For example, the AVR may pick “blue red white white.” Your task is to figure out the target color sequence by using logic. The available colors are: blue red yellow green white black orange. The standard game continues for eight rounds.
The MCU will give you two pieces of information upon which to base your next color choice: the number of colors in your sequence that are also in the target sequence and the number of colors that are correct that are also in the correct slot. For instance, if the target sequence is “blue red white white” and you picked “blue white green yellow” the MCU would inform you that you have two
colors that are correct, and one of them is in the right slot. If you chose “white white green yellow” the MCU would inform you that you have two colors correct, but none of them are in the right slot.
High score and configuration information is written to EEPROM.
In this instructable I will show you how to build a complete, regulated power, AVR development environment that you can play the Mastermind game on (as well as having a development environment for all of your other projects), including the firmware to run it. Alternatively, you may run this code on an Arduino or clone without having to build anything. I’m also making AVR Terminal available for download.

Step 1: Equipment List

This firmware can be run on any AVR with 10k flash storage. If you cut and prune the source, you can get it to run on an 8k ATtiny AVR. My system was built from “scratch” and includes a Light Determining Resistor (LDR) to provide enhanced randomization when the MCU picks its colors. If you want to build a stand-alone AVR system with serial terminal capabilities, I’ve included direct links to most of the items so you can buy most everything from the same place (cutting shipping costs). Chances are, if you’ve already been playing with AVR’s and stuff, you might have a bunch of this stuff lying around already.
You will need the following parts:

AVR Stand-alone System

    • ATmega328p ( $4.30 from [you can substitute an ATmega168, ATmega32, or any other AVR that has ~ 10k flash memory]
    • 2 x 27pF capacitors ( $0.23 each from
    • 3 x 0.1uF tantalum capacitors ( $0.40/ea from )
    • 47uF electrolytic capacitor ( $0.18 from )
    • 20 MHz crystal 20pF capacitive load ( $0.57 from [you can use 16MHz or other AT-cut crystal]
    • 2 x 10k ohm resistors ( $0.12/ea from
    • 1 x 1k ohm resistor ($0.12 from
    • 1N4002 diode ($0.11 from
    • 10uH inductor ($0.59 from or a smaller sized one if handy)
    • tactile switch ($0.26 from
    • 2 x 3mm LED (a green one and a red one) * note the red one has an integrated resistor. Any LEDs will do. One is for power indicator and one is for reset indicator.
    • light dependent resistor (something like $0.45 from Newark or get a 5-pack for $2.99 from Radioshack)
    • pin header to make our ISP header (you want a really long tail or mounting pin. i pick mine up from a local shop. this is the closest i could find online ($2.50 from
    • breadboard ($10.71 single strip from mouser or a 4-strip one with jumper wires for ~ $16.00 on )
    • wire, wire cutters, needle-nose pliars, and lots of Cheez-it’s


Serial Communication

I suggest option 1 because it’s more than just serial communication. You can use it to program an AVR if it has a bootloader on it. It’s also cleaner and requires fewer external components *and* connects via USB.
Option 1 – USB

    • USB-BUB ($12.00 or $13.50 with mini-USB cable from
    • right angle 6-position male pin header (40-pin for $1.81 at Mouser)

Option 2 – RS232

    • max232n dual RS232 receiver/driver ($0.81 from mouser)
    • 5 x 1uF capacitors ( $0.16/ea for 5mm spacing here or $0.76/ea for 2.54mm spacing here)
    • DE9 d-sub female connector ($3.48 from Mouser or $2.19 at Radioshack)
    • wire, solder, soldering iron, maybe heat-shrink tubing if you’re feeling fancy.



  • USBtiny ($22.00 from Adafruit (you have to build it yourself but has 6-pin and 10-pin connectivity)

— or —

    • AVRISP for around $27.00 on This option has better support if you use AVR Studio, but only comes with a 10-pin connector.



You have several options here. If you already have a 5V regulated power supply, then you’ll just run +5v and ground from it. If you have no power setup already then you’ll want to build one. You can put it on a breadboard or solder one together. If you get a big breadboard setup (ie not just one strip, but something like the 4-strip one I linked above) then you can just build it on your breadboard. Either way, if you don’t have any regulated power and need it (if you don’t have it, IMHO, you need it), I’ll show you how to build a variable-voltage regulator, not just a 5v regulated power supply. This way you can change the voltage from, say, 3V all the way up to 9V. This gives you more latitude on the things you can power (I used mine yesterday to recharge an old cordless phone that has a lithium/ion battery pack that I wanted to pull out and use in another project). You’ll need the following items:

    • LM317 3-position variable regulator ($0.57 from
    • 0.1uF tantalum capacitor (see link above for link)
    • 1uF polarized capacitor (tantalum or electrolytic)
    • 1k Ohm resistor (see above for link)
    • 220 Ohm resistor ($0.19 from mouser)
    • diode (either like the one I linked for you above, or you can use a smaller one. In my setup, I used a smaller switching diode because I had a strip of them. I’d probably go with a 1N400* one if I built another one, though. You can get a 25-pack of the 1N4* for $2.79 from [Radioshack] or a 50-pack of the smaller, switching ones for $2.59 from Radioshack.)
    • 10k Ohm Cermet potentiometer/trimmer. The cermet gives you finer control of the power you output, down to about 0.00 accuracy. I got one for $2.69 from Radioshack but if you’re ordering a lot of things from you can do a search for one there, if you like.
    • an LED for “power on” indicator is nice. Pick a color. 3mm is a good size. Hell, while you’re at it pick up a 20-pack of assorted LED‘s for $2.99 at Radioshack.
    • power jack. This will depend on the type of wall-wart power supply you have access to. I literally have a box full of different sizes and power ranges from various assorted electronic things that have gone the way of the dinosaur but I kept their power supply. I’ve found the most popular is 2.1mm. You can go for an on-board jack if you’re soldering (the pins won’t fit in a breadboard) like here for $0.63 cents. If you plan to put everything on your board, then I’d suggest going with something like this from Radioshack although it’s more expensive: $2.99 since it’s not right angle and you can solder up some wires to it and wrap it in tape. Ghetto, I know.


Development and Programming Software

You’ll need something to write software on, if you’re planning on building Mastermind (or anything else) from source. On Windows there’s AVR Studio from Atmel and WinAVR. I’d say WinAVR is a must, as it already includes AVR GCC, avrdude (the programming software), and several other utilities like make, etc. If you’re on a FreeBSD system (or Linux), then you probably already have avr-gcc, avr-binutils, etc. See one of my other instructables for instructions on downloading and installing it for FreeBSD/Linux if you need help.
Mastermind should compile in either AVR Studio, WinAVR, or pure avr-gcc. I use both on Windows and avr-gcc on FreeBSD, but wrote it in Programmer’s Notepad under WinAVR. You’ll be using avrdude to configure the fuses on your new AVR development and Mastermind system.
That should be everything you need to build a stand-alone, power regulated, external crystal 20MHz AVR system complete with 6-pin ISP programmer and inductive-filtered analog voltage. Ready to get building?

Step 2: Base 20MHz AVR System

We’ll start with the base AVR environment. The design is based off of what is suggested in AVR042: Hardware Design Considerations and the ATmega328P datasheet (448 pages!). Read them, learn them, love them.
Note the ATmega328P pinout. Fresh IC’s come with their legs slightly bowed out. Lay one side of your AVR flat against the table, grab both ends and gently rock it forward to bend the pins straight. Put it on your breadboard straddling the fuller or gap running down the middle of each strip. Note where pin 1 is. It is represented by the dot next to the dip cut out in one end of the IC.
First, let’s put in the 20MHz crystal and its capacitors. I’ve already determined what sized capacitors to use, so if knowing how to determine them doesn’t interest you and you just want to build it, skip down a bit. A crystal’s capacitors should most always be symmetric and of the correct capacitance to ensure a high enough capacitive load to stably oscillate and low enough to keep it from starting to oscillate. To determine this we use the equation:
CL = (C’L1 * C’L2) / (C’L1 + C’L2)
C’L is capacitive load and
C’Ln = CLn + CLns and CLns is stray capacitance.
This is slightly complicated because when we configure our AVR to use an external full-swing crystal, it toggles it’s own internal capacitors on the XTAL lines. The design note suggests capacitors in the value of 22-33pF assuming a Cs between 5-10pF. I’ve tried 27pF and 33pF and both work fine. I use 27pF here, but I’ve gotten great results with 18pF capacitors and you’re welcome to try different values.
Connect your crystal to pins 9 and 10 (XTAL1 and XTAL2) of your ATmega328. Connect a 27pF capacitor to each pin on the other side (the crystal is between the capacitor and the AVR) of the crystal and pull them to ground. See the picture below if that’s unclear. We’ll have to configure the AVR to use our external crystal, but for now we’ll just keep that in the back of our collective heads.
Next, connect all the power connections except for primary Vcc. That means pulling pins 8 and 22 to ground. Now is a good time to connect our analog Vcc too. We’re supplying AVcc with a separate decoupled power voltage to further isolate it from noise caused from digital switching by using a 10uH inductor and a 0.1uF capacitor for power filtering.
Place the inductor at a convenient spot on the same side as pin 20, AVcc. Pull one side up to +5V by connecting it to the positive power rail. Connect the positive side of a 0.1uF tantalum capacitor to the other side of the inductor and connect the negative side of the capacitor to ground. Then connect a line from the positive side of the capacitor to the AVcc pin. See the picture for details. We’ll leave the digital Vcc line for later when we build the regulated power source and then decouple it separately.
Congratulations, you’re about halfway done with the base setup.
For more details, click: Build a Complete AVR System and Play Mastermind Using Micro-controllers

About The Author

Ibrar Ayyub

Ibrar Ayyub is an experienced technical writer with a Master's degree in computer science from BZU Multan University. He has written for various industries, mainly home automation, and engineering. He has a clear and simple writing style and is skilled in using infographics and diagrams. He is a great researcher and is able to present information in a well-organized and logical manner.

Scroll to Top