Skip to content

Working with PlatformIO

Phil Schatzmann edited this page Jun 16, 2024 · 33 revisions

The library has been developed to be used in Arduino, but it can also be used easily in PlatformIO. When

  1. Create a new PlatformIO Project.

  2. Update the platformio.ini file in the root of the project:

[platformio]
description = Audio Example
default_envs = esp32dev

[env:esp32dev]
platform = https://github.com/platformio/platform-espressif32.git
board = esp32dev
framework = arduino
lib_deps = https://github.com/pschatzmann/arduino-audio-tools.git
build_flags = -DCORE_DEBUG_LEVEL=5 -Wno-unused-variable -Wno-unused-but-set-variable -Wno-unused-function -Wno-format-extra-args 
monitor_speed = 115200
monitor_filters = esp32_exception_decoder

Please note that:

[env:pico]
platform = https://github.com/maxgerhardt/platform-raspberrypi.git
board = pico
framework = arduino
board_build.core = earlephilhower
  • you might need to add additional dependencies e.g. lib_deps = https://github.com/pschatzmann/arduino-audio-tools, https://github.com/pschatzmann/arduino-libhelix, https://github.com/greiman/SdFat
  • to get the best sound quality, I suggest to set build_flags = -DCORE_DEBUG_LEVEL=2 // stands for Warning
  • if you use #platform = espressif32 inline variables are not supported. Change the platforms as indicated above.
  1. Add your desired Arduino sketch into the src directory e.g.
#include <Arduino.h>
#include "AudioTools.h"

typedef int16_t sound_t;                                   // sound will be represented as int16_t (with 2 bytes)
uint16_t sample_rate=44100;
uint8_t channels = 2;                                      // The stream will have 2 channels 
SineWaveGenerator<sound_t> sineWave(32000);                // subclass of SoundGenerator with max amplitude of 32000
GeneratedSoundStream<sound_t> sound(sineWave);   // Stream generated from sine wave
I2SStream out; 
StreamCopy copier(out, sound);                             // copies sound into i2s

// Arduino Setup
void setup(void) {  
  // Open Serial 
  Serial.begin(115200);

  // start the bluetooth
  Serial.println("starting I2S...");
  I2SConfig config = out.defaultConfig(TX_MODE);
  config.sample_rate = sample_rate; 
  config.channels = channels;
  config.bits_per_sample = 16;
  out.begin(config);

  // Setup sine wave
  sineWave.begin(channels, sample_rate, N_B4);
}

// Arduino loop - copy sound to out 
void loop() {
  copier.copy();
}

The file name does not matter but is should have a cpp or ino extension - you could use e.g. simple.ino or main.cpp. You could also take e.g. any sketch from the samples directory.

  1. Compile the project

This step also automatically installs the dependencies into the .pio/libdeps/esp32dev/ directory.

  1. Upload the binary

  2. Open a new Platformio Terminal and confirm that you get log messages.

  3. Make adjustments

  • You can extend the sketch with your own logic
  • You could replace the sketch with any other example
  • After you confirmed that everything is working you should consider to lower the debug level in platformio.ini because a high log level might impact the sound quality!

Final Deployment Comments

I was using the lib_deps functionality. As an alternative you could remove the lib_deps = https://github.com/pschatzmann/arduino-audio-tools and do a git clone https://github.com/pschatzmann/arduino-audio-tools into the lib folder

Debugging

In oder to debug you need to have am USB JTAG adapter which needs to be connected to the microcontroller.

ESP32

ESP32 JTAG Adapter
GND GND
GPIO12 TDI (Test Data In)
GPIO15 TDO (Test Data Out)
GPIO13 TCK (Test Clock)
GPIO14 TMS (Test Mode Select)
n/a TRST (Test Reset) optional

For further information please consult Low-cost ESP32 In-circuit Debugging from Manuel Bl.

Clone this wiki locally