Introduction: MicroSimon

This project shows how to create an MB Electronics Simon game clone using an 8-pin PIC12F683 microcontroller. The game includes a full emulation of the original Simon 'game 1' and the ability to select from 4 skill levels which control the number of colours you must repeat in a sequence in order to win the game.

The project was created for Sparkfun's microcontroller competition 2011 and aims to demonstrate an number of useful design techniques which can be used when working with low-pin count microcontrollers. The code is written entirely in C and fits neatly into the 2K of available program flash on the chip. The PCB is only 1.5 inches square and uses both SMD and through-hole components, however it is perfectly possible to recreate the project using larger components on a breadboard or a small piece of strip-board.

The game is powered by a standard CR2032 3V lithium cell which is mounted on the underside of the game. The design uses the ultra low power standby feature of the microcontroller meaning that no power switch is required. Overall power consumption is kept to a minimum by using the internal oscillator of the PIC running at 4Mhz.

You can see a video of it in action over on youtube

Step 1: Parts List


The MicroSimon hardware consists of the following parts:

* 1 x PIC12F683 SOIC 8-pin microcontroller
* 5 x 10K resistors (1206 SMD)
* 1 x BC817 NPN Transistor
* 1 x 100nF capacitor (0805 SMD)
* 4 x 3mm LEDs
* 1 x CR2032 battery holder
* 5 x Tactile push-button switches
* 1 x QMX-05 speaker
* 1 x 6 pin 2.54" right angled header

Step 2: Circuit Schematic and PCB

The Eagle CAD schematics and board files are available for download with the project files. The board is a small dual-layer PCB, all components are mounted top-side apart from the battery holder which is mounted on the bottom of the board to save space. I tried to make the board as small as possible whilst still being playable (I have big thumbs!). The in-circuit programming header can be omitted if not required to make the hardware neater looking. The PCB was made using the UV photo-resist technique which allows for good track accuracy even from a hobbyist set up.   Below you can see a picture of both the schematic and the board artwork:

Step 3: 4 Switches on 1 ADC Pin

The hardware design uses a common microcontroller trick of using a single ADC (Analogue to Digital Converter) pin to connect the 4 colour buttons of the game. Each switch is connected to the controller through a single 'pull-down' resistor to earth which acts as the lower half of the voltage divider. The other half of the voltage divider is created using a network of 10K resistors on the other side of the push-buttons. Depending on what button you press you connect either 0 Ohms, 10K Ohms, 20K Ohms or 30K Ohms to the top half of the voltage divider. Using some simple ohms-law maths you can then work out the voltage for any given switch as shown in the diagram below:

Step 4: Charlieplexing the LEDs

The LEDs are connected to the PIC using 3 pins. This is achieved by using 'charlieplexing' which takes advantage of the fact that an LED is a diode and only allows power to flow in one direction. The PIC's IO pins are 'tri-state' meaning they can be 0V (sinking power to ground), 3V (sourcing power from Vdd) or 'high-impedance' inputs (high impedance is a fancy expression meaning that the pin is effectively disconnected and virtually no power flows through it). Using 3 pins it is possible to control up to 6 LEDs, however the game only requires 4 as shown in the following diagram:

Step 5: Hardware (the Rest Of...)

Instead of using a bulky toggle switch for the power control the hardware uses the low-power sleep mode available on the PIC. The 'start' button for the game is actually a MCLR reset button. When the PIC is reset the game starts, once the game is over the PIC enters low-power sleep. Pressing the reset button resets the controller and the game starts again. The power drain of the PIC in sleep mode is very low so it would take some considerable time for it to deplete the 3V battery.

The sound circuitry consists of a miniture QMX-05 speaker with a single NPN transistor which makes the sound louder and also avoids placing a high load on the microcontroller's output pin. It is possible to drive the speaker directly from the PIC without using the resistor or transistor, however this results in a much weaker sound. If you want to build the circuit using larger components the BC817 can be replaced with any compatible small-signal transistor such as a BC547 or BC337.

Step 6: Firmware Design

To make the game as simple as possible to understand and alter the firmware is written purely in C using the Hi-Tech C compiler. The PIC12F683 only has 2K of program flash which means you have to be as efficient as possible with the code to ensure it will fit in the target device. The code is divided into modules which each deal with the various hardware components to allow it to be easily understood and modified.

