Little Melody Maker

Here is a simple do it yourself project of a little melody maker useful for all those hobbyists interested in making electronic toys or fancy gizmos for their kids. For extreme simplicity and flexibility, the circuit is built around the inexpensive microcontroller board ‘Digispark Attiny85 (Rev2)’. By default, the finished circuit will play the “twinkle twinkle little star…” melody through a small piezo-transducer.

Further, the onboard status indicator of Digispark will give a winking red light effect in tune with the melody. For compactness, try to use a 6F22 9V alkaline battery to run the melody maker. If you are a newcomer in the Digispark world, first of all read this official documentation to learn most things about the minuscule microcontroller development board: https://digistump.com/wiki/digispark/tutorials/connecting

Digispark melody circuit
Circuit

Hardware setup of the melody maker is surprisingly very very simple. All you need is to complete the setup as shown above, and upload the given code to Digispark board through your Arduino IDE. In the setup, PB1 (PWM,504 Hz) of the Digispark is used to drive the piezo-transducer just because the onboard red LED is also wired to the same pin. In case you don’t want the light effect, change the code to use the next port PB0 (PWM,504 Hz) to drive the sounder.

Note that, PB0 is actually pin 1 of the digispark’s 6-pin header while pin 2 of the header is linked to PB1.

Digispark melody maker

The code is in the format of the common Arduino sketch. It’s very simple so that you can tweak it well to your specific requirement and taste. Happy coding!

/*
* Digispark Melody
* Simple Melody Generator (v1)
* Based on Digispark Attiny85 (rev2)
* An inspired work – Adapted sketch for learners!
* T.K.Hareendran/2018
* Codrey Electronics https://www.codrey.com
*/
int length = 15; // Number of notes
//twinkle twinkle little star melody
char notes[] = "ccggaag ffeeddc ggffeed ggffeed ccggaag ffeeddc ";
int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };
int tempo = 300;
void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(piezoPin, HIGH);
delayMicroseconds(tone);
digitalWrite(piezoPin, LOW);
delayMicroseconds(tone);
}
}
void playNote(char note, int duration) {
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
for (int i = 0; i < 8; i++) {
if (names[i] == note) {
playTone(tones[i], duration);
}
}
}
void setup() {
pinMode(piezoPin, OUTPUT);
}void loop() {
for (int i = 0; i < length; i++) {
if (notes[i] == ' ') {
delay(beats[i] * tempo); // rest
} else {
playNote(notes[i], beats[i] * tempo);
}
delay(tempo / 2);
}
}

Here are the notes to be used for playing the melody. I got this from http://www.piano-keyboard-guide.com/how-to-play-twinkle-twinkle-little-star-easy-piano-tutorial/

C C G G A A G
Twinkle, twinkle, little star
F F E E D DC
How I wonder what you are
G G F F E E D
Up above the world so high
G G F F E E D
Like a diamond in the sky
C C G G A A G
Twinkle, twinkle little star
F F E E D D C
How I wonder what you are

This article is in fact a shortcut for making melody using a microcontroller. Here we are taking the advantage of Attiny85 processor’s – the core of Digispark – capability to produce tone signals in order to play the melody. Is everything perfect? No promises! Still there is a lot to learn and try!!!

3 Comments

  1. I just found an error in the code posted here (it’s a mistake in my final draft – sorry for that).

    Add this – int piezoPin = 1; – as the first line of your code to avoid getting error “piezoPin was not declared in this scope”. That means it should be added before the line “int length = 15; // Number of notes”. Thank You!

    1. I am trying to run your code but I am having trouble understanding your for statement
      for (long i = 0; i < duration * 1000L; i += tone * 2)
      for (int i = 0; i < 8; i++)
      for (int i = 0; i < length; i++)

      The Syntax for a for statement is listed as
      for (initialization; condition; increment)
      but your have more than 3 in your for statements.
      Please advise

Leave a Reply

Your email address will not be published. Required fields are marked *