Speaker help

Hello,

I took a tweeter speaker from a toy and was wondering, how they make different sounds? is it different amounts of electricity? Will that change the sounds? Thanks!

its the frequency you put into the speaker

As josh97531 stated, the primary way to change the sound outputed from an Arduino is by varying the frequency (how fast the output switches between HIGH and LOW). Trying to change the current to the speaker won't do much, until you start exceeding the maximum rating and then the speaker might get damaged. Changing the voltage level incrementally (like from +5 VDC to +3.3 VDC) will act like a crude volume control, the lower the voltage the quieter the sound.

You can do this: arduino pin 2 to 150 ohm or higher (up to 1K) resistor to speaker terminal, other speaker terminal to ground.
Then run this sketch:

byte speakerPin = 2;
unsigned long halfPeriod = 50;

void setup(){
pinMode (speakerPin, OUTPUT);
}

void loop(){
digitalWrite (speakerPin, HIGH);
delayMicroseconds (halfPeriod);
digitalWrite (speakerPin, LOW);
delayMicroseconds (halfPeriod);
}

Now by changing the value of halfPeriod, you will hear different frequencies.

50uS x 2 =100uS signal period. Frequency is 1/period. So 1/.000100 = 10,000 Hz, pretty high pitched.
1136 x 2 = 2272uS. 1/2272 = 440Hz, which is Middle A.

The frequency will be a little off as there is some delay associated with the digitalWrites. If you have a good ear, or something to compare the notes against, you can tweak the halfPeriod a little to adjust the frequency.
If you further replace halfPeriod with say quarterPeriod and threequarterPeriod, and define those as 25 and 75 for example, you will still get a 10,000 Hz note, but it will have a different sound to it as the high and low times are different.

Don't leave out the series resistor tho, or you could damage the arduino pin by putting mroe than 40mA into the speaker.