Nano ESP32 on bootloop after uploading sketch

I have an Arduino Nano ESP32 with Arduino IDE 2.3.8 on a Linux machine. Whenever I try to upload this sketch:

#include <driver/i2s.h>

#define I2S_WS 7
#define I2S_SCK 8
#define I2S_SD 6

#define I2S_PORT I2S_NUM_0

#define bufferLen 64 
int16_t buff [bufferLen]; //immagazziniamo 16 campioni da 16 bit

//creare struttura tipo i2s config (i2s_config_t offerta dalla libreria )

const i2s_config_t i2s_config = {
    .mode = i2s_mode_t(I2S_MODE_MASTER | I2S_MODE_RX), //il dispositivo si comporta da MASTER e RICEVE i dati (dal mic)
    .sample_rate = 44100, // frequenza di campionamento
    .bits_per_sample = i2s_bits_per_sample_t(16), // quanti bit per campione (16)
    .channel_format = I2S_CHANNEL_FMT_ONLY_LEFT, // solo canale sinistro
    .intr_alloc_flags = 0,
    .dma_buf_count = 8,
    .dma_buf_len = bufferLen, // lunghezzza del buffer
    .use_apll = false
};

// config per impostare i pin (const i2s_pin_config_t offerta dalla libreria)

const i2s_pin_config_t pin_config = {
    .bck_io_num = I2S_SCK,
    .ws_io_num = I2S_WS,
    .data_out_num = -1,
    .data_in_num = I2S_SD
};

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  delay(2000);
  Serial.println("I2s MIC");
  //installiamo configurazioni 

  i2s_driver_install(I2S_PORT, &i2s_config, 0, NULL); //0 e null se non vogliamo una coda di eventi
  i2s_set_pin(I2S_PORT, &pin_config);
  i2s_start(I2S_PORT); //avviamo processore

  delay(1000); //piccolo delay per permettere a tutto di partire

}

void loop() {
  // qui riceviamo i dati

  size_t bytesRicevuti = 0; // quanti bit ricevuti
  esp_err_t res = i2s_read(I2S_PORT, &buff, bufferLen, &bytesRicevuti, portMAX_DELAY); //funzione di ricezione: prende processore, campioni ricevuti, lunghezza del buffer, quanti bit ricevuti, attendi se non ci sono campioni. risultato della lettura è oggetto esp_err_t
  if (res == ESP_OK) { //se res è uguale a esp ok↓
    int n = bytesRicevuti / 16; //andiamo a leggere i campioni. quanti ne abbiamo ricevuti ce lo dice bytesRicevuti diviso per 16
    if (n > 0) { // se il numero è > 0 vuol dire che abbiamo ricevuto qualcosa e possiamo leggerlo quindi processarlo 
      for (int i = 0; i < n, i++;) {
        Serial.println(buff[i]); //li andiamo a stampare (i valori)
      }

    }
  }
}

, which I wrote following this tutorial), the upload fails and the Arduino enters a bootloop state (connecting and disconnecting from the computer each time flashing the rainbow LED). In order to exit this stuck state I have to enter Download mode (purple LED) and re-flash the bootloader with the command
esptool.py --chip esp32s3 --port "/dev/ttyACM0" --before no_reset --baud 115200 write_flash --flash_mode dio --flash_freq 80m --flash_size 16MB 0x0 /tmp/nano32blink16mb.bin
After the command, the Arduino is back to working correctly and I am able to upload other sketches (e.g. the classic LED blink test). If I try to upload the indicted one, however, the bootloop state starts again.

How can I solve this problem? Is there a problem with the sketch?

Thank anybody!!

Welcome to the forum

Please make it easier to help you by posting your sketch here, using code tags when you do

roger

If I run the code on an ESP32 it tells me that the driver has been depreciated.

Try running the FullDuplex.ino I2S example.

How do I load the FullDuplex.ino? sorry for late replying (and my bad knowledge as I'm a complete newbie)!

Click on Files->Examples->I2S->FullDuplex
This is just for debugging to see if the code will compile and upload

okay, I'm trying but in the I2S.setAllPins section the pins are CLK, WS, IN and OUT. On my inmp44 module however the pins are SCK (which I believe to be the CLK), WS (which matches), GND, L/R and VOO (which is the voltage input). How can I assign the correct pins?

Make sure you have selected the Arduino Nano ESP32 and NOT the esp32 version.


Also make sure what you selected for 'Pin numbering' agrees with the code.

VOO is probably actually VCC
Isn't there an output?
Maybe SO or SD?

Yes, there is an SD pinout on the microphone, which goes to pin 6! The point is that VCC (the IN pin I guess) is connected to the 3v3 arduino pin, which does not seem to have a number.

yes, the settings are all correct (Nano selected and Arduino pin numbering)

Well make the connection as specified in the code but forget about the DOUT (26) connection.

okay, so I just remove the fourth number (the OUT pin)?

Don't worry about the microphone connections just yet.
The purpose of this test is to see if the code compiles and uploads.

ok, so I just leave that line commented (as it was originally)?

I complied and uploaded without changing anything from the original example. The code uploaded correctly and the Nano ESP32 now has all the three leds powered on (one orange, one bright-green and one green). I guess this means that the example uploads and works correctly. Still remains the problem of the first code which still breaks it.

It's old code that is not compatible with the Nano ESP32.

oh, this is a pity. Sorrry to hear that, the tutorial's author ensured me it would work on the Nano ESP32 just like on the ESP32 :((

It may if you use the Nano ESP32 default I2s pins.
Try this:

/*
Default I2S pins for Nano ESP32
D7 = SCK
D8 = FS (WS)
D9 = Output
D10 = Input (connect to mic SD)
 */

const i2s_pin_config_t pin_config = {
    .bck_io_num = D7,
    .ws_io_num = D8,
    .data_out_num = -1,
    .data_in_num = D10
};