Skip to content

Commit cdf3a7c

Browse files
committed
Extended SimpleTone example with DAC settings
1 parent cb6da04 commit cdf3a7c

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

libraries/I2S/examples/SimpleTone/SimpleTone.ino

+21-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
and sample rate. Then outputs the data using the I2S interface to a
44
MAX08357 I2S Amp Breakout board.
55
6-
Circuit:
6+
I2S Circuit:
77
* Arduino/Genuino Zero, MKR family and Nano 33 IoT
88
* MAX08357:
99
* GND connected GND
@@ -12,8 +12,19 @@
1212
* BCLK connected to pin 1 (Zero) or 2 (MKR), A3 (Nano) or 5 (ESP32)
1313
* DIN connected to pin 9 (Zero) or A6 (MKR), 4 (Nano) or 26 (ESP32)
1414
15+
DAC Circuit:
16+
* ESP32 or ESP32-S2
17+
* Audio amplifier
18+
- Note:
19+
- ESP32 has DAC on GPIO pins 25 and 26.
20+
- ESP32-S2 has DAC on GPIO pins 17 and 18.
21+
- Connect speaker(s) or headphones.
22+
1523
created 17 November 2016
1624
by Sandeep Mistry
25+
For ESP extended by
26+
Tomas Pilny
27+
2nd September 2021
1728
*/
1829

1930
#include <I2S.h>
@@ -27,12 +38,16 @@ const int halfWavelength = (sampleRate / frequency); // half wavelength of squar
2738
short sample = amplitude; // current sample value
2839
int count = 0;
2940

41+
//i2s_mode_t mode = I2S_PHILIPS_MODE;
42+
i2s_mode_t mode = I2S_ADC_DAC;
43+
44+
3045
void setup() {
3146
Serial.begin(115200);
3247
Serial.println("I2S simple tone");
3348

3449
// start I2S at the sample rate with 16-bits per sample
35-
if (!I2S.begin(I2S_PHILIPS_MODE, sampleRate, bps)) {
50+
if (!I2S.begin(mode, sampleRate, bps)) {
3651
Serial.println("Failed to initialize I2S!");
3752
while (1); // do nothing
3853
}
@@ -45,9 +60,11 @@ void loop() {
4560
sample = -1 * sample;
4661
}
4762

48-
// write the same sample twice, once for left and once for the right channel
49-
I2S.write(sample);
5063
I2S.write(sample);
64+
// write the same sample twice, once for left and once for the right channel
65+
if(mode == I2S_PHILIPS_MODE){
66+
I2S.write(sample);
67+
}
5168

5269
// increment the counter for the next sample
5370
count++;

0 commit comments

Comments
 (0)