buttons.c:
This module is responsible for reading the button state via the PICs ADC. The ADC is read and the pressed button is determined using a small range of possible input voltage for each button. The code also provides button debounce on and off to ensure that the button action is 'positive' and noisy switches do not result in false readings (which would not be fun during your epic winning game!).

charlieplex.c :
This module provides the charlieplexed LED control which sets the PIC's port directions and power output in order to light the correct LED.

sleep.c:
This module contains the commands to put the PIC into low-power sleep mode.

sound.c:
This module contains functions which allow the sound to be turned on (at a specified frequency) and turned off. It also contains the interrupt service request (ISR) function which is called using a timer1 based interrupt and toggles the output pin on and off to generate the required sound.

simon.c:
This module contains the game's state-machine which is polled by the main looping function every 10 milliseconds. The game is implemented as a polled state-machine to allow accurate timing of the game whilst avoiding using interrupts (which would interfere with the sound generation since the PIC12F does not have multi level prioritised interrupts). The polling technique also allows the PIC to constantly read the game's button states and perform debounce without preventing the game from waiting for time-outs and user input during the game.

The game itself is based on the original game 1 from the MB Electronics Simon game. The game outputs an ever increasing number of colours in a sequence which you must repeat in order to win. The number of colours in the winning sequence is determined by the selected skill level.

main.c:
The main.c module initialises the PIC ready for use and then allows the user to select a required skill level before starting the game. Once the start button has been pressed the firmware 'chases' the LEDs to indicate it is waiting for skill level selection. The user selects skill level 1 to 4 by pressing the blue button for level 1, the red button for level 2, the green button for level 3 or the blue button for level 4.

The main module is also responsible for generating a random number which selects the next colour in the sequence. This is done using the simple technique of continuously incrementing a counter between 1 and 4 as the game poll loops. Since the game reads the counter based on when the user presses a button the result is a random number being picked every time. Since the time between pressing the reset button and the PIC being ready is constant, the initial skill level selection is used to get the random first colour for the game.

Step 7: Project Files

In the attached zip files you will find the PCB artwork and schematics in Eagle CAD format as well as the PIC12F683 firmware source code (for HiTech C) .

MB Electronics Simon is one of my favourite games and I love the idea of being able to have a truly pocket-sized version. As a next step I will probably build a small case for the game and mount in neatly to protect the electronics. I'd been thinking about building a version of the game using an 8-pin PIC for a while, so thank-you to Sparkfun's microcontroller competition for giving me the encouragement to get it built and working!

Comments

author
romainhuon514 made it! (author)2015-01-23

can you post a hex file for this? I am getting errors on your code and i'm still a newbie in coding or generating hex...

author
Grazfather made it! (author)2011-02-06

Nice. I like this a lot and I have a few 12f683's sitting around. Nice code, too.

One totally useless tip:

randomNumber++;
if (randomNumber == 5) randomNumber = 1;

could be

randomNumber &= 0b11;
randomNumber++;

to get rid of the branch, not that you have such a busy loop that you need to save instructions...

author
simoninns made it! (author)simoninns2011-02-08

@Grazfather - You could actually do it in one line using the mod operator, something like:

randomNumber = (randomNumber + 1) % 5;

But there is always the trade-off between code readability and memory efficient coding. I always go the 'readable' route unless I really need to save some bytes.

author
ishiyakazuo made it! (author)ishiyakazuo2014-01-08

Actually, you'd want
randomNumber = (randomNumber % 4) + 1;
The way you have it, it'll make a number between 0 and 4, but it looks like you're looking for a number between 1 and 4 in the original code.
Or you could do
randomNumber = (randomNumber & 0b11) + 1;
With the same effect of being on one line...

author
AJMansfield made it! (author)AJMansfield2013-09-17

However, you should not use a mod operator here, because depending on the compiler your mod operator could end up eating up a lot pf processing. On most processors, division (including modulo division) is about the very slowest single action you can have the processor take, and for a PIC, it might end up expanded into a fairly large subroutine call. (If you are paying a few hundred dollars for a proprietary compiler for embedded system development, then it might not matter, as the compiler will likely optimize it to the same thing Grazfather suggested)

author
ishiyakazuo made it! (author)2014-01-08

