The issue turned out to be a faulty SD card.
I had tried other SD cards but must have used incorrect wiring for them.
EDIT: Here is the updated code:
#define CS 5
#define SCK 18
#define MOSI 23
#define MISO 19
SPIClass spi(VSPI);
int setup_sd_card() {
spi.begin(SCK, MISO, MOSI, CS);
if (!SD.begin(CS,spi,4000000)) {
Serial.println("ERROR: SD Card Mount Failed");
delay(1000);
return -1;
}
Serial.println("Finished initializing SD card. Getting card type...");
uint8_t cardType = SD.cardType();
if(cardType == CARD_NONE){
Serial.println("ERROR: No SD card attached");
delay(1000);
return -1;
}
Serial.print("SD Card Type: ");
if (cardType == CARD_MMC){
Serial.println("MMC");
} else if(cardType == CARD_SD){
Serial.println("SDSC");
} else if(cardType == CARD_SDHC){
Serial.println("SDHC");
} else {
Serial.println("UNKNOWN");
}
Serial.printf("Total space: %lluMB\n", SD.totalBytes() / (1024 * 1024));
Serial.printf("Used space: %lluMB\n", SD.usedBytes() / (1024 * 1024));
Serial.println("SUCCESS! (SD CARD)");
return 1;
}