A simple example that shows how to record a WAV file to an SD Card on the ESP32

Overview

ESP32 SDCard Wave File Example

This code shows how to record a WAV file to an SD Card attached to an ESP32 and then play it back.

There's an explanatory video here

Demo Video

There are two projects - one using Arduino and one using the Esspresif IDF. Both of these projects use PlatformIO to build.

Edit config.h for your device. In particular, you will probably want to change the button that is used to trigger recording and playback.

// record button
#define GPIO_BUTTON GPIO_NUM_23

Usage

To record audio press and hold the button. Recording will stop when you release the button.

The SD Card should contain a file called test.wav after a successful recording.

To playback the recording push the button.

Once the playback has finished the device will switch back to waiting for a new recording to be made.

Arduino

The arduino-wav-sdcard folder has code that works on the current release of Arduino for the ESP32.

IDF

The idf-wav-sdcard folder has code that works with the latest IDF release.

Issues
  • BT sink integration + failed sd card initialization

    BT sink integration + failed sd card initialization

    Hi,

    1. Is it possible to integrate the recording feature to a BT sink? I'm currently working on integrating this to a BT sink, where instead of the I2S mic, the audio data will be coming from a BT source.

    However I'm still having a hard time on making your code work on my esp32 board, I still got problems with initialization of the sd card. image I'm using a microSD card reader module with SanDisk Ultra (16GB) MicroSD HCI formatted as FAT32. 2. Do you have any idea/tips on how to debug this?

    I tried using the SD card with other sound recording code and it worked, still puzzled on why I can't initialize the SD card using your code.

    Warm regards, rmond

    opened by rmond18 8
  • SD Card Wiring with pull-up Resistors and code issue

    SD Card Wiring with pull-up Resistors and code issue

    I'm trying to run this project.

    First When trying to write to the SD Card using the idf code, it always fails to initialize the SD card. It is as HC card though. Did you use any pull-up resistors? If yes, can you provide your connection scheme?

    Furthermore, I would like to use the internal ADC. After commenting //#define USE_I2S_MIC_INPUT in config.h

    there will be two bugs in line 97 of main.cpp 97: I2SSampler *m_input = new ADCSampler(ADC_UNIT_1, ADC1_CHANNEL_7);

    1. shouldn't *m_input be *input?
    2. ADCSampler requires an additional parameter, is this i2s_adc_config?

    I would propose the solution following solution, for which I'm still not sure since I cannot test it as long as I can have the SD card working

    I2SSampler *input = new ADCSampler(ADC_UNIT_1, ADC1_CHANNEL_7, i2s_adc_config);

    opened by dtiziano 8
  • SDCard mount error;

    SDCard mount error;

    Hi! I tried to use your IDF version code and can't mount sd card

    I (343) app: Starting up I (343) app: Mounting SDCard on /sdcard I (353) SDC: Initializing SD card I (353) gpio: GPIO[5]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0 E (403) sdmmc_sd: sdmmc_init_sd_if_cond: send_if_cond (1) returned 0x108 E (403) vfs_fat_sdmmc: sdmmc_card_init failed (0x108). I (403) gpio: GPIO[5]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0 E (413) SDC: Failed to initialize the card (ESP_ERR_INVALID_RESPONSE). Make sure SD card lines have pull-up resistors in place. I (423) app: Creating microphone

    I've redefined sd card pins on config.h

    #define PIN_NUM_MISO GPIO_NUM_19 #define PIN_NUM_MOSI GPIO_NUM_23 #define PIN_NUM_CLK GPIO_NUM_18 #define PIN_NUM_CS GPIO_NUM_5

    and temporarily disable output logic and speaker pins...

    maybe you can provide some help? thanks

    opened by 7doctor7 3
  • converting data from int32_t to int16_t

    converting data from int32_t to int16_t

    Hi Atomic14 I've got a question about what is happening in line 35 of https://github.com/atomic14/esp32_sdcard_audio/blob/main/idf-wav-sdcard/lib/audio_input/src/I2SMEMSSampler.cpp where the following code is executed.

    samples[i] = (raw_samples[i] & 0xFFFFFFF0) >> 11;

    where raw_samples is a int32_t, which is filled with values that can be as high as the microphone resolution, e.g. 24 bit for the ICS43434. Am I right, assuming that operating & 0xFFFFFFF0 on values values confined to 24bit results in unchanged values, and is thus a useless operation? I'm then not sure why the left shift of 11 bits? Here we would like to end having "samples" of the size of 16bit, thus I don't understand why shifting right by 11, shouldn't this be either >> 32-16=16 or 32-24=8?

    Thanks a lot

    Tiziano

    opened by dtiziano 3
  • Failed to initialize the card (ESP_ERR_INVALID_RESPONSE)

    Failed to initialize the card (ESP_ERR_INVALID_RESPONSE)

    Hi, I have flash( arduino-wav-sdcard) code at ESP32 Dev-Kit V1(ESP-WROOM-32), I try same setting for SPI pins mentioned in this code config.h file. #define PIN_NUM_MISO GPIO_NUM_4 #define PIN_NUM_CLK GPIO_NUM_14
    #define PIN_NUM_MOSI GPIO_NUM_15 #define PIN_NUM_CS GPIO_NUM_25 Its not worked for me, giving error E (324) sdmmc_sd: sdmmc_init_sd_if_cond: send_if_cond (1) returned 0x108 E (325) SDC: Failed to initialize the card (ESP_ERR_INVALID_RESPONSE). Make sure SD card lines have pull-up resistors in place.

    I study SPI Pins for ESP-WROOM-32 that are (VSPI (MISO, CLK, MOSI, CS)=(19, 18, 23, 5)) (HSPI (MISO, CLK, MOSI, CS)=(12, 14, 13, 15)) Also test using VSPI and HSPI pins, also not work for me. I have used Kingston micro SD adopter and try with 32gb SDHC.

    Kindly help me, I do not know what wrong did I do. Thanks in advance.

    opened by waqas-subhani 2
  • Follow up questions (memory allocation + I2S mode)

    Follow up questions (memory allocation + I2S mode)

    Hi, it's me again, I have some follow up questions on how you written the samples to the SD Card:

    1.1. Why 1024 is multiplied to the size of the memory allocation? https://github.com/atomic14/esp32_sdcard_audio/blob/9529f60639c22a53948138ab6a0789c70480fd0e/arduino-wav-sdcard/src/main.cpp#L24

    1.2. Is it because of the dma buffer length? https://github.com/atomic14/esp32_sdcard_audio/blob/9529f60639c22a53948138ab6a0789c70480fd0e/arduino-wav-sdcard/src/config.cpp#L12

    1. When writing data to SD card, is the mode in i2s config should be I2S_MODE_RX? (I'm having trouble since I'm integrating your code on how you written the samples to the SD Card with this BT sink where the mode for i2s is TX)

    image

    Thank you very much!

    opened by rmond18 1
  • @cgreening I don't want to use the push button to record. what needs to be done.

    @cgreening I don't want to use the push button to record. what needs to be done.

    @cgreening I don't want to use the push button to record. what needs to be done.

    Originally posted by @Yatinbaluja in https://github.com/atomic14/esp32_sdcard_audio/issues/1#issuecomment-846150096

    opened by cgreening 1
  • Adding Volume Control either digitally or using analog input

    Adding Volume Control either digitally or using analog input

    I am attempting to use a potentiometer to add analog volume control to the i2s play back by modifying the play function like below however this is causing the esp32 to panic do you have a better idea of how this could be implemented digitally?:

    void play(Output *output, const char *fname)
    {
      int16_t *samples = (int16_t *)malloc(sizeof(int16_t) * 1024);
      // open the file on the sdcard
      FILE *fp = fopen(fname, "rb");
      // create a new wave file writer
      WAVFileReader *reader = new WAVFileReader(fp);
      ESP_LOGI(TAG, "Start playing");
      output->start(reader->sample_rate());
      ESP_LOGI(TAG, "Opened wav file");
      // read until theres no more samples
      int16_t samples_read_signed;
      uint16_t i;
    
      while (true)
      {
        int16_t samples_read = reader->read(samples, 1024);
        if (samples_read == 0)
        {
          break;
        }
        
        Volume = float(analogRead(PIN_VOL_ADC)) / 2047;
    
        for(i=0;i<samples_read;i+=2)  // We step 2 bytes at a time as we're using 16bits per channel
       {         
         samples_read_signed=*((int16_t *)(samples_read+i));   // Get the Byte address, convert to an int pointer, then get contents of that address as an int 
         samples_read_signed=samples_read_signed*Volume;         // multiply by the volume - a value that will be between 0 and 1, 1 would be full vol    
          *((int16_t *)(samples_read+i))=samples_read_signed;   // Store back in the memory location we got the sample from
          
    
        }
        
    
    
        ESP_LOGI(TAG, "Read %d samples", samples_read);
        output->write(samples, samples_read);
        ESP_LOGI(TAG, "Played samples");
      }
      // stop the input
      output->stop();
      fclose(fp);
      delete reader;
      free(samples);
      ESP_LOGI(TAG, "Finished playing");
    }
    
    opened by markirwin 1
  • Button set-up

    Button set-up

    Hi, may I ask how did you wire your button to the ESP32?

    I tried applying this example I saw in youtube: image however I'm still waiting for the resistor I ordered since 7.5k ohm is the lowest resistor value I have at the moment. May I ask if this implementation okay?

    Thank you!

    opened by rmond18 1
  • Update SDCard.cpp

    Update SDCard.cpp

    Adding a line to specify the SDSPI max frequency. Came across an issue where my Lolin32 esp32 couldn't detect the SD card on the default frequency so had to specify to a lower one to make it work. The choice of frequency came from seeing what is used in the SDFat Library for ESP's.

    I suspect the issue is more to do with the breakout board used for the micro SD than the esp32 or SD cards themselves. Limiting it to the lower speed should make it compatible with most (hopefully all) boards and can be increased if desired.

    opened by Fionnoch 0
  • Using Arduino SD library;

    Using Arduino SD library;

    Hi! Maybe you can provide some help for using the Arduino-provided SD library for saving WAV files to SD card? Because now I can't do it with stdio.h in yours WAVFileWriter and Arduino SD and FS libs. Thank in advance.

    opened by 7doctor7 0
