Build a Complete AVR System and Play Mastermind!




Introduction: Build a Complete AVR System and Play Mastermind!

About: Gian is a computational biologist and is the Managing Director at Open Design Strategies, LLC. He holds a BA in Molecular/Cellular Biology and an MS in Computer Science. He has a collection of 8-bit microcont…
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.

Step 3: ISP Header and Reset Circuit

Take your pin header and cut off two strips of three pins each. I found some really long pin headers at a local electronics shop so was really lucky for this. The pin headers I list in the parts section are the longest I could find online, but aren't as long as these, so you may have to push the pin guides down to get enough room.

Make the ISP header
Take a pair of needle-nose pliers and make a bend outwards close to one side of the guide. Then, make another bend back inwards about 3 or 4mm down from the first bend. Check out the pictures to get an idea of what it should look like. Do the same for the other set.

Place each set on either side of the breadboard fuller/center gap such that they bend inward toward each other. The top of the pins should look like a 2x6 header when you look straight down on it. Don't fret too much about getting them perfectly aligned. They'll be fine once they're inside the ISP programmer's 6-pin connector.

Connect the ISP header pins to the AVR
Check out the pinout of what an ISP header looks like from Atmel's AVR042. Pin 1 will be the top right pin as you're looking straight down on it. Make the ISP connections:

  • Connect MISO to pin 18 on the AVR.
  • Connect SCK to pin 19 on the AVR
  • Connect RST to pin 1 on the AVR
  • Connect VTG to a +5V rail
  • Connect MOSI to pin 17 on the AVR
  • connect GND to the ground rail

See below for how this should look.

Establish Reset Circuitry
We want to create a circuit that allows us to push a momentary switch and have the AVR reset itself. This allows the AVR to reload its firmware.

Place the tactile switch so that it straddles the middle gap just like the AVR and ISP header does. You may need to use a multimeter to check which pins are connected when you press the button. On most tactile switches, they are paired across the gap where the pins curve inward. So, if you don't have a multimeter, just place the switch with the curve of the pins straddling the gap and test empirically later.

Once you've placed the switch, run a line from the top left pin to pin 1 RESET on the AVR. Then place one side of a 10k Ohm resistor from that same pin on the switch and place the other side of the resistor in a +5V rail. This acts as a pull-up. On the other side of the gap and on the other side of the switch, place the positive end (the longer pin) of a 3mm red LED and run the other side to ground. You'd normally place a resistor in between but with such a short burst I've never had to. The picture below shows what the reset circuit should look like.

If we already had a regulated power source, we could decouple it and power up the AVR and start programming it now. Pretty simple, huh. Once you get the practice of these previous couple of steps, you can throw together an AVR development system quick snap, and this includes precautions like decoupling both digital and analog power sources, an ISP programming header, external crystal, and a reset switch. You only need to make the power source once so the next step is unnecessary in the future after you've already built it.

Let's make our regulated power source so we can power up our AVR and start the fun stuff.

Step 4: Regulated Variable-Voltage Power Supply

The power supply needs to take a 9V or 12V source from a wall-wart transformer and produce a safe, steady current at our specified voltage. The circuit we're going to build will provide around 2.5V to 8.5V with a very fine level of control. I've gotten a granularity of two significant figures of voltage.

Since I've already built this and went ahead and soldered a small board with all the pieces on it, as to clear up limited space on my breadboards, I'll lay down most of the pieces on the breadboard then show how it would look once it's soldered into a permanent circuit. Feel free to keep yours on your breadboard, but as you start making more projects, and especially if you've always got multiple projects in various stages of completion, like me, then breadboard space is valuable real estate, and it makes sense to solder a more permanent solution. Besides, most DC power jacks don't fit onto breadboards. Ok, enough talk, let's build!

