Example Code for Arduino-Record Voice

Last revision 2025/12/27

This guide explains how to record voice using Arduino with step-by-step instructions and sample code.

Hardware Preparation

Software Preparation

Sample Code

Record your voice.

#include <SPI.h>
#include <Wire.h>
#include <SD.h>
#include <WAV.h>

// set up variables using the SD utility library functions:
Sd2Card card;

const int chipSelect = 30;  ///< assign sd card chipselect pin

void setup()
{
    // Open serial communications and wait for port to open:
    Serial.begin(115200);
    while (!Serial) {
        ; // wait for serial port to connect. Needed for Leonardo only
    }

    while(1){
        Serial.print("\nInitializing SD card...");

        // we'll use the initialization code from the utility libraries
        // since we're just testing if the card is working!
        if (!card.init(SPI_HALF_SPEED, chipSelect)) {
            Serial.println("initialization failed. Things to check:");
            Serial.println("* is a card inserted?");
            Serial.println("* is your wiring correct?");
            Serial.println("* did you change the chipSelect pin to match your shield or module?");
            delay(2000);
            continue;
        } else {
            Serial.println("Wiring is correct and a card is present.");
            if (!SD.begin(chipSelect)) {
                Serial.println("Card failed, or not present");
                delay(2000);
                continue;
            }
            break;
        }
    }
}

void loop()
{
    int ret;
    ret = wav.record("rec.wav");
    unsigned int start = millis();
    while(ret == WAV_ENCODING){
        ret = wav.encode();
        if(millis() - start > 5000){
            wav.stop();
            ret = WAV_ENCODE_END;
        }
    };
    while(1);
}

Result

Record the voice for 5 seconds.

Was this article helpful?

TOP