Owner
atomic14
atomic14
Command line C++ and Python VSTi Host library with MFCC, FFT, RMS and audio extraction and .wav writing.

______ _ ___ ___ | ___ \ | | | \/ | | |_/ /___ _ __ __| | ___ _ __| . . | __ _ _ __

Leon Fedden 283 Aug 1, 2022
This project shows how to interface Nokia 5110 LCD with Esp32 module to show current prices of any cryptocurrency like Bitcoin, Dogecoin, etc

ESP32 Cryptocurreny Ticker Introduction This project shows how to interface Nokia 5110 LCD with Esp32 module to show current prices of any cryptocurre

Aniket Katkar 20 Jun 16, 2022
ESP32-Skid-Steer - Bruder Catepillar Skid Steer model converted to RC, controlled by an ESP32 with 2 analog joysticks and a receiver that is an ESP32 on the model.

ESP32-Skid-Steer Bruder Catepillar Skid Steer model converted to RC, controlled by an ESP32 with 2 analog joysticks and a receiver that is an ESP32 on

null 5 Feb 4, 2022
Example-application - Example out-of-tree application that is also a module

Zephyr Example Application This repository contains a Zephyr example application. The main purpose of this repository is to serve as a reference on ho

Zephyr Project 68 Aug 2, 2022
This is a tool for software engineers to view,record and analyse data(sensor data and module data) In the process of software development.