The schematic shows the regulator that we are going to build. I've built several of these and the design is pretty solid. Start off placing the LM317. The pins as you're looking at the front (the black box) is AOI (Adjust Out In). Figure out where you're going to have the unregulated 9V and ground from the power jack comes in. Place a 0.1uF tantalum capacitor between the two lines, keeping in mind the polarity of the capacitor. Do the same with a 3mm LED from the positive line, followed by a 1k ohm resitor connecting the negative side of the LED to the ground line. This will provide an indicator that there is power flowing into the regulator. It may seem redundant to have an LED on here when we're going to put another on the AVR Vcc line, but we're doing this with the thought that the voltage regulator will ultimately be on a separate board, and the indicator light will go with it.

Run your positive line to behind the LM317 to the In pin (far right). In front of the LM17 attach the diode from out (middle pin) to in (far right). Next, attach the 220 Ohm resistor from out to adjust (far left). Check out the pictures.

The cermet trimmer comes next, but there are no pics of it on the breadboard. The picture with the capacitor is the final piece and the cermet would be sitting between the red and black wires and the capacitor. Fret not, the next steps show how to move it off your breadboard and onto a permanent board so you can use it with all your projects that need a regulated power source.

Get a small proto board or cut a square from a large perf board like I did. Lay out the components on the board in the same order that you put them on your breadboard: 0.1uF, led->resistor behind the LM317. Diode and resistor in front of the LM317, the cermet trimmer, then the 1uF capacitor. Drill holes on both sides for stress-relieving wire connections. You can throw on a big honkin' heat shield on the LM317 but you won't need one. I've ran my regulator for days and it didn't even get warm.

I use a 2.1mm jack as most of my wall plug-in power supplies are that size. Wire it up as shown below making sure you have the positive and negative correct. You can plug it into your wall jack and measure the voltage with a multimeter to ensure you have the correct pins if you're not certain. Solder the wires to the jack and seal them with heat-shrink tubing or wrap them with electrical tape. Run them up through your holes you drilled and back down into the board. This helps relieve stress on the soldered connections when you tug and pull on it. Do the same on the opposite side for the regulated power leaving the board.

You now have a working regulated variable voltage power source. All you have to do is connect it to your wall unit and your breadboard and you are in business. Make sure you hook up a multimeter to the output and measure it. Turn the trimmer screw until you get 5V +/- 0.05V. Keep it on for a bit and double check that the voltage is smooth and steady.

How do we celebrate such a momentous occasion? By powering up our AVR and configuring it! Man, the excitement just keeps building, doesn't it?

Step 5: Configure Fuses on the AVR

The first order of business is to run our regulated power supply into our AVR. Even though it's regulated, we're still doing to decouple it. This is important because current is drawn in spikes on the clock edges, and the spikes are even larger concomitant with the number of I/O lines that are switching. This means the spikes can draw several hundred mA and even in short bursts this is significant. We mitigate this by the use of decoupling capacitors that will provide the source for the spiked current draws. This helps ensure a smooth power supply and all the current a hard-working AVR needs.

Despite the critical importance of decoupling, the setup is very simple and straightforward. Here's how we're going to do it. We're going to put two capacitors between the regulated power source and a voltage clamp diode. Along with that, we're going to add an LED with associated resistor as a power indicator. You'll be done in about 30 seconds.

Make your decoupling circuit

