diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index b06629896ed..c92e9ad0dce 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -34,6 +34,7 @@ repos:
     hooks:
       - id: clang-format
         types_or: [c, c++]
+        exclude: ^.*\/build_opt\.h$
   - repo: https://github.com/psf/black-pre-commit-mirror
     rev: "22.10.0"
     hooks:
diff --git a/cores/esp32/esp32-hal-gpio.c b/cores/esp32/esp32-hal-gpio.c
index b433adcc7fc..98dc26599be 100644
--- a/cores/esp32/esp32-hal-gpio.c
+++ b/cores/esp32/esp32-hal-gpio.c
@@ -17,6 +17,16 @@
 #include "hal/gpio_hal.h"
 #include "soc/soc_caps.h"
 
+// RGB_BUILTIN is defined in pins_arduino.h
+// If RGB_BUILTIN is defined, it will be used as a pin number for the RGB LED
+// If RGB_BUILTIN has a side effect that prevents using RMT Legacy driver in IDF 5.1
+// Define ESP32_ARDUINO_NO_RGB_BUILTIN in build_opt.h or through CLI to disable RGB_BUILTIN
+#ifdef ESP32_ARDUINO_NO_RGB_BUILTIN
+#ifdef RGB_BUILTIN
+#undef RGB_BUILTIN
+#endif
+#endif
+
 // It fixes lack of pin definition for S3 and for any future SoC
 // this function works for ESP32, ESP32-S2 and ESP32-S3 - including the C3, it will return -1 for any pin
 #if SOC_TOUCH_SENSOR_NUM > 0
@@ -159,7 +169,7 @@ extern void ARDUINO_ISR_ATTR __digitalWrite(uint8_t pin, uint8_t val) {
     neopixelWrite(RGB_BUILTIN, comm_val, comm_val, comm_val);
     return;
   }
-#endif
+#endif  // RGB_BUILTIN
   if (perimanGetPinBus(pin, ESP32_BUS_TYPE_GPIO) != NULL) {
     gpio_set_level((gpio_num_t)pin, val);
   } else {
diff --git a/libraries/ESP32/examples/RMT/Legacy_RMT_Driver_Compatible/Legacy_RMT_Driver_Compatible.ino b/libraries/ESP32/examples/RMT/Legacy_RMT_Driver_Compatible/Legacy_RMT_Driver_Compatible.ino
new file mode 100644
index 00000000000..74578f755dc
--- /dev/null
+++ b/libraries/ESP32/examples/RMT/Legacy_RMT_Driver_Compatible/Legacy_RMT_Driver_Compatible.ino
@@ -0,0 +1,38 @@
+/*
+ * This example demonstrates how to use the file build_opt.h to disable the new RMT Driver
+ * Note that this file shall be added the Arduino project
+ *
+ * If the content of this file changes, it is necessary to delete the compiled Arduino IDE cache
+ * It can be done by changing, for instance, the "Core Debug Level" option, forcing the system to rebuild the Arduino Core
+ *
+ */
+
+#ifndef ESP32_ARDUINO_NO_RGB_BUILTIN
+
+// add the file "build_opt.h" to your Arduino project folder with "-DESP32_ARDUINO_NO_RGB_BUILTIN" to use the RMT Legacy driver
+#error "ESP32_ARDUINO_NO_RGB_BUILTIN is not defined, this example is intended to demonstrate the RMT Legacy driver.
+#error "Please add the file 'build_opt.h' with '-DESP32_ARDUINO_NO_RGB_BUILTIN' to your Arduino project folder."
+#error "Another way to disable the RGB_BUILTIN is to define it in the platformio.ini file, for instance: '-D ESP32_ARDUINO_NO_RGB_BUILTIN'"
+
+#else
+
+// add the file "build_opt.h" to your Arduino project folder with "-DESP32_ARDUINO_NO_RGB_BUILTIN" to use the RMT Legacy driver
+// neoPixelWrite() is a function that writes to the RGB LED and it won't be available here
+#include "driver/rmt.h"
+
+bool installed = false;
+
+void setup() {
+  Serial.begin(115200);
+  Serial.println("This sketch is using the RMT Legacy driver.");
+  installed = rmt_driver_install(RMT_CHANNEL_0, 0, 0) == ESP_OK;
+}
+
+void loop() {
+  String msg = "RMT Legacy driver is installed: ";
+  msg += (char *)(installed ? "Yes." : "No.");
+  Serial.println(msg);
+  delay(5000);
+}
+
+#endif  // ESP32_ARDUINO_NO_RGB_BUILTIN
diff --git a/libraries/ESP32/examples/RMT/Legacy_RMT_Driver_Compatible/build_opt.h b/libraries/ESP32/examples/RMT/Legacy_RMT_Driver_Compatible/build_opt.h
new file mode 100644
index 00000000000..583b42d9880
--- /dev/null
+++ b/libraries/ESP32/examples/RMT/Legacy_RMT_Driver_Compatible/build_opt.h
@@ -0,0 +1 @@
+-DESP32_ARDUINO_NO_RGB_BUILTIN