🌐
ESP32 Forum
esp32.com › viewtopic.php
Nanosecond delay - ESP32 Forum
April 12, 2024 - For less time-sensitive delays I used "busy_wait_us", but the best I found for ESP is "esp_rom_delay_us" which is marked as "Internal and Unstable". Of course, if I can manage to make a delay in 10s on ns, then this won't be a problem either. For short delays, you can use esp_rom_delay_us().
🌐
Reddit
reddit.com › r/esp32 › need to have sub microsecond delay precision
r/esp32 on Reddit: Need to have sub microsecond delay precision
July 29, 2020 -

Hi guys,

I'm trying to implement something that will generate two triggers at regular intervals where the two triggers are separated by a configurable delay. The delay has to be configurable to sub microsecond resolution. Ideally, 500ns or less.

I chose the ESP32 because of its 240MHz clock, but it still seems to be having trouble.

I'm messing around with the following code:

#define cyclespermicro 240
#define microcycles(n) (n*cyclespermicro)

uint32_t startCounter, counter, cpu_cycles;
int cyclediff, totalcycles, corrected;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

  startCounter = ESP.getCycleCount();
  totalcycles = startCounter + microcycles(2);
  //delayMicroseconds(1);

 while (ESP.getCycleCount() < totalcycles) {
        __asm__ __volatile__ ("nop");
 }
  
  counter = ESP.getCycleCount();
  cpu_cycles = counter - startCounter;
  corrected = cpu_cycles - 262;
  Serial.print("StartCounter: ");
  Serial.print(startCounter);
  Serial.print(" counter: ");
  Serial.print(counter);
  Serial.print(" cpu_cycles: ");
  Serial.print(cpu_cycles);
  Serial.print(" corrected: ");
  Serial.println(corrected);
}

void loop() {

}

The problem is that I get the same number of clock cycles for 1 us as I do for 2 us. I'm guessing this is because the while loop overhead is overwhelming the 1 us delay.

The problem is I need to have the amount of delay configurable and I don't know the best way to do this. Anyone have any ideas how to efficiently create configurable sub-microsecond delays?

EDIT: I am also noticing, when I get rid of the while loop entirely, and just straight up copy in NOP lines, it works as expected for like 3 NOPs, but once I hit 5 NOPS, the overhead jumps to like 500 cycles. What is going on here?

Discussions

