Arduino API pitfalls on ESP32
Arduino functions that behave differently on ESP32 than on AVR, or differently than their names suggest. Each entry says what happens, why, and what to do, with a quote from the arduino-esp32 3.3.12 or ESP-IDF v5.5.5 source or documentation.
Timing
Time counters, waits and what the scheduler does meanwhile.
micros() and millis() are 32-bit
Both functions take esp_timer_get_time(), a 64-bit count of microseconds since boot, and cast it to unsigned long. With GCC for Xtensa (ESP32, ESP32-S3) unsigned long is 32 bits, so micros() wraps after 2^32 µs (about 71.6 minutes) and millis() after 2^32 ms (about 49.7 days).12
Fix: Compare elapsed time, not deadlines: (now - last >= interval) with unsigned long operands stays correct across the wrap, because unsigned arithmetic is modulo 2^n. For a 64-bit count, call esp_timer_get_time() directly.
unsigned long now = micros();
if (now - last >= 1000000UL) { // not: now >= last + 1000000UL
last = now;
}
delay() blocks the task, delayMicroseconds() spins
delay() calls vTaskDelay(), so the loop task blocks and FreeRTOS can run other tasks on that core, including its idle task. delayMicroseconds() is a loop that reads esp_timer_get_time() until the time has passed; the task never blocks.1
Fix: Keep delayMicroseconds() for short pauses; use delay() (or a FreeRTOS wait) for anything that can give the core away.
yield() does not let the idle task run
yield() is vPortYield(). loop() runs in loopTask, created with priority 1; each core's idle task has priority 0 and runs only when no other task is ready. A loop() that never blocks stays ready, so yield() leaves the idle task of its core starved. The Task Watchdog primarily watches the idle tasks; if the idle task of that core is subscribed, this ends in a watchdog timeout. loopTask itself is not subscribed until enableLoopWDT() is called. On single-core chips loopTask itself calls vTaskDelay() between loop() passes at most every 2 s.1
Fix: In long busy loops block briefly, e.g. delay(1), instead of calling yield().
GPIO & interrupts
Pin ownership, pulls, interrupt handlers and the built-in LED.
digitalWrite() without pinMode() does nothing
digitalWrite() writes only to a pin that the peripheral manager has registered as GPIO, which pinMode() does. Otherwise it logs an error and returns; the pin is not changed.34
Fix: Call pinMode(pin, OUTPUT) first.
A pin belongs to one peripheral at a time
pinMode() first clears whatever bus the pin was attached to, so pinMode() on a pin driven by analogWrite() or ledcAttach() stops the PWM. analogWrite() does the same in the other direction: it clears the GPIO bus and attaches LEDC. Starting another peripheral on the UART pins detaches them from the UART.3
Fix: Configure each pin for one job; the last call that attaches the pin decides what it does.
INPUT_PULLUP on ESP32 GPIO34–39 leaves the pin floating
On the original ESP32, GPIO34–39 are input-only and have no internal pull-up or pull-down. pinMode(pin, INPUT_PULLUP) on them does not give a defined level.56
Fix: Fit an external pull-up or pull-down resistor, or use another GPIO.
attachInterrupt(): where the handler must live
The handler is called from the ESP-IDF GPIO ISR service, which arduino-esp32 installs with ARDUINO_ISR_FLAG. That flag is ESP_INTR_FLAG_IRAM, and ARDUINO_ISR_ATTR is IRAM_ATTR, only when CONFIG_ARDUINO_ISR_IRAM is set. With an IRAM interrupt, everything the handler touches, including functions it calls, must be in IRAM or DRAM: during flash writes and erases the cache is off and flash is not accessible to interrupt handlers.7
Fix: Mark handlers ARDUINO_ISR_ATTR so they follow the core's setting, and keep them short.
volatile bool fired = false;
void ARDUINO_ISR_ATTR onEdge() {
fired = true; // handle it in loop()
}
void setup() {
pinMode(4, INPUT_PULLUP);
attachInterrupt(4, onEdge, FALLING);
}
Calls that do not belong in an interrupt handler
Several common calls block or take locks and are not allowed in an ISR. analogRead() goes through adc_oneshot_read(), which uses mutexes and should not be called from an ISR. FreeRTOS functions such as xQueueSend() must not be called from an ISR; they have FromISR variants.8
Fix: In the handler only set a flag or call a ...FromISR() function; do the actual work in a task or in loop().
digitalWrite() is a driver call, not a register write
Each digitalWrite() checks the pin in the peripheral manager (and diverts RGB_BUILTIN to the RGB LED driver) before it calls ESP-IDF gpio_set_level(). For bit-banging, the ESP32-S3 has Dedicated GPIO: pins configured as dedicated are accessed by CPU instructions directly.3
Fix: For tight timing use the Dedicated GPIO driver (or a peripheral such as RMT, SPI or LEDC) instead of a digitalWrite() loop.
LED_BUILTIN on ESP32S3 Dev Module is not a GPIO number
The esp32s3 variant defines LED_BUILTIN as SOC_GPIO_PIN_COUNT + 48, a virtual pin for the RGB LED on GPIO48. digitalWrite(LED_BUILTIN, HIGH) is redirected to rgbLedWrite() over RMT and sets all three colours to RGB_BRIGHTNESS (64).9
Fix: Use rgbLedWrite() for colours; use a real GPIO number when a plain logic level is needed.
Analog & PWM
ADC readings and LEDC-based PWM and tone.
analogRead() is raw, analogReadMilliVolts() is calibrated
analogRead() returns the raw, non-calibrated conversion. The ADC reference voltage of the ESP32-S3 is 1100 mV by design but ranges from 1000 to 1200 mV between chips, so converting raw counts with a fixed formula is off by a chip-specific amount. analogReadMilliVolts() runs the value through the ESP-IDF calibration scheme (curve fitting where the chip supports it).10
Fix: Use analogReadMilliVolts() when you need a voltage.
uint32_t mv = analogReadMilliVolts(A0); // not analogRead(A0) * 3300 / 4095
Default attenuation is 11 dB, and the range is not 0–3.3 V
arduino-esp32 sets ADC_11db for all channels. The measurable input range at 11 dB is 0–3100 mV on the ESP32-S3 and 150–3100 mV on the ESP32, per the arduino-esp32 ADC docs.8
Fix: For small signals choose a lower attenuation per pin with analogSetPinAttenuation(); for higher voltages add a divider.
analogReadResolution() does not add bits
The default is 12 bits. On every chip except the original ESP32, analogReadResolution() only shifts the 12-bit result to the requested width; analogReadResolution(16) gives a 16-bit range with the same 12 bits of information.10
ADC2 pins and Wi-Fi
ADC2 is shared with Wi-Fi. The ESP32-S3 datasheet says ADC2 analog functions cannot be used together with Wi-Fi; the ESP32 TRM says ADC2 pins cannot be measured while Wi-Fi uses ADC2. In ESP-IDF, adc_oneshot_read() may then fail with ESP_ERR_TIMEOUT and an invalid result. analogRead() does not check that return code, so the sketch gets no error.11
Fix: Put analog inputs on ADC1 channels in any sketch that uses Wi-Fi.
analogWrite() is LEDC at 1 kHz, 8 bits, with shared timers
analogWrite() attaches the pin to LEDC with a global default of 1000 Hz and 8 bits. analogWriteFrequency() and analogWriteResolution() set that global default; when the pin is already attached, they also change its LEDC timer. Pins attached with the same frequency and resolution typically share one timer, and changing the frequency on one of them changes all channels on that timer.12
Fix: Set analogWriteFrequency()/analogWriteResolution() before the first analogWrite(). For pins that need independent frequencies, attach them with ledcAttach() at different frequency or resolution from the start.
analogWriteFrequency(PIN_FAN, 25000); // before the first analogWrite()
analogWrite(PIN_FAN, 128);
ledcSetup() and ledcAttachPin() are gone in 3.x
2.x
ledcSetup(0, 5000, 8);
ledcAttachPin(PIN_LED, 0);
ledcWrite(0, 128);3.x
ledcAttach(PIN_LED, 5000, 8);
ledcWrite(PIN_LED, 128);arduino-esp32 3.0 removed ledcSetup() and ledcAttachPin(). ledcAttach() replaces both and picks a channel automatically; the other LEDC functions now take a pin instead of a channel, and ledcDetachPin() became ledcDetach().13
LEDC frequency and resolution limit each other
PWM frequency is roughly the LEDC clock divided by the divider and by 2^resolution, and the divider has a limited range, so each resolution has a minimum and a maximum frequency. With the 40 MHz XTAL clock and 8 bits the minimum is about 153 Hz. A frequency that is too low fails the same way as one that is too high.14
Fix: Check the return value of ledcAttach(); for low frequencies use more resolution bits.
tone() is queued, plays on one pin, and keeps the pin
tone() sends a request to a separate toneTask and returns at once. With a duration, the task waits out the duration before it takes the next request, so consecutive tone() calls play one after another. Only one pin can play at a time: tone() on a second pin logs an error until noTone() is called on the first. The pin stays attached to LEDC (10-bit) after the duration ends; only noTone() detaches it.15
Fix: Call noTone(pin) before using the pin for something else or playing on another pin.
Serial & buses
Which port Serial is, how it buffers, and default pins of the buses.
Which port Serial is depends on USB CDC On Boot
Serial is a macro. With USB CDC On Boot disabled it is Serial0 (UART0); enabled, it is HWCDCSerial (USB Mode "Hardware CDC and JTAG") or USBSerial (TinyUSB). For ESP32S3 Dev Module the menu's default entry is Disabled, so Serial goes to UART0 on GPIO43 (TX) and GPIO44 (RX), not to the chip's own USB pins.16
Fix: Pick the port in Tools → USB CDC On Boot to match the connector you use, or print to Serial0 / HWCDCSerial explicitly.
Serial.print() on a UART waits for the wire
HardwareSerial starts with a TX buffer of 0 bytes. With no TX ring buffer the ESP-IDF UART driver's write blocks the task until all data have been sent out, so long prints at 115200 baud cost loop() real time.17
Fix: Call Serial.setTxBufferSize() before begin(), print less, or raise the baud rate.
Serial.setTxBufferSize(1024); // before begin()
Serial.begin(115200);
Serial buffer sizes are fixed at begin()
The UART RX buffer is 256 bytes by default. setRxBufferSize() and setTxBufferSize() fail with an error once the port is running, and a size not larger than the hardware FIFO is raised to FIFO + 1.17
Fix: Set buffer sizes before Serial.begin().
Serial over USB Serial/JTAG: no baud rate, no host, no waiting
On the HWCDC port the baud rate means nothing (updateBaudRate() only logs that it has no effect). if (Serial) is true only when USB is plugged and the CDC port is connected, so while (!Serial) waits forever without a host. With no host, writes do not wait for it: data go to a 256-byte TX ring buffer, and the oldest bytes are dropped to make room.18
Fix: Wait for the port with a timeout, and do not rely on output sent while no terminal is open.
Serial.begin();
while (!Serial && millis() < 3000) {} // give the host 3 s, then go on
Serial2.begin() without pins takes the USB pins on ESP32-S3
UART2's default pins on the ESP32-S3 are RX 19 and TX 20, and begin() uses them when no pins are given. GPIO19 and GPIO20 are USB_D- and USB_D+, connected by default to the USB Serial/JTAG controller.16
Fix: Always pass RX and TX pins to Serial1/Serial2.begin().
Serial2.begin(115200, SERIAL_8N1, 17, 18); // RX, TX
Wire.begin(): the first call wins
Without arguments Wire uses SDA and SCL from the variant (8 and 9 on esp32s3) at 100 kHz; frequencies above 1 MHz are clamped. If the bus is already running, begin() only logs a warning and returns true, keeping the old pins, so a library that calls Wire.begin() first decides the pins. Wire1 has no default pins unless the variant defines them.19
Fix: Call Wire.setPins(sda, scl) or Wire.begin(sda, scl) in setup() before any library starts the bus.
SPI.begin(): default pins, first call wins, CS is yours
SPI is the FSPI bus on the ESP32-S3; begin() without pins uses SCK 12, MISO 13, MOSI 11 from the variant. A SPIClass on another bus gets no default pins on the S3. If the bus is already started, begin() returns true without changing pins. SS is attached to the hardware only by setHwCs(true), so chip select is not driven by default.20
Fix: Pass pins explicitly for any bus other than the default one, and drive CS with digitalWrite() (or call setHwCs(true)).
Memory & storage
Stack, heap and the EEPROM emulation.
loop() has an 8 KB stack
setup() and loop() run in loopTask, created with getArduinoLoopTaskStackSize(): 8192 bytes unless CONFIG_ARDUINO_LOOP_STACK_SIZE or the sketch sets another value. Large local arrays in loop() come out of this stack, not out of the heap.21
Fix: Make big buffers static or global, or raise the stack with SET_LOOP_TASK_STACK_SIZE() at file scope.
SET_LOOP_TASK_STACK_SIZE(16 * 1024);
→ Arduino IDE says 306 KB for local variables. Your loop gets 8.
ESP.getFreeHeap() is not the largest block you can allocate
getFreeHeap() is the total free internal heap; because of fragmentation a single allocation of that size is probably not possible. getMaxAllocHeap() returns the largest free block.22
Fix: Check ESP.getMaxAllocHeap() before a large allocation.
String grows with realloc()
Short strings are stored inside the String object (SSO). Longer ones live on the heap, and every append that exceeds the capacity calls realloc() with the size rounded up to a multiple of 16. If the allocation fails, concat() returns false.23
Fix: reserve() the expected size once before building a String in a loop.
String line;
line.reserve(128);
EEPROM is an NVS blob that needs begin() and commit()
The EEPROM library keeps a RAM copy of a byte array stored in NVS. Without EEPROM.begin(size) there is no copy: read() returns 0 and write() does nothing. write() only changes the RAM copy; commit() (also called by end()) writes the blob to NVS. The arduino-esp32 docs recommend Preferences instead.24
Fix: Call EEPROM.begin(size) in setup() and EEPROM.commit() after writing; for new code use Preferences.
EEPROM.begin(64);
EEPROM.write(0, 42);
EEPROM.commit();
Wi-Fi & system
Wi-Fi defaults, the temperature sensor, random numbers, sleep and the two cores.
WiFi.begin() stores the credentials in flash
WiFi.persistent() defaults to true. Only when it is false does arduino-esp32 switch the Wi-Fi driver to RAM storage, and that happens when Wi-Fi is first turned on. Otherwise the ESP-IDF Wi-Fi NVS storage is on and all configuration set through the Wi-Fi APIs is stored in flash.25
Fix: Call WiFi.persistent(false) before the first WiFi.mode() or WiFi.begin().
WiFi.persistent(false);
WiFi.begin(ssid, pass);
Wi-Fi modem sleep is on by default
arduino-esp32 starts Wi-Fi with WIFI_PS_MIN_MODEM on every chip except the ESP32-S2. In this mode the station sleeps between DTIM beacons, and receiving data can be delayed by up to a DTIM cycle.25
Fix: WiFi.setSleep(false) when latency matters more than power.
temperatureRead() is the chip's own temperature
On chips with the temperature sensor peripheral, temperatureRead() installs the ESP-IDF driver for a 10–50 °C range and returns NAN if that fails. ESP-IDF says the sensor measures the temperature inside the silicon, is not recommended for ambient temperature, and that the measurement error depends on the configured range. On the original ESP32 the function uses an undocumented call.1
Fix: Use an external sensor for air temperature; use temperatureRead() to watch the chip.
randomSeed() turns random() into a software PRNG
random() uses the hardware RNG (esp_random()) until randomSeed() is called with a non-zero seed; after that it uses the C library rand(). The hardware RNG itself gives true random numbers only when Wi-Fi or Bluetooth is on or the SAR ADC entropy source is enabled; otherwise its output should be considered pseudo-random.26
Fix: Do not call randomSeed() unless you want a repeatable sequence. For keys and nonces call esp_random() or esp_fill_random() with Wi-Fi or Bluetooth running.
Deep sleep and ESP.restart() start the sketch from the beginning
ESP.deepSleep() calls esp_deep_sleep(), which does not return: on wake-up the chip runs the wake stub and then loads the application, so setup() runs again. During deep sleep most RAM is powered off; variables marked RTC_DATA_ATTR are in RTC slow memory, which stays powered by default. ESP.restart() resets both CPUs and the bootloader loads the application again. esp_reset_reason() tells the cases apart.22
Fix: Keep state that must survive deep sleep in RTC_DATA_ATTR variables and check esp_reset_reason() in setup().
RTC_DATA_ATTR uint32_t bootCount = 0;
void setup() {
bootCount++;
if (esp_reset_reason() == ESP_RST_DEEPSLEEP) { /* woke up */ }
}
loop() and a task on the other core really run in parallel
loop() runs on the core chosen in Tools → Arduino Runs On; a task pinned to the other core runs at the same time. Disabling interrupts affects only the current core, so it does not protect shared data. IDF FreeRTOS critical sections take a portMUX_TYPE spinlock instead.27
Fix: Guard shared variables with a spinlock critical section (or a mutex or queue).
static portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED;
taskENTER_CRITICAL(&mux);
counter++;
taskEXIT_CRITICAL(&mux);
→ Two cores, one counter: why counter++ loses updates on ESP32
Sources
Each number links here; open “What it confirms” for the exact quotes. Checked 2026-11-28.
- arduino-esp32 3.3.12: cores/esp32/esp32-hal-misc.c primary
What it confirms (4)
- Arduino API pitfalls on ESP32: micros() and millis() are 32-bit “unsigned long ARDUINO_ISR_ATTR micros() { … return (unsigned long)(esp_timer_get_time()); … return (unsigned long)(esp_timer_get_time() / 1000ULL); … #define LONG_TYPE_SIZE 32 … int64_t esp_timer_get_time(void) … Get time in microseconds since boot.”
- Arduino API pitfalls on ESP32: delay() blocks the task, delayMicroseconds() spins “void delay(uint32_t ms) { … vTaskDelay(ms / portTICK_PERIOD_MS); … void ARDUINO_ISR_ATTR delayMicroseconds(uint32_t us) { … while ((uint64_t)esp_timer_get_time() < e) { … The idle task runs when no other task is ready to run”
- Arduino API pitfalls on ESP32: yield() does not let the idle task run “void __yield() { … vPortYield(); … xTaskCreateUniversal(loopTask, "loopTask", getArduinoLoopTaskStackSize(), NULL, 1, &loopTaskHandle, ARDUINO_RUNNING_CORE); … loopTaskWDTEnabled = false; … void enableLoopWDT() { … Vanilla FreeRTOS will implicitly create an idle task of priority 0 … In IDF FreeRTOS, a separate pinned idle task is created for each core. … The TWDT primarily watches the Idle Tasks of each CPU … #if CONFIG_FREERTOS_UNICORE … if ((now - lastYield) > 2000) { … vTaskDelay(5);”
- Arduino API pitfalls on ESP32: temperatureRead() is the chip's own temperature “temperature_sensor_config_t temp_sensor_config = TEMPERATURE_SENSOR_CONFIG_DEFAULT(10, 50); … float result = NAN; … //Undocumented!!! Get chip temperature in Fahrenheit … The temperature sensor is designed primarily to measure the temperature inside the silicon. … So it's not recommended to use it for ambient temperature measurement. … different measure ranges have different measurement errors”
- cppreference: Arithmetic operators primary
What it confirms (1)
- Arduino API pitfalls on ESP32: micros() and millis() are 32-bit “Unsigned integer arithmetic is always performed modulo 2n where n is the number of bits in that particular integer.”
- arduino-esp32 3.3.12: cores/esp32/esp32-hal-gpio.c primary
What it confirms (3)
- Arduino API pitfalls on ESP32: digitalWrite() without pinMode() does nothing “if (perimanGetPinBus(pin, ESP32_BUS_TYPE_GPIO) != NULL) { … gpio_set_level((gpio_num_t)pin, val); … log_e("IO %u is not set as GPIO. Execute digitalMode(%u, OUTPUT) first.", pin, pin);”
- Arduino API pitfalls on ESP32: A pin belongs to one peripheral at a time “if (!perimanClearPinBus(pin)) { … log_e("Deinit of previous bus from IO %u failed", pin); … if (bus == NULL && perimanClearPinBus(pin)) { … Detaching may occur when, for instance, starting another peripheral using RX and TX pins, such as Wire.begin(RX0, TX0).”
- Arduino API pitfalls on ESP32: digitalWrite() is a driver call, not a register write “extern void ARDUINO_ISR_ATTR __digitalWrite(uint8_t pin, uint8_t val) { … if (pin == RGB_BUILTIN) { … gpio_set_level((gpio_num_t)pin, val); … Any GPIO that is configured as "dedicated" can be access by CPU instructions directly, which makes it easy to achieve a high GPIO flip speed”
- arduino-esp32 3.3.12: docs/en/api/gpio.rst primary
What it confirms (1)
- Arduino API pitfalls on ESP32: digitalWrite() without pinMode() does nothing “This function is only used if the ``pinMode`` was configured as ``OUTPUT``.”
- ESP-IDF v5.5.5 docs (ESP32): gpio primary
What it confirms (1)
- Arduino API pitfalls on ESP32: INPUT_PULLUP on ESP32 GPIO34–39 leaves the pin floating “GPIO34-39 can only be set as input mode and do not have software-enabled pullup or pulldown functions.”
- ESP32 Series Datasheet v5.3 primary
What it confirms (1)
- Arduino API pitfalls on ESP32: INPUT_PULLUP on ESP32 GPIO34–39 leaves the pin floating “GPIO pins 34-39 are input-only”
- arduino-esp32 3.3.12: cores/esp32/esp32-hal.h primary
What it confirms (1)
- Arduino API pitfalls on ESP32: attachInterrupt(): where the handler must live “#if CONFIG_ARDUINO_ISR_IRAM … #define ARDUINO_ISR_ATTR IRAM_ATTR … #define ARDUINO_ISR_FLAG ESP_INTR_FLAG_IRAM … esp_err_t err = gpio_install_isr_service((int)ARDUINO_ISR_FLAG); … When performing write and erase operations on SPI flash, ESP32-S3 will disable the cache, making SPI flash and SPIRAM inaccessible for interrupt handlers. … Never register an interrupt handler with ESP_INTR_FLAG_IRAM flag if you are not 100% sure that all the code and data that the interrupt ever accesses are in IRAM (code) or DRAM (data). … #define ARDUINO_ISR_FLAG (0)”
- arduino-esp32 3.3.12: cores/esp32/esp32-hal-adc.c primary
What it confirms (2)
- Arduino API pitfalls on ESP32: Calls that do not belong in an interrupt handler “adc_oneshot_read(adc_handle[adc_unit].adc_oneshot_handle, channel, &value); … This function uses mutexes to avoid concurrent hardware usage. Therefore, this function should not be used in an ISR context. … This function must not be called from an interrupt service routine. See xQueueSendFromISR () for an alternative which may be used in an ISR.”
- Arduino API pitfalls on ESP32: Default attenuation is 11 dB, and the range is not 0–3.3 V “static uint8_t __analogAttenuation = ADC_11db; … .. tab:: ESP32-S3 … ``ADC_11db`` 0 mV ~ 3100 mV … ``ADC_11db`` 150 mV ~ 3100 mV”
- arduino-esp32 3.3.12: variants/esp32s3/pins_arduino.h primary
What it confirms (1)
- Arduino API pitfalls on ESP32: LED_BUILTIN on ESP32S3 Dev Module is not a GPIO number “#define PIN_RGB_LED 48 … static const uint8_t LED_BUILTIN = SOC_GPIO_PIN_COUNT + PIN_RGB_LED; … #define RGB_BRIGHTNESS 64 … //use RMT to set all channels on/off … const uint8_t comm_val = val != 0 ? RGB_BRIGHTNESS : 0; … rgbLedWrite(RGB_BUILTIN, comm_val, comm_val, comm_val);”
- arduino-esp32 3.3.12: docs/en/api/adc.rst primary
What it confirms (2)
- Arduino API pitfalls on ESP32: analogRead() is raw, analogReadMilliVolts() is calibrated “This function will return analog raw value (non-calibrated). … This function will return analog value in millivolts (calibrated). … By design, the ADC reference voltage for ESP32-S3 is 1100 mV. However, the true reference voltage can range from 1000 mV to 1200 mV among different chips. … err = adc_oneshot_get_calibrated_result(adc_handle[adc_unit].adc_oneshot_handle, adc_handle[adc_unit].adc_cali_handle, channel, &value); … #if ADC_CALI_SCHEME_CURVE_FITTING_SUPPORTED … ESP32-S3 supports ADC_CALI_SCHEME_VER_CURVE_FITTING scheme.”
- Arduino API pitfalls on ESP32: analogReadResolution() does not add bits “Default is 12 bits (range from 0 to 4095) for all chips. … When different resolution is set, the values read will be shifted to match the given resolution. … For the ESP32, the resolution is between 9 to12 and it will change the ADC hardware resolution. Else value will be shifted.”
- ESP32-S3 Series Datasheet v2.2 primary
What it confirms (1)
- Arduino API pitfalls on ESP32: ADC2 pins and Wi-Fi “analog functions (see Table 2-8 Analog Functions) cannot be used with Wi-Fi simultaneously … If Wi-Fi module is using the SAR ADC2, users can not measure the analog signal from the pins using SAR ADC2. … ADC2 is also used by Wi-Fi. … This function may fail when the ADC is in use by other drivers/peripherals, and return ESP_ERR_TIMEOUT. Under this condition, the ADC raw result is invalid. … adc_oneshot_read(adc_handle[adc_unit].adc_oneshot_handle, channel, &value); … return mapResolution(value);”
- arduino-esp32 3.3.12: cores/esp32/esp32-hal-ledc.c primary
What it confirms (1)
- Arduino API pitfalls on ESP32: analogWrite() is LEDC at 1 kHz, 8 bits, with shared timers “static uint8_t analog_resolution = 8; … static int analog_frequency = 1000; … if (ledcAttach(pin, analog_frequency, analog_resolution) == 0) { … // if pin is attached to LEDC change frequency, otherwise update the global frequency … analog_frequency = freq; … analog_resolution = resolution; … they typically share one timer … on either pin then changes **all** channels on that timer”
- arduino-esp32 3.3.12: docs/en/migration_guides/2.x_to_3.0.rst primary
What it confirms (1)
- Arduino API pitfalls on ESP32: ledcSetup() and ledcAttachPin() are gone in 3.x “``ledcAttach`` used to set up the LEDC pin (merged ``ledcSetup`` and ``ledcAttachPin`` functions). … In all functions, input parameter ``channel`` has been changed to ``pin``. … ``ledcDetachPin`` renamed to ``ledcDetach``.”
- arduino-esp32 3.3.12: docs/en/api/ledc.rst primary
What it confirms (1)
- Arduino API pitfalls on ESP32: LEDC frequency and resolution limit each other “``freq ≈ ledc_clock / (clock_divider × 2^resolution)`` … At 40 MHz with 8-bit resolution, the minimum is about **153 Hz** … Asking for a frequency that is too low for the chosen resolution fails the same way as asking for one that is too high. … To get lower frequencies, **increase** the resolution”
- arduino-esp32 3.3.12: cores/esp32/Tone.cpp primary
What it confirms (1)
- Arduino API pitfalls on ESP32: tone() is queued, plays on one pin, and keeps the pin “xQueueSend(_tone_queue, &tone_msg, portMAX_DELAY); … delay(tone_msg.duration); … log_e("Tone is still running on pin %d, call noTone(%d) first!", _pin, _pin); … ret = ledcAttach(tone_msg.pin, tone_msg.frequency, 10); … ledcDetach(tone_msg.pin);”
- arduino-esp32 3.3.12: cores/esp32/HardwareSerial.h primary
What it confirms (2)
- Arduino API pitfalls on ESP32: Which port Serial is depends on USB CDC On Boot “#define Serial HWCDCSerial … #define Serial USBSerial … #define Serial Serial0 … esp32s3.menu.CDCOnBoot.default=Disabled … esp32s3.menu.USBMode.hwcdc=Hardware CDC and JTAG … static const uint8_t TX = 43; … static const uint8_t RX = 44;”
- Arduino API pitfalls on ESP32: Serial2.begin() without pins takes the USB pins on ESP32-S3 “// Default pins for UART2 are arbitrary, and defined here for convenience. … #define RX2 (gpio_num_t)19 … #define TX2 (gpio_num_t)20 … rxPin = _rxPin < 0 ? (int8_t)RX2 : _rxPin; … RTC_GPIO19 USB_D- ADC2_CH8 … RTC_GPIO20 USB_D+ ADC2_CH9 … USB_D+/- – by default, connected to the USB Serial/JTAG Controller.”
- arduino-esp32 3.3.12: cores/esp32/HardwareSerial.cpp primary
What it confirms (2)
- Arduino API pitfalls on ESP32: Serial.print() on a UART waits for the wire “_rxBufferSize(256), _txBufferSize(0) … Default TX buffer size is 0 (no buffering). … If set to zero, driver will not use TX buffer, TX function will block task until all data have been sent out.”
- Arduino API pitfalls on ESP32: Serial buffer sizes are fixed at begin() “log_e("RX Buffer can't be resized when Serial is already running. Set it before calling begin()."); … new_size = FIFOLen + 1; … Default RX buffer size is 256 bytes.”
- arduino-esp32 3.3.12: cores/esp32/HWCDC.cpp primary
What it confirms (1)
- Arduino API pitfalls on ESP32: Serial over USB Serial/JTAG: no baud rate, no host, no waiting “log_w("USB Serial/JTAG baud rate is set by the USB standard; updateBaudRate(%lu) has no effect", baud); … // It should return <true> just when USB is plugged and CDC is connected. … //TX Buffer default has 256 bytes if not preset … // how many byte should be flushed to make space for the new data”
- arduino-esp32 3.3.12: libraries/Wire/src/Wire.cpp primary
What it confirms (1)
- Arduino API pitfalls on ESP32: Wire.begin(): the first call wins “sdaPin = SDA; //use Default Pin … log_w("Bus already started in Master Mode."); … log_e("no Default SDA Pin for Second Peripheral"); … static const uint8_t SDA = 8; … static const uint8_t SCL = 9; … frequency = 100000UL; … } else if (frequency > 1000000UL) { … frequency = 1000000UL; … //call setPins() first, so that begin() can be called without arguments from libraries”
- arduino-esp32 3.3.12: libraries/SPI/src/SPI.cpp primary
What it confirms (1)
- Arduino API pitfalls on ESP32: SPI.begin(): default pins, first call wins, CS is yours “SPIClass SPI(FSPI); … _sck = (_spi_num == FSPI) ? SCK : -1; … bool SPIClass::begin(int8_t sck, int8_t miso, int8_t mosi, int8_t ss) { … _use_hw_ss(false) … spiAttachSS(_spi, 0, _ss); … static const uint8_t SCK = 12; … static const uint8_t MISO = 13; … static const uint8_t MOSI = 11; … static const uint8_t SS = 10;”
- arduino-esp32 3.3.12: cores/esp32/main.cpp primary
What it confirms (1)
- Arduino API pitfalls on ESP32: loop() has an 8 KB stack “#define ARDUINO_LOOP_STACK_SIZE 8192 … #define ARDUINO_LOOP_STACK_SIZE CONFIG_ARDUINO_LOOP_STACK_SIZE … xTaskCreateUniversal(loopTask, "loopTask", getArduinoLoopTaskStackSize(), NULL, 1, &loopTaskHandle, ARDUINO_RUNNING_CORE); … #define SET_LOOP_TASK_STACK_SIZE(sz)”
- arduino-esp32 3.3.12: cores/esp32/Esp.cpp primary
What it confirms (2)
- Arduino API pitfalls on ESP32: ESP.getFreeHeap() is not the largest block you can allocate “return heap_caps_get_free_size(MALLOC_CAP_INTERNAL); … return heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL); … Note that because of heap fragmentation it is probably not possible to allocate a single block of memory of this size. Use heap_caps_get_largest_free_block() for this purpose.”
- Arduino API pitfalls on ESP32: Deep sleep and ESP.restart() start the sketch from the beginning “esp_deep_sleep(time_us); … esp_restart(); … Upon waking up, the device calls deep sleep wake stub, and then proceeds to load application … In Deep-sleep mode, the CPUs, most of the RAM, and all digital peripherals that are clocked from APB_CLK are powered off. … If some variables in the program are placed into RTC SLOW memory (for example, using RTC_DATA_ATTR attribute), RTC SLOW memory will be kept powered on by default. … When the function is called, execution of the program stops, both CPUs are reset, the application is loaded by the bootloader and starts execution again. … Reset after exiting deep sleep mode. … void esp_deep_sleep(uint64_t time_in_us) … Call to this function is equivalent to a call to esp_deep_sleep_enable_timer_wakeup followed by a call to esp_deep_sleep_start. … The function does not do a return (no rejection).”
- arduino-esp32 3.3.12: cores/esp32/WString.cpp primary
What it confirms (1)
- Arduino API pitfalls on ESP32: String grows with realloc() “// Can we use SSO here to avoid allocation? … size_t newSize = (maxStrLen + 16) & (~0xf); … char *newbuffer = (char *)realloc(isSSO() ? nullptr : wbuffer(), newSize); … if (!reserve(newlen)) {”
- arduino-esp32 3.3.12: libraries/EEPROM/src/EEPROM.cpp primary
What it confirms (1)
- Arduino API pitfalls on ESP32: EEPROM is an NVS blob that needs begin() and commit() “Uses a nvs byte array to emulate EEPROM … bool EEPROMClass::begin(size_t size) { … if (!_data) { … // Optimize _dirty. Only flagged if data written is different. … esp_err_t err = nvs_set_blob(_handle, _name, _data, _size); … It should be considered as the replacement for the Arduino EEPROM library.”
- arduino-esp32 3.3.12: libraries/WiFi/src/WiFiGeneric.cpp primary
What it confirms (2)
- Arduino API pitfalls on ESP32: WiFi.begin() stores the credentials in flash “bool WiFiGenericClass::_persistent = true; … if (!wifiLowLevelInit(_persistent)) { … lowLevelInitDone = esp_wifi_set_storage(WIFI_STORAGE_RAM) == ESP_OK; … If the Wi-Fi NVS flash is enabled, all Wi-Fi configurations set via the Wi-Fi APIs will be stored into flash”
- Arduino API pitfalls on ESP32: Wi-Fi modem sleep is on by default “wifi_ps_type_t WiFiGenericClass::_sleepEnabled = WIFI_PS_MIN_MODEM; … #if CONFIG_IDF_TARGET_ESP32S2 … When Modem-sleep mode is enabled, the delay in receiving Wi-Fi data may be the same as the DTIM cycle (minimum power-saving mode) … Disabling it increases power consumption, but minimizes the delay in receiving Wi-Fi data in real time.”
- arduino-esp32 3.3.12: cores/esp32/WMath.cpp primary
What it confirms (1)
- Arduino API pitfalls on ESP32: randomSeed() turns random() into a software PRNG “uint32_t val = (s_useRandomHW) ? esp_random() : rand(); … if (seed != 0) { … s_useRandomHW = false; … RF subsystem is enabled, i.e., Wi-Fi or Bluetooth are enabled. … If none of the above conditions are true, the output of the RNG should be considered as pseudo-random only.”
- arduino-esp32 3.3.12: boards.txt primary
What it confirms (1)
- Arduino API pitfalls on ESP32: loop() and a task on the other core really run in parallel “menu.LoopCore=Arduino Runs On … &loopTaskHandle, ARDUINO_RUNNING_CORE); … However, interrupts are only disabled or enabled on the current core. … Spinlocks are of portMUX_TYPE (not to be confused to FreeRTOS mutexes) … taskENTER_CRITICAL(&spinlock) enters a critical from a task context … static portMUX_TYPE my_spinlock = portMUX_INITIALIZER_UNLOCKED;”