Introduction: A Simple 2 Hour PICAXE Project - Play Tunes With an 08M2 Microcontroller for About $10
This project can be built and programmed in about 2 hours if you have all the required parts on hand. What it does is play the tunes that are already pre-programmed in the 08M2. The tunes are "Happy Birthday", " Jingle Bells", "Silent Night" and "Rudolph the Red Nosed Reindeer".
For this project we will include a dip switch which will allow you to select which of the 4 tunes you'd like to play. Also included on board is the programming jack and circuit so that you can program and re-program the PICAXE at will.
Once you have this circuit built you could also write a program to use the "tune" and "sound" commands. You could also write a program to use the Tune Wizard and import a cellphone ring tone. For now I will only include the program and set up to play the four pre-canned tunes and will leave it to the reader to experiment with other programs.
I'd estimate that you can build and program this project in about 2 hours if you have all the parts ready and already have the programming software and interface cable drivers set up on your PC. The estimated cost is about $10 using the approximate prices I paid - PICAXE $2.50, Speaker $4, Jack $1.50, vector board $1, resistors & wire $1 and assumes you already have a 5V power source such as a wall wart or bench top supply, tools, solder etc. 3 AA batteries in a clip to give 4.5 V is a good power supply too.
For this project a prerequisite is that you have the PICAXE Programming Editor set up on your PC as well as the programming cable drivers and programming cable and that you know how to use them.
Step 1: PICAXE Project Parts List
1 - PICAXE 08M2 Microcontroller
1 - 8 ohm speaker or 40 ohm speaker and omit the 33 ohm resistor when using 40 ohms.
1 - 8 pin IC Socket
1 - 3mm stereo jack
1 - 22K ohm resistor
1 - 33 ohm resistor
3 - 10K ohm resistors
1 - two position dip switch or equivalent switches
1 - 5V power source - wall wart, bench top supply or 3 AA batteries in a battery clip.
1 - 9V battery connector (optional if using clip)
1 - small vector board approximately 1.5" x 2.5"
1 - 22 gauge solid core and stranded wire
Step 2: PICAXE Tune Player Circuit Diagram
In this section we review the circuit diagram. To see it more clearly zoomed in click on the image or look at the actual jpg file included in the zip file at the end.
First you see the programming jack attached to the serial in/out pins and ground as per the PICAXE specifications.
The C.3 and C.4 pins are connected to +5V via 10K resistor which normally holds them high. The dip switches are also connected to the C.3 and C.4 pins and this will pull them to ground when the switches are closed. By this means, we will tell the PICAXE which of the 4 internal tunes to play. With just two switches we have 4 states 0,0 or 0,1 or 1,0 or 1,1 where 1 is +5V on the pin and 0 is ground on the pin.
The loudspeaker, which in this case is an 8 ohm speaker, is connected through a 33 ohm resistor as the PICAXE specification recommends that Pin 5 is driving into 40 ohms. As there is no 32 ohm resistor readily available, use 33 ohms. For other speaker impedances, use an appropriate resistor value.
Each pin of the 08M2 has other functions but here the pins are labelled only with the pin function of interest for this project. Note that with the 8 Pin PICAXE only Pin 5, the C.2 pin can be used to output music to the speaker.
Step 3: PICAXE Project - Programming Jack Connections
The photograph of the 3.5 mm programming jack in this section shows the connections to the Serial In, Serial Out and Ground pins as this is not intuitively obvious and can be difficult to figure out for new users. Here we show the top of the jack with corresponding locations of each connection to the pins on the bottom. Most jacks only have one pin in each of the three locations. Some jacks will have 2 pins close together at each location which are connected internally, you can verify this with a DVM if you need to and you only need to connect to one of the pins at each of the 3 points.
Step 4: PICAXE Tune Player Program
Below and in the picture is the program "Play PICAXE 08M2 Tunes.bas". This is a simple program that reads the state of the two dip switches and from the state will play one of the 4 tunes. Once a tune is selected the player will loop continuously playing that tune over and over until a new dip switch combination is selected. It will then play to the end of the tune it is currently playing and then switch to the new selected tune. On power up it will immediately play the tune selected by the dip switch setting in place right before power up.
Here we have set the com port on the computer to com port 3 using the directive #com 3. If you have trouble communicating with the PICAXE, select View > Options > Serial Port in the Program Editor and see which Com port says "Ready for use AXE027 PICAXE USB". Next change the directive in the program to the com port you are using on you PC. For example if you are using com port 4, then set the directive to #com 4, not #com 3 as we have here. Next save the program and check syntax to make sure you are good to go and then program your PICAXE.
' =============== Play.bas ===============
' This program runs on a PICAXE-08M2.
' It plays the pre-canned tunes in an 08M2
' === Constants ===
symbol SWCH1 = pinC.3 ' Programming Switch 1
symbol SWCH2 = pinC.4 ' Programming Switch 2
' === Directives ===
#com 3 ' specify serial port
#picaxe 08M2 ' specify processor
' ============= Begin Main Program =============
if SWCH1 = 0 and SWCH2 = 0 then Play 0, C.2 endif ' Play Happy Birthday
if SWCH1 = 1 and SWCH2 = 0 then Play 1, C.2 endif ' Play Jingle Bells
if SWCH1 = 0 and SWCH2 = 1 then Play 2, C.2 endif ' Play Silent Night
if SWCH1 = 1 and SWCH2 = 1 then Play 3, C.2 endif ' Play Rudolph the Res Nosed Reindeer
Step 5: PICAXE Tune Player Files and Attachments
1. 08M2 Player Program.bas
2. Circuit diagram
3. Programming Jack Connections
4. PICAXE Manuals