Display “HEX” data of audio “WAV” file from SD card using Arduino Mega

After completing the circuit, upload the following program to your Arduino Mega. A file named “wavFile6.WAV” should be there in your SD card.

//#include <VS1003ESP12.h>
//#include <ESP8266WiFi.h>
#include <SPI.h>
#include <SD.h>

const int xcsPin = A3;
const int sdCs = 10;

File myFile;
char fileName[] = "wavFile6.WAV";
byte data[4];


void disableVS1003(void)
{
  digitalWrite(xcsPin, HIGH);   
}

void setup () {

  pinMode(sdCs, OUTPUT);
  //  digitalWrite(sdCs, HIGH);


  // initiate SPI
  SPI.begin();

  // initiate a serial port at 57600
  Serial.begin(115200);

  disableVS1003();

  while(!SD.begin(sdCs))
  {
    Serial.println("SD initialization failed...");
  }

  (myFile = SD.open(fileName, FILE_READ)) ? Serial.println("SD opening successfull") : Serial.println("SD opening failed");

  int colCount = 0;

  Serial.println(myFile.size());

  for(int i=0;i< myFile.size();i++)
  {
    if(colCount % 16 == 0)
    {
      Serial.println(" ");
      colCount = 0;
    }
    
    else if (colCount % 8 == 0)
    {
      Serial.print("  ");
    }
    
    Serial.print("0x");
    Serial.print(myFile.read(), HEX);
    Serial.print(", ");
    colCount++;

    if((i+1) % 512 == 0)
    {
      Serial.println(" ");
      Serial.println(" ");
    }
  }

  myFile.close();
}

void loop() {
  
}

If uploading successful, open your serial monitor. “HEX” data will be displayed as groups of 512 bytes in 32 rows and 16 columns.

0 0 vote
Article Rating

Published by

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
X