Magazines: AutoSpeed  |  Rallysport  |  V8X  |  Silicon Chip  Shopping: Property  |  Cars  |  Fishing
Email Address:
Password:

Lost your password?

Article Search

PICAXE the Red-Nosed Reindeer

It's based on the new PICAXE-08M chip, flashes its nose and eyes and plays popular Christmas tunes and mobile phone ring tones!

By Clive Seager

 Advertisement
Advertisement 

Microchip has recently released a new PIC micro, the PIC12F683, with twice as much memory as the original PIC12F629 used within the PICAXE-08. This has allowed Revolution Education to develop the PICAXE-08M, an enhanced PICAXE-08 with in-built music playing capabilities – and much more.

Click for larger image
Fig.1: the pinout diagram for the new PICAXE-08M microcontroller.

The PICAXE-08M supports all of the standard commands and features of the PICAXE-08, with the following enhancements:

  • Program memory has been doubled in capacity (approx. 80 lines of code).
  • Musical tune playing capability, user generated or four pre-programmed tunes (play and tune commands).
  • 10-bit ADC option on 3 pins (read-adc10/readadc commands).
  • Interrupt feature on inputs (setint command).
  • Infrared remote control input and output (infrain2/infraout commands).
  • Continuously driven PWM motor drive output (pwmout command).
  • Control of radio control type servos (servo command).
  • Count high frequency pulses within a set time period (count command).
  • Accurate digital temperature sensor interface (readtemp/readtemp12 commands).
  • Read serial number from any Dallas 1-wire device (eg, iButton) (readowsn command).
  • Software support for increased (8MHz) clock frequency (setfreq command).

Note that all existing PICAXE-08 programs will run on the 08M without modification.

Click for larger image
Fig.2: with the aid of a PICAXE-08M micro and simple software, the circuit can flash Rudolph's nose and eyes and play a seasonal tune. The infrared receiver (described next month) allows remote selection of one of four possible tunes.

Playing tunes on a PICAXE

The main obstacle when playing tunes on PIC microcontrollers is limited memory space. All the PICAXE chips have a sound command to make noises. However, the number of notes and playback speed, or "tempo", is very limited, as the sound command data rapidly consumes all available program memory.

To overcome these issues, the PICAXE-08M has a newly developed tune command to play music. The tune command incorporates a note data compression algorithm to save memory space. It is also pre-programmed with four melodies: Happy Birthday, Jingle Bells, Silent Night and Rudolph the Red Nosed Reindeer. In addition, it can be programmed with the mobile phone ring tone of your choice.

Mobile phone ring tones are widely available on the Internet in "ring tone text transfer" format (RTTTF), as used on most Nokia phones. Although some web sites charge for ring tone downloads, there are still a number of sites that provide these free of charge. A simple microcontroller cannot play complicated "polyphonic" tunes, but can make a good attempt at playing "monophonic" (one note at a time) tunes.

Circuit description

Click for larger image
Fig.3: follow this diagram closely when assembling Rudolph. Note in particular the orientation of the three LEDs and the PICAXE micro as well as the power supply positive and negative leads.

Fig.2 shows the circuit of our simple Christmas decoration, with the PCB shaped in the form of Rudolph’s head. Output 0 is connected to a 10mm red LED, forming Rudolph’s nose. Output 4 drives two smaller green LEDs for the eyes, whereas a piezo transducer on output 2 plays the tune. Inputs 1 and 3 are connected to a light-dependant resistor (LDR) and push-button switch.

In this circuit, the PICAXE-08M micro can be programmed to play a tune when the switch is pressed and/or the light level (sensed by the LDR) changes. Note that the push-button switch circuit is "active low" for compatibility with the infrared upgrade, which will be described next month.

The supplied piezo transducer does a reasonable job of tone reproduction. For a richer sound, it can be replaced with a low-cost 40Ω speaker if desired. Wire one of the speaker leads in series with a 10μF electrolytic capacitor before hook-up to the PC board.

Kit assembly

A kit of parts that includes the pre-shaped PC board will be available from your usual PICAXE reseller. The PC board overlay is shown in Fig.3. Assembly of the board is very straightforward, with attention to the points listed below.

Although the infrared sensor (IR) and capacitor (C1) appear on the circuit and overlay diagrams, they are required only for remote control operation and can be left out for the moment.

Click for larger image
Fig.4: this piano keyboard representation shows the key frequencies in octaves 5, 6 and 7. This is the range of frequencies covered by the PICAXE-08M.

Take care with the orientation of the three LEDs, which must have their flat (cathode) sides positioned as shown. Also, make sure that the notched (pin 1) end of the PICAXE micro faces the serial link socket. Before soldering the battery clip leads, thread them through the adjacent hole to provide strain relief.