Sub microsecond delays on esp32
I have several ESP's and haven't used them due to my confusion about Timer hardware. I would want a Timer Interrupt for the task you describe. This discussion on sub-microsecond ESP timing seems well worth reading FYI If ESP Timers prove problematic, then I like your assembler-delay idea. It should be something you could tweak for the desired result -- if you have a way to write and load assembler to the ESP. hth, gl More on reddit.com
🌐 r/arduino
19
5
December 18, 2021
micros() & delayMicroseconds() not good enough
There was an error while loading. Please reload this page · Calling micros() twice is in a row, takes nearly 50uS More on github.com
🌐 github.com
13
March 23, 2017
Nanosecond delay using "nop" (GIT8266O-487)
Environment Development Kit: ESP8266 Wemos D1 mini Development Env: Make/Eclipse Operating System: Ubuntu Power Supply: USB Problem Description Hi, I need to create a NanoSecond delay. Previously I used OPEN RTOS SDK and the library whic... More on github.com
🌐 github.com
18
June 10, 2020
vTaskDelay with long wait times
"The constant port TICK_PERIOD_MS can be used to calculate real time from the tick rate - with the resolution of one tick period." More on reddit.com
🌐 r/esp32
25
9
October 24, 2023
🌐
PlatformIO Community
community.platformio.org › platformio core
How to delay microseconds on ESP32 under a Free RTOS task - PlatformIO Core - PlatformIO Community
September 8, 2022 - I have some code running as a FreeRTOS task on my ESP32. I would like to toggle an output pin in the order of microseconds so use the function delayMicroseconds. However, this crashes my ESP32 every time. Even a simple loop causes it to crash: for ( int i = 0 ; i
🌐
Mechatronics LAB
mechatronicslab.net › home › lessons › protected: esp32 arduino programming handbook › chapter 12: timers, delays & interrupts › delaymicroseconds() function for esp32 arduino programming
delayMicroseconds() Function for ESP32 Arduino Programming - Mechatronics LAB
The delayMicroseconds() function tells your ESP32 to pause for an exact number of microseconds. This is useful when you need very small, precise delays. For example, it helps when generating signals for certain sensors, controlling communication protocols, or creating accurate pulses.
🌐
ESP32 Forum
esp32.com › viewtopic.php
Nanosecond precision packet arrival time for WiFi - ESP32 Forum
If I can guarantee that the callback finished before the next packet arrives, then I think getting the ccount register in the first statement of callback function might be a closer indication of the packet arrival time. Since I need to subtract two such values, the possible (constant?) delay from packet arrival to callback execution would get factored out automatically.
🌐
Esp32developer
esp32developer.com › programming-in-c-c › delays › delays
Delays – ESP32 Developer
We’ve also found instances where (10 / portTICK_PERIOD_MS) results in a delay of 100mS regardless of the value used! Even vTaskDelay(10) does it.
🌐
Espressif
docs.espressif.com › projects › esp-idf › en › stable › esp32s2 › api-reference › system › esp_timer.html
ESP Timer (High Resolution Timer) - ESP32-S2 - — ESP-IDF Programming Guide v6.0.1 documentation
The ESP Timer feature allows for creating software timers and invoking their callback functions (dispatching callbacks) on timeout. ESP Timer is useful when user software needs to perform delayed or periodic actions, such as delayed device start/stop or periodic sampling of sensor data.
🌐
ESP32 Forum
esp32.com › viewtopic.php
Microsecond or fraction of microsecond delay - ESP32 Forum
March 11, 2018 - function delay(period) { start = CCOUNT; while (CCOUNT - start < period) { // do nothing } }[ This would busy wait (waste) the cycles, but it should work. You might also have to disable some of the interrupts if you need absolutely delays no matter what.
Find elsewhere
🌐
Rust
docs.rs › esp32-hal › latest › esp32_hal › struct.Delay.html
Delay in esp32_hal - Rust
Delay for the specified number of nanoseconds · source§ · source§ · Returns a copy of the value. Read more · 1.0.0 · source§ · Performs copy-assignment from source. Read more · source§ · source§ · Pauses execution for ms milliseconds · source§ · source§ ·
🌐
ESP32 Forum
esp32.com › viewtopic.php
esp32 get time with nanoseconds precision - ESP32 Forum
Hi, I am looking for a way to get the time since boot with nanoseconds precision. Currently, the best I can do is esp_timer_get_time() which returns time with microsecond precision. I connected my board to an oscilloscope and found out that it takes around 1.29 us for the esp_timer_get_time() ...
🌐
Reddit
reddit.com › r/arduino › sub microsecond delays on esp32
r/arduino on Reddit: Sub microsecond delays on esp32
December 18, 2021 -

Do I have to write instructions in assembly? Im using Arduino IDE and im trying to sample a signal at 44.1khz. I come from a PIC Micro background where I would just use a TMR. Whats the play here in Arduino Land?

🌐
ESP32 Forum
esp32.com › viewtopic.php
Wait microsecond - ESP32 Forum
The ROM function ets_delay_us() (defined in rom/ets_sys.h) will allow you to busy-wait for a correct number of microseconds.
🌐
Espressif
docs.espressif.com › projects › esp-idf › en › latest › esp32 › api-reference › system › esp_timer.html
ESP Timer (High Resolution Timer) - ESP32 - — ESP-IDF Programming Guide latest documentation
The ESP Timer feature allows for creating software timers and invoking their callback functions (dispatching callbacks) on timeout. ESP Timer is useful when user software needs to perform delayed or periodic actions, such as delayed device start/stop or periodic sampling of sensor data.
🌐
GitHub
github.com › espressif › arduino-esp32 › issues › 282
micros() & delayMicroseconds() not good enough · Issue #282 · espressif/arduino-esp32
March 23, 2017 - void IRAM_ATTR delayMicroseconds(uint32_t us) { if(us){ uint32_t m = micros(); while( (micros() - m ) < us ){ NOP(); } } }
Author   espressif
🌐
Mechatronics LAB
mechatronicslab.net › home › lessons › esp32 arduino programming handbook › chapter 12: timers, delays & interrupts › delay() function for esp32 arduino programming
Delay() Function for ESP32 Arduino Programming - Mechatronics LAB
It pauses your program for a set amount of time, measured in milliseconds. What Is delay() and Why Use It? The delay() function tells your ESP32 to “wait” before running the next line of code. One millisecond equals one-thousandth of a second. For example, delay(1000) makes the ESP32 pause ...
🌐
ControllersTech®
controllerstech.com › home › arduino › core tutorials › delaymicroseconds() tutorial
Arduino delayMicroseconds() Tutorial: Precise Timing, Pulses & Alternatives
May 29, 2026 - On a 16 MHz Arduino (UNO, Nano, ... is 62.5 nanoseconds. A 10 µs delay requires 160 clock cycles. The function calculates this count based on the board’s clock frequency and loops exactly that many times.
🌐
GitHub
github.com › espressif › ESP8266_RTOS_SDK › issues › 914
Nanosecond delay using "nop" (GIT8266O-487) · Issue #914 · espressif/ESP8266_RTOS_SDK
June 10, 2020 - Using "nop" command gives me 0.5us delay when ESP works at 80MHz. It should be much faster. Aswell "for" loop with 1 iteration takes longer then on OPEN RTOS SDK.
Author   espressif
🌐
ESP32 Forum
esp32.com › viewtopic.php
Need API support to provide micro seconds delay - Page 2 - ESP32 Forum
October 6, 2020 - Tue Oct 06, 2020 7:46 pm This will take about 30 microseconds to execute: int samp = adc1_get_raw((adc1_channel_t)channel2); So for a hundred microsecond delay do: for(int loop = 0; loop < 3; loop++) { int samp = adc1_get_raw((adc1_channel_t)channel2); } It goes without saying that this a ...
🌐
ESP32 Forum
esp32.com › board index › english forum › discussion forum › esp-idf
[SOLVED] My function for microseconds delay doesn't work properly - ESP32 Forum
September 9, 2021 - Firstly, I didn't included esp_timer.h, I think the compiler didn't recognize esp_timer_get_time() and that's why it wasn't working (but no errors during compiling). Am I right? Then, I tried your esp_rom_delay_us but it gives a possible problem (and here the second question): it stops the processor for a certain time, giving the impossibility of performing other tasks.
🌐
ESP32 Forum
esp32.com › viewtopic.php
How to delay microsecond - ESP32 Forum
March 11, 2017 - kolban wrote:It might be that the function called "ets_delay_us(value)" is what you could use. Its proto type is defined in <rom/ets_sys.h>. I have done it by your support.