![Contributors][Huang Jianyu] Statement 由于工具源码在网上公开,除使用部分开源项目代码外,其余代码均来自我个人,工具本身不包含公司的知识产权,所有与公司有关的内容均从软件包中移除,软件发布遵循Apache协议,任何人均可下载进行修改使用,如使用过程中出现任何问

HuangJianyu 34 May 5, 2022
PL/0 Compiler Extension(including repeat, for, case, function, real, record and so on)

EX-PL0 PL/0 Compiler Extension(including repeat, for, case, function, real, record and so on) Structure src/PL source code of PL compiler; src/interpr

Chunshuo Qiu 16 Jul 21, 2022
EyeLog Can Record And Steal Consecutive Keystrokes (and much more) That The User Enters On A Device

What This EyeLog EyeLog Is A Keylogger Can Record And Steal Consecutive Keystrokes (and much more) That The User Enters On A Device How To Setup EyeLo

sami 2 Dec 28, 2021
Anotter USB temperature logger that can record up to four channels with thermocouple or NTCs connected via CDC directly or SCPI to USB.

temperature-logger Anotter USB temperature logger that can record up to four channels with thermocouple or NTCs connected via CDC directly or SCPI to

Jana Marie Hemsing 47 Apr 6, 2022
This repository shows my all (maybe all) solved problem (CodeForces & AtCoder) and their description with my Codes!!!