Attach your shiny, brand-new regulated power source to your breadboard. I connect via power at the top mounting posts. Check out the voltage regulator in action. You want your decoupling capacitors as close to your Vcc as possible, so I moved my ATmega328P over a few holes, along with the wires, and brought my decoupling circuit up from the bottom left. Run positive on the left, negative on the right and place a 47uF electrolytic capacitor from positive to ground, followed by a 0.1uF tantalum capacitor, also from positive to ground. After that, place a 1N4002 diode to clamp, noting the direction (it's polarized). Finally, place a "power on" LED with it's requisite 1K Ohm resistor and that's the decoupling circuit. Check out the pictures.

Run a wire from the positive end of the decoupling circuit to pin 7 Vcc of the AVR and you are DONE! You have a pretty complete, feature rich AVR development environment.

So how about we christen our brand new AVR by configuring it?

Configuring your new AVR

The primary thing we need to configure are the fuse settings to indicate to the AVR that we want to use an external crystal. Along with this, we'll configure a few settings at the same time that specifies clock speed, startup time, and brown-out detection.

Connect your programmer to the ISP header. I use a USBTiny from Adafruit Industries. Note the way it connects. Make sure you mate the female connector to the header in the correct orientation.

Fire up avrdude and issue the following command:
avrdude -c usbtiny -p atmega328p -F -t
You should be greeted with a prompt as shown below. Click the [i] in the top left corner of the picture and select the original size if you find the picture difficult to read.

Next issue the "part" command to get some device information, followed by the following command:
read lfuse
This reads the lower fuse settings, which we are interested in, as it contains clock settings. Remember that 400-some page datasheet I linked earlier? In it, you'll find the settings for the lower fuse in Table 27-9. The eight bits in it are:
Fuse settings are peculiar, as a 1 means unprogrammed and a 0 indicates programmed. CKDIV8 comes programmed from the factory and divides the clock by 8. The internal RC oscillator is programmed by default at 8MHz and with CKDIV8 programmed, that sets the clock speed at 1MHz. We're going to change that.

The whole lower fuse register is one byte and what we are going to do is set:
  • CKDIV 1
  • CKOUT unchanged
  • SUT1 0
  • SUT2 1
  • CKSEL3 0
  • CKSEL2 1
  • CKSEL1 1
  • CKSEL0 1

The lower fuse comes programmed from the factory with a setting of 0x62. To make our changes we need to convert the hex to binary, then back to hex to make our change. 0x62 in binary is 0110 0010. Based on the changes I mentioned above, we want that register to contain 1101 0111 which is 0xD7 in hex. Here's how that looks:
      [ CKDIV8 | CKOUT | SUT1 | SUT0 | CKSEL3 | CKSEL2 | CKSEL1 | CKSEL0 ]0x62       0       1       1      0      0        0        1        00xD7       1       1       0      1      0        1        1        1

Fire avrdude back up and issue: avrdude -c usbtiny -p atmega328p -F -t
At the prompt type
write lfuse 0 0xD7
It should give you a message about writing the fuse. Issue "read lfuse" to read the fuse and verify that it wrote 0xD7. Once complete, type "quit" to leave.

Your AVR is now programmed to run at a blazing 20MHz with a quick startup time and brown-out detection enabled. You're encouraged to read the section on lower fuse settings if any of that is confusing.

At this point, you are able to program your AVR to your hearts content. You can hook up LED's and blink them, read DIP switches, drive motors, write to LCD's, read external memories, and more. But you don't have any way to capture output on a grand scale. That's the next order of business.

Step 6: USB Serial Communication

The USB-BUB from is my preferred method of connecting an AVR to the outside world. An AVR with a hardware UART/USART makes communication a snap, and even those ATtiny's without a hardware UART can still communicate if you write a software driver, but that's another instructable. ;-) The USB-BUB allows you to connect, obviously, via USB and on newer computers that don't have DE9 RS232 ports, this is great. I still connect to my FreeBSD machine via DE9 RS232, so I'll show you how to set that up in the next step if that's your flavor.

The ATmega328P has one programmable serial USART. What we're going to do is connect the USB-BUB to our AVR. This will allow us to use standard USART routines to handle all of our clocking and timing needs and allow us to communicate to an external host, such as your FreeBSD box, Windows, or Mac machine via a serial terminal application. This is great for debugging and essential for interactivity with your AVR, as we will soon see as your AVR challenges you to a duel in logic in my Mastermind game.

The USB-BUB has six connections:
  • DTR
  • RX
  • TX
  • 5V
  • 3.3V
  • Ground