Power your completed project only from a 3 x alkaline AA cell (4.5V) battery pack or regulated 5V DC supply. Take particular care that you have the power leads around the right way, otherwise you’ll destroy the PICAXE!

The following paragraphs explain how the music encoding process works. If you’re not interested in the technicalities and just want to play tunes, you can skip directly to the "Tune Wizard" section below!

Bit No. 7, 6 - Duration Bit No. 5, 4 - Octave Bit No. 3, 2, 1, 0 - Note
00 = 1/4 00 = Middle Octave (6) 0000 = C
01 = 1/8 01 = High Octave (7) 0001 = C#
10 = 1 10 = Low Octave (5) 0010 = D
11 = 1/2 11 = not used 0011 = D#
0100 = E
0101 = F
0110 = F#
0111 = G
1000 = G#
1001 = A
1010 = A#
1011 = B
11xx = P (pause)

Notes: 1/16, 1/32 and 'dotted' notes are not supported. Only octaves 5-7 are supported.

Fig.5: the encoding of each note byte used by the tune command.

Tune command

The tune command has the following syntax: tune led, speed, (note, note, note . . .) where:

(1).led is a variable/constant (0-3) which specifies if other outputs flash at the same time as the tune is being played; ie,

0 = No outputs

1 = Output 0 flashes on and off

2 = Output 4 flashes on and off

3 = Output 0 and 4 flash alternately

(2).speed is a variable/constant (1-15) which specifies the tempo of the tune.

(3).note, note, etc is the encoded musical note data.

Note compression

Most ring tones use notes from octaves 5, 6 and 7 (octave 6 starts with "middle C" for the piano players!). A graphical representation of these notes compared to a piano keyboard is shown in Fig.4.

Click for larger image
Fig.6: import RTTTL tunes or create your own with the Tune Wizard, included in versions 4.0.1 and later of the PICAXE Programming Editor

There are 12 notes to an octave (including incidental notes), and if you add pause, this gives 13 different possibilities. As the tune plays, each note also varies in duration, and most ring tones use 4 different lengths - notes of duration 1/8 (quaver) 1/4 (crotchet) 1/2 (minim) or 1/1 (semibreve). Theoretically, notes of 1/16 and 1/32 are also possible, but only a few ring tones use these durations and so they are not considered in our algorithm.

Considering this information, each note byte can be encoded into just 1 byte of memory as shown in Fig.5. The encoding is optimised to ensure that the most common values (1/4 beat and octave 6) both have a value of 00. Note that as the PICAXE also performs further optimisation on the whole tune, the length of the tune data will not be exactly the same length as the number of notes in the tune.

Speed Beats/Minute
1 812
2 406
3 270
4 203
5 162
6 135
7 116
8 101
9 90
10 81
11 73
12 67
13 62
14 58
15 54

Fig.7: this table shows the equivalent tempo in beats/ minute for the 15 possible speed values.

Tempo

The speed of music is normally called "tempo", defined as the number of quarter beats per minute (BPM). The PICAXE tune command allows 15 different speeds (1-15) calculated as follows:

The sound duration of a quarter beat within the PICAXE is given by the following formula:

sound duration = speed x 65.64ms

Each quarter beat is also followed by a silence duration, given by:

silence duration = speed x 8.20ms

Therefore, the total duration of a quarter beat is:

total duration = (speed x 65.64) + (speed x 8.20) ms = speed x 73.84ms

The approximate number of beats per minute at different speed values is shown in Fig.7. The chosen values give a good range for most popular tunes.

Note that within electronically generated music a note normally plays for 7/8 of the total note time, with silence for 1/8. With the PICAXE-08M the ratio is slightly different (8/9) due to the memory and mathematical limitations of the microcontroller. However, unless you are a gifted musician you probably won’t notice the difference!

Tune Wizard

The Tune Wizard within the PICAXE Programming Editor software (v4.1.0 or later) allows musical tunes to be created for the PICAXE-08M. Tunes can be entered manually using the drop-down boxes if desired but most users will prefer to import a mobile phone monophonic ring tone.

Note that the PICAXE can only play one note at a time (monophonic), and so cannot use multiple note (polyphonic) ring tones.

A typical RTTTL ring tone, as downloaded from the Internet, is shown in Fig.8. The first few text characters describe the ring tone name, then the default characteristics (default note duration = 1/4, default octave = 5, bpm = 125), then each note in turn. This ring tone can be imported straight into the Tune Wizard. The Tune Wizard will then automatically generate the BASIC code required for the PICAXE program.

The tune can also be tested on the computer by clicking the "Play" menu (assuming the PC is fitted with a soundcard and speakers). The tune played will give you a rough idea of how the tune will sound on the PICAXE, but will differ slightly due to the different ways that the computer and PICAXE generate and playback sounds.