I like the resistor ladder for the buttons a lot.
I'm definitely going to use that in a project I'm working on, but with one small change: the top resistor I'm planning on making a 4.7k vs. all 10k. The reason is, it staggers the values to be away from binary midpoints so I can just look at the top few bits of the ADC. It'd put the values at approximately 68%, 40% and 28%, rather than 50%, 33% and 25%. Having 33% and 25% in there means I'd need to look at more bits of the ADC, but with these values, I can easily get away with looking at only the upper 3, as long as the resistors are within some reasonable tolerances (i.e. not 20% resistors). I really don't like 50% and 25% being in there, because it could be that you have 1000000000b or 0111111111b for 50%, and similar for 25%, and I'd rather just say "ignore the lower N bits, and if the upper bits match this value exactly, you have button press." :)
Anyway, just a thought. It really depends more about your code size vs. stock of resistors. Still can do the same thing with all 10k, just needs more bits and therefore more cases to handle.

author
gothen12345 made it! (author)2013-03-13

Simon, I need desperate help with a similar project using a 16F84. Unfortunately my programming skills are absolutely awful. I have the 4 inputs on PORTA and the 4 LEDs on PORTB but I can't for the life of me figure out how to program SIMON into the chip. I know the commands but figuring it out is intensely frustrating.

author
deattila made it! (author)2013-01-26

hello i have made your project but the programming is a little
problem, cause i use winpic wat for code do i need to use for
that program, i dont program for a long time so i hope you can help my

author
simoninns made it! (author)simoninns2013-01-26

You need to use MPLAB from Microchip to do the programming. You can download it (and the compiler) free from their website.

author
nodoubtman made it! (author)2012-05-30

Here's the version of Colin Mitchell i have realised : http://www.youtube.com/watch?v=IzJERfsaF6E

author
nodoubtman made it! (author)2012-05-16

I have used Colin Mitchel one's with a pic16f84A : http://www.talkingelectronics.com/te_interactive_index.html

it is great! :)
congradulation Colin!
Keep the good work!
marC:)

author
nodoubtman made it! (author)2012-04-26

2n3904 is okay?

author
simoninns made it! (author)simoninns2012-04-26

It should be fine. I've never used one myself, so I can't be 100%. The application is a very basic one though, so pretty much any NPN would probably do the trick.

author
nodoubtman made it! (author)2012-04-25

any other substitute for bc817?

thank you!
marC:)

author
simoninns made it! (author)simoninns2012-04-25

Any small signal NPN would do (like a BC337 for example).

author
nodoubtman made it! (author)nodoubtman2012-04-25

2n3904 should work?

do you have a bigger schematic please? i don't see much on this one

thank you!
marC:)

author
simoninns made it! (author)simoninns2012-04-25

Follow the link in my profile to my website; there you will find better pictures of the project.

author
Saul Cardenas made it! (author)2011-07-21

is there any other pic that i can use i cant find that pic where i live

author
simoninns made it! (author)simoninns2011-07-21

I would think that any similar PIC12F should do the trick. You will need to alter the fuse settings accordingly though.

author
Saul Cardenas made it! (author)Saul Cardenas2011-07-21

what about the PIC12C508A the only difference i see is the memory this one is EEPROM and yours is flash

author
simoninns made it! (author)simoninns2011-07-21

I can't say I've played with that many types of PIC12F to really give you good advice (I usually work with 18Fs). Microchip have an on-line product selection tool which you may find useful:

http://www.microchip.com/productselector/MCUProductSelector.html

author
Saul Cardenas made it! (author)Saul Cardenas2011-08-22

i have another question you have your own pic programmer or you using pickit?
because pickit is expensive and i would like to make my own programmer if i can

author
Saul Cardenas made it! (author)Saul Cardenas2011-08-17

thank you this is my first project with a pic im kinda confused because i dont know much about pics but ill try to do this thanks for this tutorial

author
Tolaras made it! (author)2011-02-21

Hello!

Really nice project! Congratulations!

I have one problem though. How can I assemble all these .c and .h files to an .asm file in order to load them to the PIC microcontroller? I tried microC for PIC and MPLab IDE but I couldn't managed it.

Thanks!

author
simoninns made it! (author)simoninns2011-02-23

The source code is for MPLAB with the HiTech PICC18 compiler which you can download for free from Hi-Tech's website. The code will not work with microC or microchip C18 compilers without alteration.

Hope this helps!

author
Tolaras made it! (author)Tolaras2011-02-23

I 'll try it later today..

Thanks!

author
notsure made it! (author)2011-02-06

Very well done!

About This Instructable

11,313views

19favorites

License:

More by simoninns:RGB LED Love HeartMicroSimon
Add instructable to: