Trouble playing WAV on ESP32 using I2S with internal DAC
Posted: Sat Aug 10, 2019 12:33 pm
I'm trying to use I2S and internal DAC to play WAV files from SPIFF on a Heltec WiFi LoRa 32 V2, using the Arduino IDE.
I have an audio amp and an oscilloscope hooked up to DAC2 (pin 25) of the board and I'm not getting any signal. I've simplified the problem by generating a sine wave (as in the ESP-IDF examples). Here's the code:
The code uploads and runs OK but I still get no signal on pin 25. I also looked on pin 26 (DAC1) but that seems to be used by LoRa_IRQ. Can anyone help me out?
I have an audio amp and an oscilloscope hooked up to DAC2 (pin 25) of the board and I'm not getting any signal. I've simplified the problem by generating a sine wave (as in the ESP-IDF examples). Here's the code:
- #include <Streaming.h>
- #include <driver/i2s.h>
- #include "freertos/queue.h"
- #define SAMPLE_RATE (22050)
- #define SAMPLE_SIZE 4000
- #define PI (3.14159265)
- #define I2S_BCK_IO (GPIO_NUM_26)
- #define I2S_WS_IO (GPIO_NUM_25)
- #define I2S_DO_IO (GPIO_NUM_22)
- #define I2S_DI_IO (-1)
- size_t i2s_bytes_write = 0;
- static const int i2s_num = 0;
- int sample_data[SAMPLE_SIZE];
- i2s_config_t i2s_config = {
- .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_DAC_BUILT_IN), // Only TX
- .sample_rate = SAMPLE_RATE,
- .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
- .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT, //2-channels
- .communication_format = (i2s_comm_format_t)I2S_COMM_FORMAT_I2S,
- .intr_alloc_flags = 0,//ESP_INTR_FLAG_LEVEL1
- .dma_buf_count = 8,
- .dma_buf_len = 64,
- .use_apll = false //Interrupt level 1
- };
- i2s_pin_config_t pin_config = {
- .bck_io_num = I2S_BCK_IO,
- .ws_io_num = I2S_WS_IO,
- .data_out_num = I2S_DO_IO,
- .data_in_num = I2S_DI_IO //Not used
- };
- static void setup_sine_wave()
- {
- unsigned int i;
- int sample_val;
- double sin_float;
- size_t i2s_bytes_write = 0;
- for (i = 0; i < SAMPLE_SIZE; i++)
- {
- sin_float = sin(i * PI / 180.0);
- sin_float *= 127;
- sample_val = (uint8_t)sin_float;
- sample_data[i] = sample_val;
- Serial << sample_data[i] << ",";
- delay(1);
- }
- Serial << endl << "Sine wave generation complete" << endl;
- }
- void setup() {
- pinMode(26, OUTPUT);
- Serial.begin(115200);
- i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL);
- //i2s_set_pin(I2S_NUM_0, NULL);
- i2s_set_pin(I2S_NUM_0, &pin_config);
- i2s_set_dac_mode(I2S_DAC_CHANNEL_RIGHT_EN);
- i2s_set_sample_rates(I2S_NUM_0, 22050); //set sample rates
- setup_sine_wave();
- i2s_set_clk(I2S_NUM_0, SAMPLE_RATE, I2S_BITS_PER_SAMPLE_16BIT, I2S_CHANNEL_MONO);
- i2s_write(I2S_NUM_0, &sample_data, SAMPLE_SIZE, &i2s_bytes_write, 500);
- i2s_driver_uninstall(I2S_NUM_0); //stop & destroy i2s driver
- }
- void loop()
- {
- i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL);
- i2s_write(I2S_NUM_0, &sample_data, SAMPLE_SIZE, &i2s_bytes_write, 500);
- delay(100);
- i2s_driver_uninstall(I2S_NUM_0);
- delay(10);
- }