The DTR line is used for asserting a RESET when programming via the USB-BUB. That's right, if your AVR has an Arduino-compatible bootloader, you can program directly via the USB-BUB. I use it for that purpose on my Bare Bones Board where I have an ATmega328P sitting with a bootloader installed. You have probably seen it if you've read any of my other instructables.

RX, TX, 5V, and Ground, I believe, are self-explanatory. Because the FTDI chip that is used in the USB-BUB has a 3.3V regulator, it also puts out 3.3V on the 3.3V pin, but you have to jumper it to get it. I have it configured that way so that I can offer a separate 3.3V supply to devices that have different power requirements, such as some of my SPI SRAM memory chips. How convenient is that?! You can also use the 5V from the USB-BUB to power your board if your regulated power supply is in use on another board; just move your decoupling circuit between the USB-BUB's 5V and ground pins. I've shown how I've done that on a different breadboard that I'm using with an external SRAM chip, if that's of any future interest. The picture has a lot of stuff cramped into a small area, but the decoupling circuit should be familiar and noticeable.

In order to use the USB-BUB for serial communication we need to make the connections, along with a couple of components in between. If you have your 6-pin right angle male header place it on your board somewhere convenient. If you have a right angle breakaway strip, break off 6 pins and then place it. I've shown where I've placed mine below. The standard right angle headers can be a bear to stay in place in a breadboard sometimes. I normally use a modified version with longer tail ends. It has 7 pins, but hey who cares. I'm too lazy to snip off the seventh pin anyway.

Connect the USB BUB

On the pin that will be DTR, connect a 0.1uF capacitor negative end first and connect the positive side via a wire to the RST pin of the ISP header. Refer back to the pinout if you've forgotten or figure it out from the pictures below. Connect pin 3, TXD, to the RX pin of the USB-BUB. Remember, your AVR's TRANSMIT pin is the RECEIVE pin of the remote unit. for the next pin on the USB-BUB, TX, you'll want to connect a 10k Ohm resistor followed by a wire to the RXD pin (pin 2) of the AVR. Connect 5V to your +5V power rail, skip the 3.3V pin, and connect the ground pin to your ground rail and you're done.

You can skip the next step and go straight to installing, connecting, and playing Mastermind, unless you have some driving curiosity to find out how to configure a max232 connection.

Step 7: DE9 RS232 Serial Communication Setup

So, you've got a 9-pin D-sub serial port and want to connect it to your AVR development environment? Old school, but no sweat. We'll use a max232 chip to level shift the voltage from what's used on an RS232 line to a TTL level that won't fry our AVR.

Make your DE9 Connector

