Skip to content

Commit 6ec3ebc

Browse files
authored
Update Simple_tone.ino
Fixed I2S tone issue.
1 parent 9eb7dc6 commit 6ec3ebc

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

libraries/ESP_I2S/examples/Simple_tone/Simple_tone.ino

+8-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
2nd September 2021
2525
Lucas Saavedra Vaz (lucasssvaz)
2626
22nd December 2023
27+
anon
28+
28nd December 2024
2729
*/
2830

2931
#include <ESP_I2S.h>
@@ -36,17 +38,19 @@ i2s_data_bit_width_t bps = I2S_DATA_BIT_WIDTH_16BIT;
3638
i2s_mode_t mode = I2S_MODE_STD;
3739
i2s_slot_mode_t slot = I2S_SLOT_MODE_STEREO;
3840

39-
const int halfWavelength = (sampleRate / frequency); // half wavelength of square wave
41+
const int halfWavelength = (sampleRate / frequency / 2); // half wavelength of square wave
4042

4143
int32_t sample = amplitude; // current sample value
42-
int count = 0;
44+
unsigned int count = 0;
4345

4446
I2SClass i2s;
4547

4648
void setup() {
4749
Serial.begin(115200);
4850
Serial.println("I2S simple tone");
4951

52+
i2s.setPins(5, 25, 26);
53+
5054
// start I2S at the sample rate with 16-bits per sample
5155
if (!i2s.begin(mode, sampleRate, bps, slot)) {
5256
Serial.println("Failed to initialize I2S!");
@@ -61,7 +65,9 @@ void loop() {
6165
}
6266

6367
i2s.write(sample); // Right channel
68+
i2s.write(sample >> 8);
6469
i2s.write(sample); // Left channel
70+
i2s.write(sample >> 8);
6571

6672
// increment the counter for the next sample
6773
count++;

0 commit comments

Comments
 (0)