Hi. Compiled this code again yesterday and it works fine for me. I am using a PCM5102 i2s board rather than the MAX …
/*
* seeed_esp32_tone2 (SimpleTone from I2S library)
This example generates a square wave based tone at a specified frequency
and sample rate. Then outputs the data using the I2S interface to a
MAX08357 I2S Amp Breakout board.
I2S Circuit:
* Arduino/Genuino Zero, MKR family and Nano 33 IoT
* MAX08357:
* GND connected GND
* VIN connected 5V
* LRC connected to pin 0 (Zero) or 3 (MKR), A2 (Nano) or 25 (ESP32)
* BCLK connected to pin 1 (Zero) or 2 (MKR), A3 (Nano) or 5 (ESP32)
* DIN connected to pin 9 (Zero) or A6 (MKR), 4 (Nano) or 26 (ESP32)
DAC Circuit:
* ESP32 or ESP32-S2
* Audio amplifier
- Note:
- ESP32 has DAC on GPIO pins 25 and 26.
- ESP32-S2 has DAC on GPIO pins 17 and 18.
- Connect speaker(s) or headphones.
created 17 November 2016
by Sandeep Mistry
For ESP extended
Tomas Pilny
2nd September 2021
*/
// These pins work for XIAO esp32c3 and esp32s3
#define PIN_I2S_FS 2 //LCK - Word Select
#define PIN_I2S_SCK 3 //BCK - Bit Clock
#define PIN_I2S_SD_OUT 4 //DIN - Data stream
#define PIN_I2S_SD 4
#include <I2S.h>
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
const int bps = 16;
const int halfWavelength = (sampleRate / frequency); // half wavelength of square wave
short sample = amplitude; // current sample value
int32_t rightleft; // sample right and left channels
int count = 0;
i2s_mode_t mode = I2S_PHILIPS_MODE; // I2S decoder is needed
// i2s_mode_t mode = ADC_DAC_MODE; // Audio amplifier is needed
// Mono channel input
// This is ESP specific implementation -
// samples will be automatically copied to both channels inside I2S driver
// If you want to have true mono output use I2S_PHILIPS_MODE and interlay
// second channel with 0-value samples.
// The order of channels is RIGHT followed by LEFT
//i2s_mode_t mode = I2S_RIGHT_JUSTIFIED_MODE; // I2S decoder is needed
void setup() {
Serial.begin(115200);
delay(5000);
Serial.println("I2S simple tone");
Serial.println(__DATE__);
Serial.println(__TIME__);
Serial.println(__FILE__);
Serial.println(PIN_I2S_FS); // D0 gpio02 //LCK - Word Select
Serial.println(PIN_I2S_SCK); // D1 gpio03 //BCK - Bit Clock
Serial.println(PIN_I2S_SD_OUT); // D2 gpio04 //DIN - Data stream
Serial.println(PIN_I2S_SD); // D2
Serial.println(sizeof(short)); // check the num of bits in short integer.
// start I2S at the sample rate with 16-bits per sample
I2S.setAllPins(PIN_I2S_SCK, PIN_I2S_FS, PIN_I2S_SD, PIN_I2S_SD_OUT, -1);
if (!I2S.begin(mode, sampleRate, bps)) {
Serial.println("Failed to initialize I2S!");
while (1); // do nothing
}
Serial.println("Starting tone generation");
}
void loop() {
if (count % halfWavelength == 0 ) {
// invert the sample every half wavelength count multiple to generate square wave
sample = -1 * sample;
}
if(mode == I2S_PHILIPS_MODE || mode == ADC_DAC_MODE){ // write the same sample twice, once for Right and once for Left channel
rightleft = sample; // right channel
rightleft = rightleft << 16;
rightleft += sample; // left channel
I2S.write(rightleft);
}
// increment the counter for the next sample
count++;
}