I am an absolute noob in electronic circuits, though I am a solid game programmer.
I played with leds and now I want to try a speaker, what I wan't to know is it safe to connect a speaker (taken from a home theater system) to one of the pins without a resistor or something?
and which resistor, I have 3 types of resistors came with the kit.
I can handle the frequency math and logic as I programmed a sound generator before, I just need the connection info.
I played with leds and now I want to try a speaker, what I wan't to know is it safe to connect a speaker (taken from a home theater system) to one of the pins without a resistor or something?
No - do not do this. You can connect a small piezo speaker directly, but nothing of any larger size.
You should only connect such speakers through an amplifier, the amplifier input would be driven by the Arduino (thru an inline capacitor, usually). A simple basic amplifier IC would be the LM386:
Classic, its been used everywhere; plenty of reference material on it, though the reference design inside the spec sheet will work fine to drive your speaker.
Something to keep in mind is that the output from the Arduino will be a wave (square, if using a single pin) with a voltage swing from 0-5 volts. While you can feed this into an amplifier, its not really correct; you really want a swing around 0V, and attenuated somewhat to reduce it to about 1 or 2 volts peak-to-peak (vpp). To do this, you need to bias the output signal so that ground is about 2.5 volts higher than normal, so-as to pull the signal down and center it. Then, you use a small trimmer potentiometer to reduce the signal (via a voltage divider).
Note - if this all sounds like "greek" to you, stick with something pre-built, or a piezo, or do a lot of research and learning...
To do this, you need to bias the output signal so that ground is about 2.5 volts higher than normal, so-as to pull the signal down and center it. Then, you use a small trimmer potentiometer to reduce the signal (via a voltage divider).
Or more simply wire a series 1mfd capacitor from the pin to the pot. That will remove the DC bias voltage, leaving only AC audio frequencies to pass. As already stated, you can't directly generate sine wave audio, just square wave that have lots of odd harmonic frequencies, so it will always sound buzzy and harsh.
Or more simply wire a series 1mfd capacitor from the pin to the pot. That will remove the DC bias voltage, leaving only AC audio frequencies to pass. As already stated, you can't directly generate sine wave audio, just square wave that have lots of odd harmonic frequencies, so it will always sound buzzy and harsh.
I'm sure that'll "round off" the square wave, but I don't think that will center it; in other words, you'll get audio output from approx 0-5 volts, not -2.5 to +2.5...?
Yes, if you have powered computer monitor speakers that would work great - I did my testing of the Tone melody I use as an alarm doing that.
Then I ran it thru a simple MOSFET amplifier in my built-up design, see below.
I use it to make a nice warble - doesn't sound harsh & buzzy at all.
The switch & 2nd resistor sort of make a volume selector - only the difference is not as big as I thought it would be, so I may change the switch to have 2 in series (136 ohm) and 2 in parallel (34 ohm), that should make it more apparent - need something besides a SPST switch tho.
// presetup code
// info on alarm sound
#include "pitch.h" // this is in a separate tab in the sketch
// notes in the melody to play when a touch is scored:
int thisNote = 0;
int noteDuration = 0;
int pauseBetweenNotes = 0;
int melody[] = {
NOTE_C6, NOTE_A5, NOTE_C6, NOTE_A5, NOTE_C6, NOTE_A5, NOTE_C6};
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
12,12,12,12,12,12,4};
// code run later in void loop
// create a warble once
for (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.
noteDuration = 1000/noteDurations[thisNote];
noTone(17); //apparent known bug - need this for the tone to play next.
tone(17, melody[thisNote],noteDuration);
// to distinguish the notes, set a minimum time between them.
// using the note's duration + 10%:
pauseBetweenNotes = noteDuration * 1.10;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(17);
}
digitalWrite (speakerOut, LOW); // turn off the amp -> capacitor coupled & gate pulled low, should not need this now, left over from before installing the series gate cap
delay (500);
// and suitable notes from pitch.h
#define NOTE_A5 880
#define NOTE_C6 1047
I'm sure that'll "round off" the square wave, but I don't think that will center it; in other words, you'll get audio output from approx 0-5 volts, not -2.5 to +2.5...?
Sure it will. A key characteristic of a capacitor is that it will block DC voltages but pass AC voltages. Look at most common DC single polarity powered multi-stage audio amplifier circuits. They use capacitance coupling to isolate the DC bias between each stage but allow the audio (AC) to pass to the next stage for amplification of the ac signal.
There can be audio amplifiers using full complementary circuits and dual polarity voltages that don't require capacitance coupling, but they are more complex and expensive. Classic opamps are DC coupled internally and that is why external input offsets voltage are sometimes required to remove or change the offsets of the applied input signal.
Simple two winding transformers are also used to isolate the high voltage DC bias but pass audio (AC) in the classic single tube class A amplifiers.
As long as the final load, the speaker in this case, has one lead referenced to ground, (circuit common) the other lead will 'see' a true 'centered waveform with no DC bias or offset. It's no difference when applied to sending a 0-5v square wave from an output pin through a series capacitor on to a grounded load. It won't be a sine wave, but it will be 'centered'.