Arduino – 3 speaker output

3speaker_hookup_thmbI was able to wire 3 speakers to my arduino and get them all playing the same rhythms and tones but at different times. I’m attempting to get the speakers to play individual tunes to create chords amongst the speakers. I was also able to get 4 speakers playing simultaneous from the arduino (with out a shield) however, not different tones YET. I’m in the process 🙂 My coding skills are a little rough so time is what I need most.

Anyway, I am pretty excited to get more than one speaker working with the arduino. I’ve been told several times that it cannot do multiple output but I am determined to write the code and post it online ASAP.

So here you can see I just used 3 speakers I had available… well, 2 speakers and one piezo disk. Inserted into the digital outpins 10, 9 and 3 and then grounded.

Listen (3speaker_audio.mp3, 284KB)

Arduino code for 3 speaker output/sound rotation:
__________________________

/* This code was altered from Paul Badgers freqout tone generation function.
*   freqout(freq, t)  // freq in hz, t in ms
*   Paul Badger 2007
*   a simple tone generation function
*   generates square waves of arbitrary frequency and duration
*   program also includes a top-octave lookup table & transposition function
*/

#include <math.h>  // requires an Atmega168 chip

#define piezoPin 10 // audio out to speaker or amp
#define outpin 9
#define speaker3 3

int ptime;
int   k,  x, dur, freq, t;
int i, j;

float ps;         // variable for pow pitchShift routine

float noteval;

// note values for two octave scale
// divide them by powers of two to generate other octaves
float A     = 14080;
float AS    = 14917.2;
float B     = 15804.3;
float C     = 16744;
float CS    = 17739.7;
float D     = 18794.5;
float DS    = 19912.1;
float E     = 21096.2;
float F     = 22350.6;
float FS    = 23679.6;
float G     = 25087.7;
float GS    = 26579.5;
float A2    = 28160;
float A2S   = 29834.5;
float B2    = 31608.5;
float C2    = 33488.1;
float C2S   = 35479.4;
float D2    = 37589.1;
float D2S   = 39824.3;
float E2    = 42192.3;
float F2    = 44701.2;
float F2S   = 47359.3;
float G2    = 50175.4;
float G2S   = 53159;
float A3    = 56320;

//rhythm values
int wh = 1024;
int h  = 512;
int dq = 448;
int q = 256;
int qt = 170;
int de = 192;
int e = 128;
int et = 85;
int dsx = 96;
int sx = 64;
int thx = 32;

// major scale just for demo, hack this

float majScale[] = {
C, E,  G,  A2,   C2,  E2,  G2,  A3};

void setup() {
Serial.begin(9600);
}

void loop(){
for(i= 0; i<=11; i++){
ps = (float)i / 12;         // choose new transpose interval every loop
for(x= 0; x<=8; x++){        // 8 is the number of notes played
noteval = (majScale[x] / 64) * pow(2,ps);    // transpose scale up 8 tones
// pow function generates transposition
// eliminate ” * pow(2,ps) ” to cut out transpose routine

dur = 400; // how long each note is played
freqout((int)noteval, dur);

delay(10);
}
}
}

void freqout(int freq, int t)  // freq in hz, t in ms
{
int hperiod;                               //calculate 1/2 period in us
long cycles, i;
pinMode(piezoPin, OUTPUT);  // turn on output pin
pinMode(outpin, OUTPUT);
pinMode(speaker3, OUTPUT);

hperiod = (500000 / freq) – 7;             // subtract 7 us to make up for digitalWrite overhead

cycles = ((long)freq * (long)t) / 1000;    // calculate cycles – changes length of note

for (i=100; i<= cycles; i++){              // play note for t ms – number makes a delay at end of note compilation
digitalWrite(outpin, HIGH);
delayMicroseconds(hperiod);
digitalWrite(outpin, LOW);
delayMicroseconds(hperiod – 1);     // – 1 to make up for digitaWrite overhead
}

for (i=0; i<= cycles; i++){              // play note for t ms
digitalWrite(piezoPin, HIGH);
delayMicroseconds(hperiod);
digitalWrite(piezoPin, LOW);
delayMicroseconds(hperiod – 1);     // – 1 to make up for digitaWrite overhead
}

for (i=50; i<= cycles; i++){              // play note for t ms – number makes a delay at end of note compilation
digitalWrite(speaker3, HIGH);
delayMicroseconds(hperiod);
digitalWrite(speaker3, LOW);
delayMicroseconds(hperiod – 1);     // – 1 to make up for digitaWrite overhead
}
//pinMode(outpin, INPUT);                // shut off pin to avoid noise from other operations

}

__________________________

Arduino code for 3 speaker annoyance:
__________________________

#define piezoPin 10 // audio out to speaker or amp
#define outpin 9
#define speaker3 3

void setup(void){
//Set the sound out pin to output mode
pinMode(piezoPin,OUTPUT);
pinMode(outpin,OUTPUT);
pinMode(speaker3,OUTPUT);
}

void loop(void){
digitalWrite(piezoPin,HIGH);
delayMicroseconds(900);
digitalWrite(piezoPin,LOW);

digitalWrite(outpin,HIGH);
digitalWrite(outpin,LOW);

digitalWrite(speaker3,HIGH);
digitalWrite(speaker3,LOW);
}

Advertisements

5 thoughts on “Arduino – 3 speaker output

  1. First article I’ve read on multiple outputs to speakers. I would like to design a fence alarm system, with a speaker on each post, to send a sound alarm along a fence to each post in succession. Deer would hear it coming and flee.

  2. Were you able to get the speakers to play chords? I am attempting to do the same thing but I don’t really have a coding background so it’s a nightmare…

  3. i heard you can approximate a chord by connecting the output square waves in a resistor ladder because trying to connect 2 outputs to the same speaker distorts the sound

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s