The "outputs" section of the Wizard interface allows you to choose which outputs flash on and off as the tune is played. For the Rudolph project, select both outputs (nose and eyes!)

Fig.8: Star Wars Theme Tune In RTTTL Format

Star Wars Theme:
d=4,o=5,b=125:1a#,1f6,8d#6,8d6,8c6,1a#6,2f6,8d#6,8d6, 8c6,1a#6,2f6,8d#6,8d6,8d#6,2c6,8f,8f,8f,2a#,2f6,8d#6,8d6, 8c6,1a#6,2f6,8d#6,8d6,8c6,1a#6,2f6,8d#6,8d6,8d#6,1c6

Rudolph program

The program listing shown in Fig.9 demonstrates how to play a tune when the switch is pushed.

The tune played will vary between four different melodies, depending on the light level falling on the LDR when the switch is pushed. Three of these melodies (Jingle Bells, Silent Night & Rudolph) are pre-programmed tunes included within the PICAXE-08M bootstrap program and are activated by the play command.

The final melody ("We Wish You a Merry Christmas") is a user-defined tune, generated by the Tune Wizard, and played with the tune command.

Summary

The play and tune commands allow you to play simple musical tunes on the new PICAXE-08M. There are approximately 1000 tunes for free download on the software page of the PICAXE website at:

http://www.rev-ed.co.uk/picaxe/

Some other possible sources for free ring tones are:

http://www.ringtonerfest.com/

http://www.free-ringtones.eu.com/

Fig.9: Rudolph Program Listing

' ***** main loop *****
main:

if pin3 = 0 then playit
  goto main


' ***** play tune *****

playit:
  readadc 1,b1 'read light level
  REM debug b1 'optional display on screen for testing

 

' play tune depending on light level
if b1 > 200 then play_xmas
if b1 > 150 then play_rudolph
if b1 > 80 then play_silent

play_jingle: 'internal tune Jingle Bells
  play 1,3
  goto main

 

play_silent: 'internal tune Silent Night
  play 2,3
  goto main
 
play_rudolph: 'internal tune Rudolph The Red Nosed Reindeer
  play 3,3
  goto main

 

play_xmas: 'external ring tone tune

 

' We Wish You a Merry Christmas
tune 3, 4, ($22,$27,$67,$69,$67,$66,$24,$24,$24,$29,$69,

$6B,$69,$67,$26,$22,$22,$2B,$6B,$40,$6B,$69,$27,$24,$22,

$24,$29,$26,$E7,$22,$27,$67,$69,$67,$66,$24,$24,$24,$29,

$69,$6B,$69,$67,$26,$22,$22,$2B,$6B,$40,$6B,$69,$27,$24,

$22,$24,$29,$26,$A7,$22,$27,$27,$27,$E6,$26,$27,$26,$24,

$E2,$29,$2B,$69,$69,$67,$67,$02,$22,$22,$24,$29,$26,$E7)

 

goto main

Next month

Next month, we’ll show you how to control Rudolph via an infrared remote control. The transmitter also uses a PICAXE-08M chip and will take you less than five minutes to assemble.

In addition you have to install two parts on the PC board and change the PICAXE program.

Parts List

1 Rudolph PC board

1 miniature LDR

1 miniature piezo transducer

1 miniature pushbutton switch

1 3.5mm stereo socket

1 3 x AA battery holder

1 battery clip

1 8-pin IC socket

Semiconductors

1 PICAXE-08M

2 5mm green LEDs (LED1 & LED2)

1 10mm red LED (LED3)

Resistors (0.25W 5%)

1 22kΩ (red red orange gold)

2 10kΩ (brown black orange
gold)

1 4.7kΩ (yellow violet red gold)

4 330Ω (orange orange brown gold)

Note: the infrared receiver (IR) and capacitor (C1) are not required at this stage. Their use will be covered in the infrared upgrade next month.

Also required (not in the kit)Z

PICAXE Programming Editor software (v4.1.0 or later)

PICAXE download cable (Part No. AXE026)

3 x AA alkaline cells

Obtaining kits & software

The design copyright for this project is owned by Revolution Education Ltd.

Complete kits (Part No. AXE-107S) for this project are available from authorised PICAXE distributors – see http://www.microzed.com.au/ or phone MicroZed on (02) 6772 2777.

The PICAXE Programming Editor software can be downloaded free of charge from http://www.rev-ed.co.uk/picaxe/ or ordered on CD (Part No. BAS805).

The following downloads are available for this article:



Privacy Policy  |  Advertise  |  Contact Us

Copyright © 1996-2005 Silicon Chip Publications Pty Ltd & Web Publications Pty Limited. All Rights Reserved