yield() doesn't produce any reliable delay. If you try to use it on an UNO, then there's no delay at all since the UNO doesn't have any other threads that could run during that time. You need to read and understand Several Things at One Time Answer from MorganS on forum.arduino.cc
🌐
Arduino Forum
forum.arduino.cc › projects › programming
Yield vs Delay ? Can we call yield instead of delay when needs some delay - Programming - Arduino Forum
March 8, 2019 - Hello Everyone, I am working on making small datalogger that is used to save data for some time lets say 2-5 sec and at the same time for that specific task gpio must be doing something else. My code is down below for your refernce, I am trying to read IMU data for 2 sec and i want that during the two seconds I can do other work like turning on LED or anyother gpio based task.
🌐
Arduino Forum
forum.arduino.cc › projects › programming
Calling yield() or delay() - why would you use that in an infinite loop? - Programming - Arduino Forum
July 1, 2023 - [edited out of How to delay using millis() - #36 and created this new discussion so that we don't hijack the original thread] Your sketch works well; however, the yield() (is it a function or keyword?) brings the control back to case ALARM which I have verified by placing print() command as follows. case ALARM: Serial.println("Smoke Detected"); yield(); break; Smoke Detected Smoke Detected Smoke Detected Smoke Detected Smoke Detected I am not much familiar with yield()? Google...
Discussions

What does a delay(0) actually do? - Arduino Stack Exchange
If you pass 0 as the delay length then the delay() function essentially does nothing at all. Now it could be that an earlier version of the core had yield() always being called even with ms==0, but the current version doesn't. Maybe this is a hangup from an old coding style. More on arduino.stackexchange.com
🌐 arduino.stackexchange.com
November 6, 2021
yield(), esp_yield() or delay(0) ?
@TD-er just found this: esp8266/Arduino#5254 Apparently there is a difference between the three. And using the wrong one has the potential to crash the system. Were you aware of this ? I still need to dig deeper. If you know something ab... More on github.com
🌐 github.com
13
October 23, 2018
Yield and Delay
Hi, My application is mostly async and uses tickers and other callbacks to facilitate that. Question on yield() In these callbacks, the yielding is not possible. When one of the used libraries/fram... More on github.com
🌐 github.com
2
March 13, 2017
Trying to understand yield()
I saw a throwaway line about the yield() function in a thread I was following and, having never heard of it before, I wanted to learn about it. Unfortunately, almost everything that comes up on Google seems to talk about the ESP8266. Also, the Arduino Reference page on it isn't really helpful ... More on forum.arduino.cc
🌐 forum.arduino.cc
8
1
January 19, 2017
Top answer
1 of 2
44

However, I can call yield() on my Nano or ESP8266 without including the Scheduler lib

The yield() function is also implemented inside the ESP8266 libraries:

Yielding

This is one of the most critical differences between the ESP8266 and a more classical Arduino microcontroller. The ESP8266 runs a lot of utility functions in the background – keeping WiFi connected, managing the TCP/IP stack, and performing other duties. Blocking these functions from running can cause the ESP8266 to crash and reset itself. To avoid these mysterious resets, avoid long, blocking loops in your sketch.

The amazing creators of the ESP8266 Arduino libraries also implemented a yield() function, which calls on the background functions to allow them to do their things.

That's why you can call yield() from within your main program where the ESP8266 header is included.

See ESP8266 Thing Hookup Guide.

Update:

yield() is defined in Arduino.h as:

void yield(void);

yield() is also declared in hooks.h as follows:

/**
 * Empty yield() hook.
 *
 * This function is intended to be used by library writers to build
 * libraries or sketches that supports cooperative threads.
 *
 * Its defined as a weak symbol and it can be redefined to implement a
 * real cooperative scheduler.
 */
static void __empty() {
    // Empty
}
void yield(void) __attribute__ ((weak, alias("__empty")));

So, on the Nano, it probably does nothing (unless you have other libraries #included).

2 of 2
6

yield is a "weak" function from Arduino core for AVR. I see one call for it inside wiring.c.

void delay(unsigned long ms)
{
    uint32_t start = micros();

    while (ms > 0) {
        yield();
        while ( ms > 0 && (micros() - start) >= 1000) {
            ms--;
            start += 1000;
        }
    }
}

This means that the yield() function will be executed during the loop of delay function. Thus, yield would be used for some background processing while the delay ends or for doing a function with timeout feature.

Note: yield must be defined in application/sketch

UPDATE: The question made me excited to make a little post about yield and other hidden features from arduino core.

🌐
Everything ESP8266
esp8266.com › viewtopic.php
yield() and delay() best practices - Everything ESP8266
February 5, 2018 - That is why the link in the previous post states that yield cannot be called from the non-loop context, as that is not how it's designed to work as where would it yield to? Delay calls yield until such time as the delay has expired and then returns to the delay() calling function.
🌐
GitHub
github.com › letscontrolit › ESPEasy › issues › 1950
yield(), esp_yield() or delay(0) ? · Issue #1950 · letscontrolit/ESPEasy
October 23, 2018 - @TD-er just found this: esp8266/Arduino#5254 Apparently there is a difference between the three. And using the wrong one has the potential to crash the system. Were you aware of this ? I still need to dig deeper. If you know something ab...
Author   letscontrolit
🌐
GitHub
github.com › esp8266 › Arduino › issues › 3042
Yield and Delay · Issue #3042 · esp8266/Arduino
March 13, 2017 - Is there a specific reason for the "panic exit" instead of "just return" when "cont_can_yield(&g_cont)" returns false ? ... I know there should be no blocking functions in but some libs/functions use a delay(1) or delay(2) to send a pulse. In the delay function the esp_yield() is used but when cont_can_yield(&g_cont) returns false (in callback) there is no delay at all.
Author   esp8266
🌐
Arduino Forum
forum.arduino.cc › projects › programming
Trying to understand yield() - Programming - Arduino Forum
January 19, 2017 - I saw a throwaway line about the yield() function in a thread I was following and, having never heard of it before, I wanted to learn about it. Unfortunately, almost everything that comes up on Google seems to talk about the ESP8266. Also, the Arduino Reference page on it isn't really helpful (for me anyway) and only says it is for use with the scheduler.
Find elsewhere
🌐
GitHub
github.com › bogde › HX711 › issues › 73
Solution to ESP8266 wdt issue and "yield()" problem · Issue #73 · bogde/HX711
July 8, 2017 - My solution is to avoid the yield() function entirely and replace it with delay(1). delay() function allows the ESP to go off and do "wifi things", but appears more well behaved than yield() and has a definite time delay.
Author   bogde
🌐
ESP8266 Arduino Core
arduino-esp8266.readthedocs.io › en › 3.0.0 › reference.html
Reference — ESP8266 Arduino Core 3.0.0 documentation
WiFi and TCP/IP libraries get a ... adding a call to delay function to keep the WiFi stack running smoothly. There is also a yield() function which is equivalent to delay(0)....
🌐
GitHub
github.com › esp8266 › Arduino › issues › 1410
yield() effect in delay() · Issue #1410 · esp8266/Arduino
January 12, 2016 - How much coherent with the requested value is the delay time produced, and how much is affected by yield() and so execution of other tasks? Can this lead to overshoot the requested time? If this happens maybe yield() function should be c...
Author   esp8266
🌐
Blogger
8266iot.blogspot.com › p › dont-delay.html
Home Automation and IOT with the ESP8266 and Arduino IDE: Don't delay!
Oddly, delay(0) would also have worked. As would the special function yield() which does pretty much the same as delay(0). delay is specifically designed to "yield" the CPU, i.e.
🌐
GitHub
github.com › esp8266 › Arduino › issues › 2021
Q: Minimum delay/yield time · Issue #2021 · esp8266/Arduino
May 13, 2016 - Assuming my code is pretty efficient, would it be ok to just call yield() only and never actually delay any real amount of time?
Author   esp8266
🌐
Reddit
reddit.com › r/esp32 › why did changing from yield() to delay() fix a watchdog error?
r/esp32 on Reddit: Why did changing from yield() to delay() fix a watchdog error?
December 2, 2019 -

A task on my core 0 was causing a watchdog reset, so I changed the yield() to a delay(), and that fixed it. But I was under the impression that yield() should be sufficient to whack the watchdog, and I don't really =need= or want a delay here.

Any ideas? Sorry for the messy code, but I wanted to paste it verbatim! All the defines are true, so all the code is included.

void NetworkHandlingLoop(void *)
{
#if ENABLE_WEBSERVER
    TFTStatus("Starting Web Server..."); 
    debugI("Starting Web Server..."); 
    g_WebServer.begin(); 
    debugI("Web Server Started!");
#endif
    for (;;) 
    {
#if ENABLE_WIFI
        timeval tv;
        gettimeofday(&tv, nullptr); 
        int x = tv.tv_usec / 10000; 
        x = x % 50; //digitalWrite(BUILTIN_LED_PIN, x <= 5);
        static uint32_t lastTimeCheck = 0; 
        if (millis() - lastTimeCheck > TIME_CHECK_INTERVAL_MS) 
        { 
            lastTimeCheck = millis();     
            debugI("Refreshing Time from Server...");
            digitalWrite(BUILTIN_LED_PIN, 1); 
            g_NtpTimeClient.UnsetClock(); 
            g_NtpTimeClient.UpdateClockFromWeb(&g_Udp); 
            lastTimeCheck = millis(); 
#endif
#if ENABLE_OTA
        ArduinoOTA.handle();
#endif
#ifdef ENABLE_WIFI
        Debug.handle();
#endif
    delay(10); 
    } 
}
🌐
Stack Exchange
arduino.stackexchange.com › questions › 45023 › why-isnt-esp8266-connecting-to-wifi-when-calling-yield
Why isn't ESP8266 connecting to wifi when calling yield()? - Arduino Stack Exchange
September 24, 2017 - I read that ESP8266 has the watchdog automatically on, so I used yield() when spending time waiting, instead of using an empty while loop. I didn't use delay() because it block further execution of the code, and I tend to avoid it.
🌐
Arduino Forum
forum.arduino.cc › projects › programming
Trying to understand yield() - #2 by Robin2 - Programming - Arduino Forum
January 19, 2017 - I have never used it on an Arduino. Your last question is easy to answer. It is called within delay() to allow the Arduino to do useful work during the delay(). I confess I have no idea where the yield() function is defined or if is something that you must create yourself if you need it - similar to serialEvent(). My own recommendation is to stay clear of delay() and yield() (and serialEvent() ) and use millis() to manage timing as illustrated in Several Things at a Time.
🌐
Stack Overflow
stackoverflow.com › questions › 38976137 › equivalent-of-arduinos-yield-delay-for-esp8266-with-sming-framework
Equivalent of Arduino's yield(), delay() for ESP8266 with SMING framework - Stack Overflow
Does the SMING framework have equivalent functions for yield() and delay()? ... yield function which is equivalent with delay(0), is used for getting rid of WDT resets to give process priority to cpu time.
🌐
Arduino
arduino.cc › en › Reference › delay
delay() | Arduino Documentation
No other reading of sensors, mathematical calculations, or pin manipulation can occur during the delay function, so, in effect, it brings most other activity to a halt.