添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

I have been trying to get help or example sketches on how to use I2S to output audio using in-built DAC pins 25 and 26. I can get dacWrite to work on inbuilt DAC but cannot get I2S itself configured to output to the DAC pins analog 25 and 26. dacWrite is OK for low frequency audio but I gather that using I2S functions would be faster. any help appreciated please

This is a simple sketch from the Arduino Library in the IDE install....

#include <I2S.h>
const int frequency = 440; // frequency of square wave in Hz
const int amplitude = 100; // 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
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 RIGH followed by LEFT
//i2s_mode_t mode = I2S_RIGHT_JUSTIFIED_MODE; // I2S decoder is needed
void setup() {
  Serial.begin(115200);
  Serial.println("I2S simple tone");
  // start I2S at the sample rate with 16-bits per sample
  if (!I2S.begin(mode, sampleRate, bps)) {
    Serial.println("Failed to initialize I2S!");
    while (1); // do nothing
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
      I2S.write(sample); // Right channel
      I2S.write(sample); // Left channel
    }else if(mode == I2S_RIGHT_JUSTIFIED_MODE || mode == I2S_LEFT_JUSTIFIED_MODE){
      // write the same only once - it will be automatically copied to the other channel
      I2S.write(sample);
    // increment the counter for the next sample
    count++;

If I pick the mode option "i2s_mode_t mode = ADC_DAC_MODE" I don't get anything out of DAC pins 25 or 26.
If I pick the philips option I get what looks like the clock and data pulses as if on I2S pins.
But 25 and 26 should be analog audio from the inbuilt DAC.....?

Are you using the ESP32's I2S API,
https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/i2s.html

Yes, I have looked through that document, The problem I have is it doesn't relate to Arduino IDE type of commands. The example I posted is from Arduino library ESP examples so must be a config problem with this sketch. The ESP has builtin DAC analog out on pins 25 and 26. and I can get dacWrite to work to these pins with analog out.... dacWrite(25, sample); etc The sketch uses I2S.write(sample);. If I set "i2s_mode_t _mode = ADC_DAC_MODE; // Audio amplifier is needed " I do not get any output. But if I set to "//i2s_mode_t mode = I2S_PHILIPS_MODE; // I2S decoder is needed " I get very high frequency sine on one pin a low freq square wave modulated with a high freq on the other pin. Looks possibly the the I2S is treating 25 and 26 as clock and data pins for the Philips setting but in the DAC Mode the port in maybe disabled. But I cannot find how to enable it via the Arduino IDE..??

what have you tried?

Most likely the configuration structure is being declared incorrectly for the Arduino IDE.

  gpio_config_t io_cfg = {}; // initialize the gpio configuration structure
  io_cfg.mode = GPIO_MODE_INPUT; // set gpio mode. GPIO_NUM_0 input from water level sensor
  io_cfg.pull_down_en = GPIO_PULLDOWN_ENABLE; // enable pull down
  io_cfg.pin_bit_mask = ( (1ULL << GPIO_NUM_0) | (1ULL << GPIO_NUM_18) ); //bit mask of the pins to set, assign gpio number to be configured
  gpio_config(&io_cfg); // configure the gpio based upon the parameters as set in the configuration structure

The configuration structures are declared a bit differently, as exampled from the above code.

from looking at the examples the adruino ide would look something

#include "driver/i2s_std.h"
i2s_chan_handle_t tx_handle;
/* Get the default channel configuration by helper macro.
 * This helper macro is defined in 'i2s_common.h' and shared by all the i2s communication mode.
 * It can help to specify the I2S role, and port id */
i2s_chan_config_t chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG(I2S_NUM_AUTO, I2S_ROLE_MASTER);
/* Allocate a new tx channel and get the handle of this channel */
i2s_new_channel(&chan_cfg, &tx_handle, NULL);
voided setup()
i2s_std_config_t std_cfg = {};
std_cfg. lk_cfg = I2S_STD_CLK_DEFAULT_CONFIG(48000);

and so on and so forth. I'd do one configuration statement at a time to work out the proper Adruino IDE syntax.

I have looked at some of your configs but cannot see how to put them in arduino.
The syntax is so much different as in orig sketch. endless compile errors.
I browsed more of the docs and came across these pages at this link.....
https://docs.espressif.com/projects/arduino-esp32/en/latest/api/i2s.html
It seems to use and describe the syntax that is in the sketch but I tried some options and no different than picking the options in the sketch. I now get a low freq 200 Hz on pin 26 and a very high inaudable freq on pin25 (looking at it on Audacity) when I pick Philips mode.. The sketch comes with the arduino2.0.3 installed library, I wond has anyone got it to work in internal DAC...?

PilnyTomas/issues/blob/4b5315b5547165c9f080fd3e993dce24bf0fe7bf/issue_7252B/issue_7252B.ino

// Issue 7252 https://github.com/espressif/arduino-esp32/issues/7252
// Code from user, modified to work
#include "esp_err.h"
#include "esp_log.h"
#include "driver/i2s.h"
#define BYTES_TO_WRITE 256
static uint8_t data[BYTES_TO_WRITE] =
   // Sin wave con 256 samples.
   0x80, 0x83, 0x86, 0x89, 0x8C, 0x8F, 0x92, 0x95, 0x98, 0x9B, 0x9E, 0xA1, 0xA4, 0xA7, 0xAA, 0xAD, 0xB0, 0xB3, 0xB6, 0xB9,
   0xBB, 0xBE, 0xC1, 0xC3, 0xC6, 0xC9, 0xCB, 0xCE, 0xD0, 0xD2, 0xD5, 0xD7, 0xD9, 0xDB, 0xDE, 0xE0, 0xE2, 0xE4, 0xE6, 0xE7,
   0xE9, 0xEB, 0xEC, 0xEE, 0xF0, 0xF1, 0xF2, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFB, 0xFB, 0xFC, 0xFD, 0xFD, 0xFE,
   0xFE, 0xFE, 0xFE, 0xFE, 0xFF, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFD, 0xFD, 0xFC, 0xFB, 0xFB, 0xFA, 0xF9, 0xF8, 0xF7, 0xF6,
   0xF5, 0xF4, 0xF2, 0xF1, 0xF0, 0xEE, 0xEC, 0xEB, 0xE9, 0xE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