Emma's notes    
  Class wk 12    
  Output devices.    
    Home About Classes Final project Various notes  
     
 

ASSIGNMENT
Add an output device to a microcontroller board you've designed and program it to do something.

WHAT I DID
For my final project I would like to play some melodies and I tried to generates them with a piezo. The workflow I followed is:
- play melody with Arduino Uno
- play melody with Attiny44.

ARDUINO UNO PLAYS MELODY
Thanks to the wide documentation about arduino I used this reference: http://www.arduino.cc/en/Tutorial/melody I was able to generate not only noise. I connected th piezo to ground and to pwm pin 9.

I loaded not-written-by-me code:

int speakerPin = 9;

int length = 15; // the number of notes
char notes[] = "ccggaagffeeddc "; // a space represents a rest
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(speakerPin, HIGH);
delayMicroseconds(tone);
digitalWrite(speakerPin, 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 };

// play the tone corresponding to the note name
for (int i = 0; i < 8; i++) {
if (names[i] == note) {
playTone(tones[i], duration);
}
}
}

void setup() {
pinMode(speakerPin, 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);
}

// pause between notes
delay(tempo / 2);
}
}

and you can hear the melody in the video:

Thanks Zaerc I could try the same code on different output device: a little speaker, 8 Ohm - 0.5W. The driver board implementes: a MOSFET N-CH and a resistor, the pink cable is supply (3.3V), the blue one is the input signal (pwm pin 9 of Arduino Uno) and the green cable is the ground.

ll mn

I prefer the speaker and I will evalute which solution will fit better in my final project.

Looking for informations about this topic I also found that it's possible to play "noise" with a digital output and the pwm is not necessary, using the library pitches.h and the command tone(). The example code is:

#include "pitches.h"

// notes in the melody:
int melody[] = {
NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
4, 8, 8, 4,4,4,4,4 };

void setup() {
// iterate over the notes of the melody:
for (int thisNote = 0; thisNote < 8; thisNote++) {

// to calculate the note duration, take one second
// divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000/noteDurations[thisNote];
tone(8, melody[thisNote],noteDuration);

// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(8);
}
}

void loop() {
// no need to repeat the melody.
}

In this case the sound-signal comes out from the digital pin 8.

ATTINYxx PLAYS MELODY
In my final project I will use a Attiny microcontroller and my considerations are:
- I'm afraid the ATTiny44 has not enoght memory => for safe I will use a ATTiny84.
- from the consumption point of view the speaker is the better choise.
- digital pin or pwm pin? still don't know.
If you want to see how I managed to play sound with the ATtiny I forward you to the final project page that I will keep update during the project development.

 
     
 

Tools

Software: Eagle, Arduino IDE.
Machine: Roland Modela MDX-20.