Submissions Auto-generated with ❤ using Harwest Introduction A repository to keep track of problem solving practice, containing solutions from platfor

Obaydullah Khan  3 Aug 4, 2022
Plays native alert sound and shows native dialogs/alerts in your Flutter app.

flutter_platform_alert 2021 © Weizhong Yang a.k.a zonble. A simple plugin to present native alerts, including playing alert sounds and showing alert d

Weizhong Yang a.k.a zonble 55 Jul 23, 2022
Well-organized, commented and documented sample project that shows the basic functionalities of the 42's mlx library.

miniLibX sample | slucas-s I developed this sample project to play around with the basic functionalities of the miniLibX, the simple graphics library

S. Lucas Serrano 40 Jul 28, 2022
Shows different icons for 64 and 32-bit DLLs. Register with RegSvr32 to install

DllIconHandler This project demonstrates how to create a Shell Icon Handler, that is loaded by Explorer.exe. An icon handler can show different icons

Pavel Yosifovich 23 Aug 7, 2022
ESP32 + Arducam Mini 2MP Plus Edge Impulse Example

Minimal example code for running an Edge Impulse image classification network with the ESP32, ArduCAM, and PlatformIO

David Schwarz 6 Apr 23, 2022
ESP32, ESP8266 based MIDI Organ using the ML_SynthTools library (little example arduino project)

esp32_esp8266_organ ESP32, ESP8266 based MIDI Organ using the ML_SynthTools library (little example arduino project) link to the video The required li

Marcel 13 Jul 16, 2022
ESP32, ESP8266 based MIDI Organ using the ML_SynthTools library (little example arduino project)

ml_synth_organ_example MIDI Organ using the ML_SynthTools library (little example arduino project) link to the video This project is an example suppor

Marcel 14 Aug 4, 2022
Example code for collecting weather data from an ESP32 and then uploading this data to InfluxDB in order to create a dashboard using Grafana.

InfluxGrafanaTutorial Example code for collecting weather data from an ESP32 and then uploading this data to InfluxDB in order to create a dashboard u

Michael Klements 5 May 12, 2022
A laser cut Dreamcast Pop'n Music controller and integrated memory card using the Raspberry Pi Pico's Programmable IO

Dreamcast Pop'n Music Controller Using Raspbery Pi Pico (RP2040) Intro This is a homebrew controller for playing the Pop'n Music games on the Sega Dre

null 36 Aug 3, 2022
A FAT filesystem with SPI driver for SD card on Raspberry Pi Pico

no-OS-FatFS-SD-SPI-RPi-Pico Simple library for SD Cards on the Pico At the heart of this library is ChaN's FatFs - Generic FAT Filesystem Module. It a

Carl J Kugler III 97 Aug 1, 2022
An unofficial Realtek PCIe-based card reader driver for macOS

Realtek PCIe Card Reader Driver for macOS Unleash the full potential of your SDXC UHS-I cards Introduction An unofficial macOS kernel extension for Re

FireWolf 130 Aug 1, 2022