Skip to content

Commit d71135e

Browse files
Fix(I2S example): make fix to the ESP32 I2S simple tone example (#10954)
* made some fix to the ESP32 I2S simple tone example * edit the I2S - simple tone example * edit the I2S - simple tone example * some edit * edit comment * edit * edit * ci(pre-commit): Apply automatic fixes --------- Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent c21ef70 commit d71135e

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

libraries/ESP_I2S/examples/Simple_tone/Simple_tone.ino

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,17 @@
2424
2nd September 2021
2525
Lucas Saavedra Vaz (lucasssvaz)
2626
22nd December 2023
27+
anon
28+
10nd February 2025
2729
*/
2830

2931
#include <ESP_I2S.h>
3032

33+
// The GPIO pins are not fixed, most other pins could be used for the I2S function.
34+
#define I2S_LRC 25
35+
#define I2S_BCLK 5
36+
#define I2S_DIN 26
37+
3138
const int frequency = 440; // frequency of square wave in Hz
3239
const int amplitude = 500; // amplitude of square wave
3340
const int sampleRate = 8000; // sample rate in Hz
@@ -36,17 +43,19 @@ i2s_data_bit_width_t bps = I2S_DATA_BIT_WIDTH_16BIT;
3643
i2s_mode_t mode = I2S_MODE_STD;
3744
i2s_slot_mode_t slot = I2S_SLOT_MODE_STEREO;
3845

39-
const int halfWavelength = (sampleRate / frequency); // half wavelength of square wave
46+
const unsigned int halfWavelength = sampleRate / frequency / 2; // half wavelength of square wave
4047

4148
int32_t sample = amplitude; // current sample value
42-
int count = 0;
49+
unsigned int count = 0;
4350

4451
I2SClass i2s;
4552

4653
void setup() {
4754
Serial.begin(115200);
4855
Serial.println("I2S simple tone");
4956

57+
i2s.setPins(I2S_BCLK, I2S_LRC, I2S_DIN);
58+
5059
// start I2S at the sample rate with 16-bits per sample
5160
if (!i2s.begin(mode, sampleRate, bps, slot)) {
5261
Serial.println("Failed to initialize I2S!");
@@ -60,8 +69,13 @@ void loop() {
6069
sample = -1 * sample;
6170
}
6271

63-
i2s.write(sample); // Right channel
64-
i2s.write(sample); // Left channel
72+
// Left channel, the low 8 bits then high 8 bits
73+
i2s.write(sample);
74+
i2s.write(sample >> 8);
75+
76+
// Right channel, the low 8 bits then high 8 bits
77+
i2s.write(sample);
78+
i2s.write(sample >> 8);
6579

6680
// increment the counter for the next sample
6781
count++;

0 commit comments

Comments
 (0)