Build a Complete AVR System and Play Mastermind using ATmega328p microcontroller
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 Mouser.com) [you can substitute an ATmega168, ATmega32, or any other AVR that has ~ 10k flash memory]
- 2 x 27pF capacitors ( $0.23 each from mouser.com)
- 3 x 0.1uF tantalum capacitors ( $0.40/ea from mouser.com )
- 47uF electrolytic capacitor ( $0.18 from mouser.com )
- 20 MHz crystal 20pF capacitive load ( $0.57 from mouser.com [you can use 16MHz or other AT-cut crystal]
- 2 x 10k ohm resistors ( $0.12/ea from mouser.com)
- 1 x 1k ohm resistor ($0.12 from mouser.com)
- 1N4002 diode ($0.11 from mouser.com
- 10uH inductor ($0.59 from mouser.com or a smaller sized one if handy)
- tactile switch ($0.26 from mouser.com)
- 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 mouser.com)
- breadboard ($10.71 single strip from mouser or a 4-strip one with jumper wires for ~ $16.00 on Ebay.com )
- wire, wire cutters, needle-nose pliars, and lots of Cheez-it’s
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
Option 2 – RS232
- 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 Ebay.com. 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 mouser.com
- 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 Mouser.com 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 2×6 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?
Leave a Comment
You must be logged in to post a comment.