🌐
ESP32 Forum
esp32.com › viewtopic.php
freeRTOS microsecond delay - ESP32 Forum
September 23, 2022 - You can use something like this: ... uint64_t microseconds = esp_timer_get_time(); if (0 != number_of_microseconds) { while (((uint64_t) esp_timer_get_time() - microseconds) <= number_of_microseconds) { // Wait } } where number_of_microseconds is an uint64_t and it is your delay, in microseconds.
🌐
Medium
aliafshar.medium.com › esp-idf-tutorials-part-2-delay-75a6c29e05e4
ESP-IDF for Arduino Users, Tutorials Part 2: Delay | by Ali Afshar | Medium
January 6, 2021 - This is the second part of a series of ESP-IDF tutorials that I will complete as I learn stuff. Read part 1. One of the best things about Arduino is the ability to just block for a period with: ... It’s also one of the worst things. As soon as you need to do a few things at the same time, you will end up using better systems, like calling millis() or using the elapsedMillis library. Anyway, the IDF has you covered. You can delay, but amazingly it appears that once you multi-task (which you will with Free RTOS), the delay doesn’t block the other tasks from running.
Discussions

Need to have sub microsecond delay precision
Can this be solved using the RMT peripheral? More on reddit.com
🌐 r/esp32
17
3
July 29, 2020
Fractional Microsecond Delay;
Your best bet is to use the chip's hardware timers. I'd start by reading this: https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/timer.html Depending on what you're doing, it might also be worth looking into the RMT peripheral. It's designed for generating infrared remote control signals but it can be used to precisely generate custom pulses for other purposes too. It's overkill but it'd get the job done. https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/rmt.html If you're willing to put in the time to learn the lower level ESP32 APIs, they'll seriously pay off in performance and flexibility. You should be able to accomplish microsecond timing without delayXXX() or interrupts using the RMT peripheral. More on reddit.com
🌐 r/esp8266
10
11
October 5, 2021
`FreeRtos::delay_us` will block for less than the given time for fractional milliseconds
Pauses execution for at minimum us microseconds. Pause can be longer if the implementation requires it due to precision/timing issues. The following line always rounds down, resulting in a much shorter delay: ... I see two solutions, either: using esp_timer_get_time and spinning after vTaskDelay, ... More on github.com
🌐 github.com
5
April 15, 2022
General-Purpose Delay Provider
Currently there are two delay providers. As per the docs: Ets Espressif built-in delay provider Use only for very small delays (us or a few ms at most), or else the FreeRTOS IDLE tasks’ might starv... More on github.com
🌐 github.com
1
December 29, 2022
🌐
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
Dispatches timer callbacks from a single high-priority ESP Timer task (esp_timer task (notified by ISR) > callback). Suitable for handling timer callbacks that are not time-critical. ... Dispatches timer callbacks directly from an interrupt handler (ISR > callback). Suitable for simple, low-latency timer callbacks which take a few microseconds to run. Ensures shorter delay between the event and the callback execution.
🌐
ESP32 ESP-IDF
esp32tutorials.com › home › esp32 esp-idf freertos timer and delay using esp-idf
ESP32 ESP-IDF FreeRTOS Timer and Delay using ESP-IDF
August 15, 2022 - The difference between them is 5383 microseconds. As we mentioned before, the tick time period is predefined as 10 milliseconds. However, it took less than 10 milliseconds. In the previous example we looked upon vTaskDelay() function and measured ...
🌐
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?

🌐
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. Thank you so much. ... Espressif Systems is a fabless semiconductor company providing cutting-edge low power WiFi SoCs and wireless solutions for wireless communications and Internet of Things applications. ESP8266EX and ESP32 are some of our products.
🌐
Espressif
docs.espressif.com › projects › esp-idf › en › stable › esp32 › api-reference › system › esp_timer.html
ESP Timer (High Resolution Timer) - ESP32 - — ESP-IDF Programming Guide v6.0.2 documentation
Dispatches timer callbacks from a single high-priority ESP Timer task (esp_timer task (notified by ISR) > callback). Suitable for handling timer callbacks that are not time-critical. ... Dispatches timer callbacks directly from an interrupt handler (ISR > callback). Suitable for simple, low-latency timer callbacks which take a few microseconds to run. Ensures shorter delay between the event and the callback execution.
🌐
Reddit
reddit.com › r/esp8266 › fractional microsecond delay;
r/esp8266 on Reddit: Fractional Microsecond Delay;
October 5, 2021 -

Hey all, possess any of you managed to get fractional microsecond delay on the ESP8266/ESP32? I need to delay about 5.33 microseconds between setting a pin high and then back to low. I understand that delayMicroseconds might‏‏‎‏‏‎‏‏‎‏‏‎­be close, but wanted to see if there might be something with a bit more resolution. Thanks!

🌐
GitHub
github.com › esp-rs › esp-idf-hal › blob › master › src › delay.rs
esp-idf-hal/src/delay.rs at master · esp-rs/esp-idf-hal
//! use esp_idf_hal::delay::Delay; //! //! let delay: Delay = Default::default(); //! // ... //! delay.delay_us(42); //! // ... //! delay.delay_ms(142); //! // ... //! ``` //! //! Example of a small microsecond delay: //! ``` //! use esp_idf_hal::delay::Ets; //! //! Ets::delay_us(42); //! ``` //! //! Example of a millisecond delay: //! ``` //! use esp_idf_hal::delay::FreeRtos; //! //! FreeRtos::delay_ms(42); //! ``` //! //! Example of an [embedded_hal::delay::DelayNs] consumer with an ·
Author   esp-rs
Find elsewhere
🌐
Espressif
docs.espressif.com › projects › esp-idf › en › v5.0 › esp32 › api-reference › system › esp_timer.html
High Resolution Timer (ESP Timer) - ESP32 - — ESP-IDF Programming Guide v5.0 documentation
An interrupt level of the handler depends on the CONFIG_ESP_TIMER_INTERRUPT_LEVEL option. It allows to set this: 1, 2 or 3 level (by default 1). Raising the level, the interrupt handler can reduce the timer processing delay. esp_timer set of APIs provides one-shot and periodic timers, microsecond time resolution, and 64-bit range.
🌐
GitHub
github.com › esp-rs › esp-idf-hal › issues › 62
`FreeRtos::delay_us` will block for less than the given time for fractional milliseconds · Issue #62 · esp-rs/esp-idf-hal
April 15, 2022 - embedded_hal states that delay_us: Pauses execution for at minimum us microseconds. Pause can be longer if the implementation requires it due to precision/timing issues. The following line always rounds down, resulting in a much shorter ...
Author   esp-rs
🌐
ESP32 Forum
esp32.io › viewtopic.php
Nanosecond delay - ESP32 Forum
April 12, 2024 - For short delays, you can use esp_rom_delay_us(). A context switch on the ESP takes about 10-15us, so that's the lower bound of what's achievable for a task by waiting for a notification from a (timer) ISR; if you can accept the jitter, I'd say you should go for blocking waits instead of busy ...
🌐
GitHub
github.com › esp-rs › esp-idf-hal › issues › 199
General-Purpose Delay Provider · Issue #199 · esp-rs/esp-idf-hal
December 29, 2022 - For example, some device drivers require a delay implementation, and might use it to sleep anywhere from a few dozen microseconds to a few hundred milliseconds. As a workaround, I'm using something like this: use esp_idf_hal::delay::{Ets, FreeRtos}; ...
Author   esp-rs
🌐
Esp32developer
esp32developer.com › category › programming-in-c-c › timing
Timing – ESP32 Developer
Resources https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/peripherals/timer.html CMakeLists.txt REQUIRES Ensure the following is added to the REQUIRES section of your CMakeLists.txt file in the \main folder: Example using Timer […] Read More →
🌐
Espressif
docs.espressif.com › projects › esp-idf › en › v4.3 › esp32s2 › api-reference › system › esp_timer.html
High Resolution Timer - ESP32-S2 - — ESP-IDF Programming Guide v4.3 documentation
Delete an esp_timer instance. The timer must be stopped before deleting. A one-shot timer which has expired does not need to be stopped. ... Get time in microseconds since boot.
🌐
ESP32 Forum
esp32.com › viewtopic.php
IDF documentation suggests microsecond delay - should be millisec - ESP32 Forum
December 8, 2018 - In section "API Guide - Wi-Fi Driver" -- in sub-section "ESP32 Wi-Fi API Error Code" Currently: or recoverable errors, in which case you can write a recoverable-error code. For example, when esp_wifi_start returns ESP_ERR_NO_MEM, the recoverable-error code vTaskDelay can be called, in order to get a microseconds’ delay for another try.
🌐
TechOverflow
techoverflow.net › 2020 › 04 › 09 › esp-idf-equivalent-to-arduino-delay
ESP-IDF equivalent to Arduino delay() | TechOverflow
May 6, 2026 - This example delays by 500ms: ... You can use vTaskDelay() even if not using FreeRTOS tasks. For a full example, refer to PlatformIO ESP-IDF ESP32 blink example
🌐
ESP32 Forum
esp32.com › viewtopic.php
Wait microsecond - ESP32 Forum
ESP_Angus wrote:The RTOS tick period is (by default) 1ms, so vTaskDelay() will round this down to 0 ticks, and you'll either get no delay or a full time slice (1ms) delay while another task runs. It's not advisable to make the tick period any shorter than 1ms.
🌐
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 - void delay_us(uint64_t number_of_us){ esp_rom_delay_us((uint32_t)number_of_us); } Here the microprocessor waits util the time is passed. What of these approaches is the best? ... When using unsigned values, if you do the math in the right place, the overflow takes care of itself. So I think your first option could look like this: ... void delay_us(uint64_t number_of_us) { uint64_t microseconds = (uint64_t)esp_timer_get_time(); if (number_of_us) { while ((uint64_t)esp_timer_get_time() - microseconds < number_of_us) ; } } At first glance, this appears to be incorrect if `esp_timer_get_time()` overflows back to 0 before the difference reaches `number_of_us`. But this is not the case.
🌐
Esp32developer
esp32developer.com › programming-in-c-c › delays › delays
Delays – ESP32 Developer
If you select a value < portTICK_PERIOD_MS you may get a zero delay or you may get a delay of portTICK_PERIOD_MS (so 10mS).