Arduino and a speaker

Another simple example is to hook up a speaker to your Arduino and get a bit musical with it. These are fairly low cost and can quite often be salvaged from other pieces of electronic equipment

 

Layout

speaker example

Schematic

 

speaker schematic

Code

A simple SOS example. You could do all of the morse code alphabet (thats coming later) or obviously do something more tuneful.

A better way of doing this is to have functions for the dots and dashes, again in the future morse code example we’ll do that

int pin = 8;
int note = 988;

void setup()
{
}

void loop()
{
//3 dots for the S
for (int i=0; i<3; i++)
{
tone(pin, note, 100);
delay(200);
noTone(pin);
}
delay(200);
//3 dashes for the O
for (int i=0; i<3; i++)
{
tone(pin, note, 300);
delay(400);
noTone(pin);
}
delay(200);
//3 dots for the S again
for (int i=0; i<3; i++)
{
tone(pin, note, 100);
delay(200);
noTone(pin);
}
delay(200);
//wait 3 seconds
delay(3000);
}

Parts List

 



Amount	Part Type
1	Arduino Uno (Rev3)
1	100 Ω Resistor
1	Loudspeaker

Links

Related posts

VEML6070 ultraviolet light sensor on an Arduino

Programming an Attiny44 using an Arduino

Programming an Attiny85 using an Arduino