GPIO Setup For ESP32

Hi all:
Let me first point you here but long story short, my GPIO pins, specifically my SPI clock (SCK) pin was not set to the default in the esp32-C3 datasheet. I then went digiing and found this file on github:


is pins_arduino_h part of arduino_h library or is it loaded as part of the board set-up? Can this file be used to change the gpio behavior? At any rate…at least it answers my question as to why my pins were behaving like they were!


The build system adds the variant folder (as defined in the PlatformIO board definition) to the include paths and the Arduino.h file does

So yes, The pins_arduino.h is autoincluded as part of Arduino.h (and SPI.h etc.) and that fiel does control the standard pinmapping.

With the SPI code in particular however you can easily control the used SPI pins with the SPI.begin(...); call

If you are using a library that in turn uses the SPI library for communication, then you have to look whether you can pass the SPI pins to its constructors (or methods). Sometimes they also allow that custom SPI object / pointers are passed to the class, which you can create and pre-initialize.

On the other hand, it is totally possible to redirect the used the pins_variant.h (and related files in that variant) to your own folder. That’s the entire point of this code

Meaning, you can write in your platformio.ini

board_build.variants_dir = custom_variants
board_build.variant = my_custom_variant

And that will make the builder script use the given base directory and subfolder for the variant instead of framework-arduinpespressif32/variants/lolin_c3_mini. You can easily start off by duplicating those original files inside the project folder’s custom_variants/my_custom_variant folder and adapt the needed standard settings.

1 Like

Max…you are absolutely the best!

Another observation: I am using an spi display. As part of the code the frames per second is printed. I recall a while back trying to use a method similar to the SPI.begin to change the pin assignment but the frames per secon were dramatically reduced. I came across some forum discussions that were about “software spi”(slower) vs “hardware spi” and assumed that is what I was seeing. I will try the spi.begin again to confirm.

On a side note - could I just add #include " pins_arduino.h" in my src folder in my project?

Thanks again!

A quick follow-up. I had some code on esp32-C3 and I wanted to move it over to an esp32-S3 board. I am using an SH1106 display. So I began a new project with an esp32-S3 board and just copied the code over. The display was garbled so I loaded a program that was a simple graphics test and confirmed there was an issue with talking to the display. I am using a graphics library U8g2 and I think the issue is with the default SPI pins. I wanted to check the default pins for the esp32-s3 board. So I used the following code in setup:

Serial.print("MOSI: ");
Serial.print("MISO: ");
Serial.print("SCK: ");
Serial.print("SS: ");

For some reason I cannot get anything to print in the serial monitor except the following header info:

— Terminal on COM8 | 115200 8-N-1
— Available filters and text transformations: colorize, debug, default, direct, esp32_exception_decoder, hexlify, log2file, nocontrol, printable, send_on_enter, time
— More details at Redirecting...
— Quit: Ctrl+C | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H
Build:Mar 27 2021
rst:0x3 (RTC_SW_SYS_RST),boot:0x2b (SPI_FAST_FLASH_BOOT)
Saved PC:0x4037ec34
pro cpu reset by JTAG
mode:DIO, clock div:1
entry 0x403c98d4

Any ideas on what to do next? I was going to try to find pins.arduino.h for this board on my pc just to make sure the right defaults are used.


o.k. the serial monitor was fixed by adding this to the platformio.ini:

build_flags =
and it looks like the default values are what I thought.
MOSI: 11
MISO: 13
SCK: 12
SS: 10

So it may be something with U8g2.