diff --git a/libraries/ESP_I2S/examples/Simple_tone/Simple_tone.ino b/libraries/ESP_I2S/examples/Simple_tone/Simple_tone.ino index 935aa4bc50f..8ade700f647 100644 --- a/libraries/ESP_I2S/examples/Simple_tone/Simple_tone.ino +++ b/libraries/ESP_I2S/examples/Simple_tone/Simple_tone.ino @@ -24,10 +24,17 @@ 2nd September 2021 Lucas Saavedra Vaz (lucasssvaz) 22nd December 2023 + anon + 10nd February 2025 */ #include +// The GPIO pins are not fixed, most other pins can be used for the I2S function. +#define I2S_LRC 25 +#define I2S_BCLK 5 +#define I2S_DIN 26 + const int frequency = 440; // frequency of square wave in Hz const int amplitude = 500; // amplitude of square wave const int sampleRate = 8000; // sample rate in Hz @@ -36,10 +43,10 @@ i2s_data_bit_width_t bps = I2S_DATA_BIT_WIDTH_16BIT; i2s_mode_t mode = I2S_MODE_STD; i2s_slot_mode_t slot = I2S_SLOT_MODE_STEREO; -const int halfWavelength = (sampleRate / frequency); // half wavelength of square wave +const int halfWavelength = sampleRate / frequency / 2; // half wavelength of square wave int32_t sample = amplitude; // current sample value -int count = 0; +unsigned int count = 0; I2SClass i2s; @@ -47,10 +54,13 @@ void setup() { Serial.begin(115200); Serial.println("I2S simple tone"); + i2s.setPins(I2S_BCLK, I2S_LRC, I2S_DIN); + // start I2S at the sample rate with 16-bits per sample if (!i2s.begin(mode, sampleRate, bps, slot)) { Serial.println("Failed to initialize I2S!"); - while (1); // do nothing + while (1) + ; // do nothing } } @@ -60,8 +70,13 @@ void loop() { sample = -1 * sample; } - i2s.write(sample); // Right channel - i2s.write(sample); // Left channel + // Right channel, low 8 bit then hight 8 bit + i2s.write(sample); + i2s.write(sample >> 8); + + // Left channel, low 8 bit then hight 8 bit + i2s.write(sample); + i2s.write(sample >> 8); // increment the counter for the next sample count++;