ATTINY 85 - Play Star Wars Theme and Imperial March

by scanos in Circuits > Electronics

160 Views, 0 Favorites, 0 Comments

ATTINY 85 - Play Star Wars Theme and Imperial March

The Star Wars theme and Imperial March RTTTL
IMG_20180331_113326.jpg

This is a simple project where the Star Wars theme and Imperial March are played on an ATTINY 85 through a Piezo speaker. I programmed this using the Arduino IDE, see my previous instructable. (https://www.instructables.com/id/15-Dollar-Attiny8... Please read this before proceeding with the current instructable. As you can see, you can play this using 2 x 1.5v button batteries. Indeed, it will even operate using a single 1.5v button battery. This is just a prototype and I will next build this as a proper circuit with a switch etc.

What you need:

1. Bread board.

2. ATTINY 85 - set frequency to 1 Mhz internal.

3. USBasp programmer.

4. Piezo speaker.

5. Arduino IDE.

6. Male to male breadboard cables.

7. Power source. I used two 1.5v button batteries clipped together.

Hardware Set-up

WIN_20180331_16_52_18_Pro.jpg

The basic circuit is shown in the above image.

You just need to connect

  1. The ATTINY 85 vcc and ground pins to a power source.
  2. An LED from the ATTINY 85 PB1 (physical PIN 6) to the Piezo speaker data pin(S). The larger LED leg connects to the ATTINY 85 side.
  3. Piezo (-) pin to the ground pin on the ATTINY 85
  4. I did not have to use capacitors and resistors because of the low current but if you are using higher current, please use the following:
    1. Capacitors - Use a 104 for connecting between main vcc supply and vcc pin on ATTINY85, and 0.22uf capacitor (note long leg is positive) to place across + and gnd lines.
    2. Place a low rated resistor (less than 500 ohms to protect the LED.

You can of course choose to use a switch in the circuit.

Software Code

I used the Arduino IDE as described in my earlier tutorials. The premise of the code is that a TinyTone function is used to emulate the Arduino Tone function which is not available for the ATTINY85. Using TinyTone, a series of notes are played in sequence with each note having the following attributes, identity, octave and duration, e.g. - TinyTone(Note_F, 3, 1400). I discovered this function on a website , http://www.technoblogy.com/show?KVO, and I acknowledge the work done by the author. I also developed my own musical notation for the Starwars theme and the Imperial March using my guitar. Here is my code which plays the tune 5 times in a loop.

<p>//http://merwin.bespin.org/t4aphp/index.php<br>//https://codebender.cc/example/Tone/RTTTL#RTTTL.ino
/* TinyTone for ATtiny85 */
//set frequency to 1mhz
// Notes
const int Note_C  = 239;
const int Note_CS = 225;
const int Note_D  = 213;
const int Note_DS = 201;
const int Note_E  = 190;
const int Note_F  = 179;
const int Note_FS = 169;
const int Note_G  = 159;
const int Note_GS = 150;
const int Note_A  = 142;
const int Note_AS = 134;
const int Note_B  = 127;</p><p>int Speaker = 1;
bool val = false;                 // variable to store the sensor status 
void setup()
{
  pinMode(Speaker, OUTPUT);</p><p>}</p><p>void loop()
{</p><p>  playTune();
  
delay(5000);
}</p><p>void TinyTone(unsigned char divisor, unsigned char octave, unsigned long duration)
{
  TCCR1 = 0x90 | (8-octave); // for 1MHz clock
  //TCCR1 = 0x90 | (11-octave); // for 8MHz clock
  OCR1C = divisor-1;         // set the OCR
  delay(duration);
  TCCR1 = 0x90;              // stop the counter
}</p><p>// Play a scale
void playTune(void)
{
   for (int i=0; i <= 5; i++){
 delay(5000);
TinyTone(Note_F, 3, 1400);
 TinyTone(Note_F, 3, 1400);
 TinyTone(Note_F, 3, 1400);
  TinyTone(Note_AS, 3, 7250);
   TinyTone(Note_F, 3, 7200);
   TinyTone(Note_DS, 4, 1500);
   TinyTone(Note_D, 4, 1500);
   TinyTone(Note_C, 4, 1500);
  TinyTone(Note_AS, 4, 6500);
     TinyTone(Note_F, 3, 3500);
   TinyTone(Note_DS, 3, 1500);

   TinyTone(Note_D, 3, 1500);
   TinyTone(Note_C, 3, 1500);
   TinyTone(Note_AS, 3, 7500);
  TinyTone(Note_F, 3, 3500);
     TinyTone(Note_DS, 3, 1500);
   TinyTone(Note_D, 3, 1500);
      TinyTone(Note_DS, 3, 1500);
   TinyTone(Note_C, 3, 7500);
delay(3000);
TinyTone(Note_A, 3, 5000);
TinyTone(Note_A, 3, 5000);
TinyTone(Note_A, 3, 5000);
  TinyTone(Note_F, 3, 3500);
  TinyTone(Note_C, 2, 1500);
   TinyTone(Note_A, 3, 5000);
   TinyTone(Note_F, 3, 3500);
  
delay(3000);
 TinyTone(Note_E, 2, 5000); 
 TinyTone(Note_E, 2, 5000);
//TinyTone(Note_E, 2, 5000);
  TinyTone(Note_F, 2, 3500);
    TinyTone(Note_C, 2, 1500);
     TinyTone(Note_GS, 3, 5000);
      TinyTone(Note_F, 3, 3500);
         TinyTone(Note_C, 2, 1500);
             TinyTone(Note_A, 3, 6500);     </p><p> 
delay(2500);</p><p>TinyTone(Note_A, 2, 5000);
 TinyTone(Note_A, 3, 3000);
 TinyTone(Note_A, 3, 1500);
  TinyTone(Note_A, 2, 5000);
  TinyTone(Note_GS, 2, 3250);
   TinyTone(Note_G, 2, 1750);
   TinyTone(Note_FS, 2, 1250);
     TinyTone(Note_F, 2, 1250);
  TinyTone(Note_FS, 2, 2500);
delay(3000);</p><p>TinyTone(Note_AS, 2, 2500);
 TinyTone(Note_DS, 2, 5000);
 TinyTone(Note_D, 2, 3250);
  TinyTone(Note_CS, 2, 1750);
   TinyTone(Note_C, 2, 1250);
   TinyTone(Note_B, 3, 1250);
   
  TinyTone(Note_C, 2, 2500);
 
</p><p>   
   }
   exit(0); 
 
delay(350);</p><p>}</p>

Conclusion

It's quite an interesting little project and low cost. As well as using Pizeo speakers, I also got it to work with some low cost speakers which I bought from Poundland.

I hope you enjoy it.

PS - I found this musical notation / code for La Cucaracha on the web-site to which I alluded earlier:

<p>// Play La Cucarachavoid playTune(void){TinyTone(Note_C, 4, 200);TinyTone(Note_S, 1, 10); TinyTone(Note_C, 4, 200);TinyTone(Note_S, 1, 10); TinyTone(Note_C, 4, 200);TinyTone(Note_S, 1, 10); TinyTone(Note_F, 4, 600);TinyTone(Note_S, 1, 10); TinyTone(Note_A, 4, 600);TinyTone(Note_S, 1, 10); TinyTone(Note_C, 4, 200);TinyTone(Note_S, 1, 10); TinyTone(Note_C, 4, 200);TinyTone(Note_S, 1, 10); TinyTone(Note_C, 4, 200);TinyTone(Note_S, 1, 10); TinyTone(Note_F, 4, 600);TinyTone(Note_S, 1, 10); TinyTone(Note_A, 4, 800);TinyTone(Note_S, 1, 20); TinyTone(Note_F, 4, 200);TinyTone(Note_S, 1, 10); TinyTone(Note_F, 4, 200);TinyTone(Note_S, 1, 10);TinyTone(Note_E, 4, 200);TinyTone(Note_S, 1, 10);TinyTone(Note_E, 4, 200);TinyTone(Note_S, 1, 10);TinyTone(Note_D, 4, 200);TinyTone(Note_S, 1, 10);TinyTone(Note_D, 4, 200);TinyTone(Note_S, 1, 10);TinyTone(Note_C, 4, 1000);}</p>