Grab your solder-cup female DE9 (I don't like to get into the DB9 vs DE9 debate, but formally it's a D-sub 9-pin connection in an E box, or so I've been taught) and solder one wire to pin2 (TX), one to pin 3 (RX), and one to pin 5 (Ground). If you're unsure of which pins are which, look very closely into the connector and you should see small numbers above/around the holes. If they're not there, or you're still confused, check out this site for some pinout diagrams.

The wires should be long enough to connect into your breadboard and either your RS232 port on your remote machine or an RS232 cable coming from it. I have, like, a 15 meter RS232 cable running from a serial port on a FreeBSD box so my wires only have to reach shortly beyond my breadboard. Make it long enough to be convenient.

Once you've got your connector soldered up wrap it up or throw on some heat-shrink tubing. I the pictures below I soldered in a few more lines for something else, so don't worry about having those wires connected. Also, note, that if you've already got a male RS232 lying around you can use that, you'll just probably need a gender changer. Just remember that the pins will be flipped on the male that what I've shown you on the female. I've added a couple other pics of a male DE9 with the same colored wires that I used in the female connector.

Connect the max232

We're going to use 5 x 1uF tantalum capacitors in this circuit. Place your max232 on your board in a convenient location when you can eventually run your RX/TX lines from your AVR to it along with the RS232 cable you just made.

Capacitor #
1. Connect positive to pin 1 and negative to pin 3
2. Connect positive to pin 4 and negative to pin 5
3. Connect positive to ground and negative to pin 6
4. Connect positive to pin 2 and negative to ground
5. Run +5V to pin 16 (top right) and in between place a capacitor's positive side and negative to ground.

Connect ground to pin 15. At pin 14 connect RX (red wire in picture) and at pin 13 connect TX (green wire in picture). Run your ground wire to ground (the black wire). Your setup should look similar to the picture below. Again, ignore the yellow and blue wires. You won't be using those.

Finally, connect PD0/pin2/RX to pin 12 of the max232, and PD1/pin3/TX to pin 11 of the max232. Et voila, nous avon finit! Now you have your AVR connected to the MAX and the MAX connected to the RS232 port via your cable. You're ready to start serial communication.

Step 8: Mastermind

Now we're ready to load up our Mastermind firmware and accept the AVR challenge!

Install the Firmware

Download the zip package and unpack it. Inside you'll find the complete source code for the Mastermind firmware along with two pre-compiled hex files: one for an Arduino and one for the AVR setup you just made.

You can upload the pre-compiled hex file with the command
avrdude -c usbtiny -p atmega328p -B 1 -F -U flash:w:Mastermind-57600.hex
or just copy Makefile.usbtiny to Makefile and type "make program."

If you're feeling frisky, you can compile the firmware by by copying Makefile.usbtiny (or Makefile.arduino) to Makefile and typing "make" followed by "make program," to upload the hex file.
You may have to change the com port setting in the Makefile or the AVRDUDE programmer setting to your appropriate programmer if you're using something other than a USBTiny.

Install AVR Terminal

After going through the work of installing the USB-BUB or the max232 serial connection, you'll want a terminal program that you can connect with. I've used several, including putty term, teraterm and hyperterm on windows and minicom and screen on FreeBSD. There's also developer's terminal from Smiley's micro's. They've never really met all my terminal needs for my programs, so I wrote one over the course of a weekend. On FreeBSD/Linux I still use minicom, but on Windows I use AVR Terminal pretty exclusively now. AVR Terminal is still alpha, doesn't have the entire v1.0 feature set completed yet, and probably has some bugs hanging around in it, but it's worked pretty well for me. Plus, it understands custom input and color formatting. Your mileage, however, may vary. So, I can't guarantee there aren't a bug or two hanging out in it, but I can guarantee it doesn't have spyware, backdoors, trojans, viruses, or other malware.

For Mastermind, your primary concern in choosing a serial terminal is ensuring it is using a fixed width/monospace font as the output is formatted and depends on it. Otherwise, you'll get unpleasant formatting ugliness. I'm making AVR Terminal available for playing Mastermind with, but if you like it and want to use it for future serial connectivity, feel free to drop me a line and let me know and please check the primary download site for updates as I'm continually working on it as my terminal capability needs change. If enough people want to use it, I'll move it up on my priority list.

Install your Light Determining Resistor

Mastermind uses values sampled from an LDR on the analog reference pin to randomize it's color choices. The game will work without it, but I think it's randomization is better with it. To do this, connect one pin of your LDR to ground and one pin to pin 21 (AVcc) on your AVR. Connect a resistor from AVcc to +5V. You want a resistor value that won't short the line if you get too much light or darkness. That's it. Your AVR will now use a value sampled from ambient light when you start up the game.

See if You Can Beat Your 8-bit Opponent

Start your serial terminal and connect at 57600 on whatever com port you've got it on. Reset your AVR and you should see the intro screen come up. Answer the question about the LDR, the new game (that's kind of a weird question since it's in a loop and it's not like it can shut off, but it's in there for the logic I need for the hardware version I'm building), and who you'd like to be the game master. Currently, only the MCU can be the game master. Again, that's in there for future use.

