In this quick tutorial, I dive into something that’s been driving me crazy: setting custom SPI and I2C pins on ESP32 boards. If you’re using traditional Arduino ATmega chips, you’re stuck with the default hardware SPI and I2C pins. However, on the ESP32, especially the newer S3 version (hopefully, you’ve upgraded), you can set any pin for SPI or I2C, which is a fantastic feature for custom hardware setups.
The problem is, figuring out how to manually set these pins isn’t well-documented, and I ended up spending hours trying to get it right. After plenty of trial and error, I discovered that the pins are defined in a file named pins_arduino.h. Here’s how to find and modify that file on different platforms:
Locating the Configuration File
- PlatformIO: The file is located under .platformio/packages/framework-arduinoespressif32/variants/[your_board]. This is a hidden file so you’ll need to make sure it’s revealed (see below) his path holds the board-specific settings, similar to what you select in the Arduino IDE.
- Arduino IDE: The equivalent file can be found in the hidden directory Library/Arduino15/packages/esp32/hardware/esp32/[version]/variants/[your_board]. Accessing hidden directories may require showing hidden files (using Command + Shift + . on macOS or enabling hidden files on Windows).
Making Pin Changes
Editing the pins_arduino.h file changes the default pin configurations for that board, affecting all projects using it. This can be annoying when juggling multiple projects, so I came up with a workaround:
1. Copy the folder containing the original pins_arduino.h. 2. Rename it, for example, to “custom_pins.” 3. Point your project to this custom folder by modifying the variant setting in your project’s configuration file (on PlatformIO, update the platformio.ini file; on Arduino, it’s trickier and involves editing boards.txt).
Platform-Specific Challenges
I’ve outlined how to handle these changes in PlatformIO, but the Arduino IDE is a bit more complicated. I experimented with modifying boards.txt without success. If you know how to create a custom board definition on the Arduino IDE, feel free to share your solution.
Conclusion
This method allows you to customize your SPI and I2C pins for specific projects without altering the default settings globally. Hopefully, this guide saves you the frustration I went through. If you found this helpful, please like and subscribe to the channel. There’s more content coming soon. Happy making!