Delaynie’s Status Report for 03/28/2026

SPI Struggles

  • This board uses a different set of SPI pins from the default Arduino SPI pins. Because of how the Arduino library and examples are written for SPI, and specifically, SD card interfacing, it makes this a little more difficult.
    • A lot of “out-of-the-box” example code fails when I try to run it.
  • After a lot of code failing, I decided to break the problem down further: first make sure the SPI pins that I want are indeed the ones being used. And only after that’s been confirmed worry about SD card interfacing.
  • The easiest way to do this is to simply have the ESP write a byte to the SPI bus, and look at the output with an oscilloscope. Luckily, there is a SPI.transfer(uint8_t) function provided by the library.
  • To make things even easier, I have the byte increment every cycle so that I know that what I’m looking at is in fact the data I’m trying to send and not something else. See below for full code.
  • This worked, click here to see a video of the oscilloscope measurements.
  • Also, I noticed my silkscreen labels are wrong—SPI CIPO and SPI_SCLK are swapped.

SPI Testbench Code

#include <Arduino.h>
#include <SPI.h>
#include <SD.h>

#define SD_COPI 38
#define SD_CIPO 39
#define SD_SCLK 40
#define SD_CS 41

void setup()
{
  Serial.begin(115200);
  delay(500);

  SPI.begin(SD_SCLK, SD_CIPO, SD_COPI, SD_CS);
}

uint8_t c = 0x00;

void loop()
{
  uint8_t x = SPI.transfer(c++);
  Serial.printf("transfer %x\\n", x);
  delay(500);
}

SD Card

  • Once I determined that I could interface with the correct set of pins for SPI, I started interfacing with the SD card.
  • I modified the code to see if I can successfully talk to the SD card. See below for code.
  • At first it failed. Specifically, somewhere in the card mounting, it failed. I formatted the micro SD card with my laptop, and tried again. It worked this time. I would have guessed that the library would be able to handle something like this, but I guess not. It may also be worth noting that this card was fresh out of the box.

SD Card Initialization Code

#include <Arduino.h>
#include <SPI.h>
#include <SD.h>

#define SD_COPI 38
#define SD_CIPO 39
#define SD_SCLK 40
#define SD_CS 41

void setup()
{
  Serial.begin(115200);
  delay(500);

  SPI.begin(SD_SCLK, SD_CIPO, SD_COPI, SD_CS);

  if (!SD.begin(SD_CS))
  {
    while (true)
    {
      Serial.println("failed to begin SD card.");
      delay(500);
    }
  }
  else
  {
    while (true)
    {
      Serial.println("success");
      delay(500);
    }
  }
}

uint8_t c = 0x00;

void loop()
{
  // uint8_t x = SPI.transfer(c++);
  // Serial.printf("transfer %x\\n", x);
  // delay(500);
}
  • I then moved on to reading and writing text to and from files. That test was a success.

 

Leave a Reply

Your email address will not be published. Required fields are marked *