Type all your commands in lower case.


Step 9: Summary

I hope I've been able to help you in creating a very quick yet pretty complete AVR development environment taking into account the Atmel AVR Hardware Design Considerations application notes. If you do this a couple of times, you can build an Atmel-approved development environment literally in minutes. It doesn't take much to go that extra bit to get the decoupling, filtering, reset switch, ISP header, etc that a system really should have. There may be more that you think could be added to improve the robustness of the system, and I encourage you to redesign the system to support whatever you think it should have.

Further, I've shared my Mastermind game with you and I hope that along with providing some general fun, that the code may provide some insight or inspiration to help you in your coding endeavors. The AVR Terminal application is written in C# and is provided as a serial terminal substitute for Windows-based PC's that want to connect to AVR's for intput/output. I only initially created it for private use, but I'm making it available here if there's an interest in using, and especially because it looks great with the Mastermind game.

I hope you enjoyed this instructable, and as always, I'm always glad to hear any comments or suggestions!

Happy AVR'ing,

Be the First to Share


    • Make It Modular: Student Design Challenge

      Make It Modular: Student Design Challenge
    • Tinkercad to Fusion 360 Challenge

      Tinkercad to Fusion 360 Challenge
    • Digital Fabrication Student Design Challenge

      Digital Fabrication Student Design Challenge


    Thank you for making this 'idible! Helped solve a wiring issue for a Rocstoc! Great referencing your sources :D


    11 years ago on Step 6

    Thanks for putting this together. This is a great reference! You mention that it's possible to program an AVR using the USB-BUB. Can you describe how one would go about doing that?


    11 years ago on Step 5

    Thank you so very much for the tip on the AVR clock select fuses. :)


    Reply 11 years ago on Step 5

    Thanks, Glad I could help! :)


    12 years ago on Introduction


    Your project seems very cool... How the user interacts?? How he plays?

    I have a KIT Workshop - Base level with Arduino Duemilanove x4, it´s a good ideia to use it tp build the game?



    12 years ago on Step 5

    If, say, a noob who jumped in too quickly with VMware and AVRStudio were to, say, set the fuses improperly on one of his 328Ps and that theoretical 328P stopped paying attention to calls from the noob's AVR ISP MkII, how might you recover that chip from its apparent "Shaun of the Dead" state?

    On a side note... Thanks to your incredible Instructible, I now have a very slick little 20 MHz ATmega328P running on my breadboard with die BlinkenLEDs! Thanks!


    Reply 12 years ago on Step 5

     Hi Penguins,
    Thanks for the kind words!  Well, hypothetically, if one were tohave a "Shaun of the Dead" AVR (which of course, I would onlyknow of hypothetically), one could reset it via a process called High Voltage Programming. It will reprogram and reset your AVR to default settings. This circuit comes wired on AVR development boards from Atmel, butI did find a way to do this by building the circuit myself when I, um,hypothetically needed to do this. 

    Check out for a step-by-step instruction on setting it up.  Out ofthree fried AVR's, I was able to recover one.  This is probably dueto the fact that the other two were truly fried and not just fubar'dwith the fuses.  Hypothetically, of course.

    There's also a schematic floating around (and I think a complete kit) onmaking a high voltage programmer shield for an Arduino.

    Good luck (hypothetically)!  :)


    Reply 12 years ago on Step 5

    Thanks, nevdull (hypothetically)!

    If this were to have really happened, it would have been the fuses thatwere borked. ;)


    Reply 12 years ago on Step 5

     Hehe :)  If you're running with BOD make sure you have yourBOD fuses set how you need/want them.  I'm iffy with Brown-OutDetection, but I bricked one of my AVR's when myBOD threshold was too high (or something like that or maybe ileft them was a long time ago).  Check table 28-4in the Holy Book ( for BODLEVEL voltages if you've got BOD enabled.


    12 years ago on Introduction

    I wonder if this could be made into a handheld unit using RGB LEDs to display the "pegs?" It would be hard to display all the rows you've already guessed, though. That would be a lot of LEDs. Hmmm...


    Reply 12 years ago on Introduction

    haha yah i actually mention that on one of the pages in this instructable. I wrote this firmware on the way to building the hardware version. i bought 50 RGB LEDs on ebay for $20 and just waiting for them to get in...come to think of it, I may have to hit you up on how to drive the LED's efficiently...8 rows, 4 per row, a row of 8 with each color (to show the colors to chose from) at the bottom with tactile buttons beneath each color, and then 4 small 3mm LEDs to the far right per row to show the "color correct" and "place correct" numbers. I could only come up with doing it in binary, though, if that makes sense:

    (LED) (LED) (LED) (LED) o ooo

    Hard to format it in this space, but the four little LED's would be 2x2, with the first column = color correct and 2nd column = place correct. and they would light up in binary.

    (off)  (on)  (off)  (on)(off)  (off)  (on)  (on)
    to represent 0, 1, 2, 3, and both on blinking = 4. I couldn't think of another way to do this and not take up a lot of space. I'd definitely be open to any suggestions you might have since you're the LED/LASER king :)


    Reply 12 years ago on Introduction

    The best way is using a serial LED driver. I'll have to dig up some part numbers, but I think that Maxim IC makes one that would be suitable. That would work for the main bank of LEDs. To indicate whether the colour selection is correct, you could make the LEDs blink or otherwise change, instead of using a separate LED. Some drivers are even capable of PWM fading... At the bottom, where the user selects the colours for the next guess, the button for each row would just scroll through the available colours. There's no need to indicate which ones are available (IMO).


    Reply 12 years ago on Introduction

    I think I see what you mean...have only four colors at the bottom (one for each slot) with buttons underneath them and the button would cycle the color, then there would be a separate button (or something) to indicate that you're done analyzing/picking colors and are ready to submit?

    Re: correct color selection, it would have to
    a) be able to indicate if the color was correct, but place was not
    b) if the place and color was correct
    c) keep the original chosen colors in the row because the logic of the game depends on them. That is, the player will have to keep looking up to their previously chosen colors and to the "color/place correct" indicators for their logic to be sound.

    I'm not sure if those conditions could be met by indicating them in the row itself (although I've only thought about it since you mentioned it).

    Check out here for an online java version. They use black/white pegs to indicate color/place correct, but I didn't like the idea of "off" being a color but I could probably use a 2-color 3mm LED.

    I'll check out the max chip...that seems vaguely familiar..i've taken a look through a lot of their catalog of chips and think I may have seen something like a driver (or does it sink?) in there.


    Reply 12 years ago on Introduction

    Ah, I see. My memory was foggy and I forgot that it doesn't tell you individually which peg is correct. Well, you could still use the LEDs in the row as a display, but I think it would get confusing to look at once you have a few rows filled up! Hmmm, there must be a way to do it without adding another 32 LEDs... And yes, a separate button would act as a submit button when you're done.


    Reply 12 years ago on Introduction

    Yah, I agree; it might start looking very busy, but I did get the diffused RGB's since I always thought they were easier on the eyes and their colors were more easily discernible. Oh, and look what I *just* got from my mailbox in between our conversations...


    Reply 12 years ago on Introduction

    Nice! I happen to have exactly the same package myself (from the same seller!), purchased for a project that never came to fruition.


    Reply 12 years ago on Introduction

    LOL small world! At least you have a bunch of RGB LEDs lying about to use when the whim strikes you tho! It took a looong time getting here from Hong Kong, but well worth the wait for the price!


    Reply 12 years ago on Introduction

    Yeah, they take quite a while to arrive but the price is right. I'm not sure what I'll make out of them now. I'll wait for inspiration to strike.