Skip to content

Commit c01b1eb

Browse files
committed
add simple tone example
1 parent eaca4e6 commit c01b1eb

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <I2S.h>
2+
3+
const int frequency = 440; // frequency of square wave in Hz
4+
const int amplitude = 500; // amplitude of square wave
5+
const int sampleRate = 8000; // sample rate in Hz
6+
7+
const int halfWavelength = (sampleRate / frequency); // half wavelength of square wave
8+
9+
short sample = amplitude; // current sample value
10+
int count = 0;
11+
12+
void setup() {
13+
Serial.begin(9600);
14+
Serial.println("I2S simple tone");
15+
16+
if (I2S.begin(I2S_PHILIPS_MODE, sampleRate, 16)) {
17+
Serial.println("Failed to initialize I2S!");
18+
while (1); // do nothing
19+
}
20+
}
21+
22+
void loop() {
23+
if (count % halfWavelength == 0) {
24+
// invert the sample every half wavelength count multiple to generate square wave
25+
sample = -1 * sample;
26+
}
27+
28+
// write the same sample twice, once for left and once for the right channel
29+
I2S.write(sample);
30+
I2S.write(sample);
31+
32+
// increment the counter for the next sample
33+
count++;
34+
}

0 commit comments

Comments
 (0)