Arduino delayMicroseconds() Function Tutorial

In this tutorial, you’ll learn how to use the Arduino delayMicroseconds() function and how it actually works. We’ll also investigate the accuracy of the Arduino delayMicroseconds() function and do some measurements to reach its fundamental limitations of it. And we’ll discuss what would be a better alternative if needed by your application.

Without further ado, let’s get right into it!

Table of Contents

  1. The Need For delayMicroseconds in Arduino
  2. Arduino delayMicroseconds() Function
  3. Arduino delayMicroseconds Example
  4. Arduino delayMicroseconds Accuracy Tests
  5. Concluding Remarks

The Need For delayMicroseconds in Arduino

In a previous tutorial, we’ve discussed in detail the Arduino delay() function and how to use it in your Arduino projects to generate time delays in milliseconds and up to 25 days. But in this tutorial, we’ll go down to very precise time delay intervals generation down to 1µs and measure the accuracy of the Arduino delayMicroseconds built-in function.

Occasionally, you might need to insert a very short time delay to achieve certain functionality for your Arduino projects. Things like short pulse generation, triggering a sensor or external electronic circuit, or forcing the CPU to wait for a short time period before attempting to execute a certain piece of logic.

Despite the fact that using a delay function is not generally preferred, it’s still a very popular option for various use cases. And it can be acceptable as long as it’s a few µs time delay.


Arduino delayMicroseconds() Function

Description

The delayMicroseconds() function pauses the program for the amount of time (in microseconds) specified as a parameter. There are 1000 microseconds in a millisecond and 1,000,000 microseconds in a second.

Syntax

Parameters

us: the number of microseconds to pause. Allowed data types: unsigned int

❕ Note

Currently, the largest value that will produce an accurate delay is 16383. Larger values can produce an extremely short delay. This could change in future Arduino releases. For delays longer than a few thousand microseconds, you should use the  delay() function instead.


Arduino delayMicroseconds Example

Now, we’ll test the delayMicroseconds() function with a very simple short pulse generation example.

Short Pulse Generation With delayMicroseconds

We’ll drive an Arduino output pin to HIGH and insert a short delay of 100µs, and then drive it back to LOW. And the resulting short pulse will be measured with an oscilloscope to make sure it has “nearly” the desired pulse width of 100µs.

Example Code

Here is the full code listing for this example.

Code Explanation

The code example simply sets pin 8 to be output and sends output signal High and Low with 100µs time delay after each state change.

setup()

in the setup() function, we initialize the digital IO pin 8 to be an OUTPUT pin.

loop()

in the loop() function, we use the digitalWrite() function to set the pin state to high and low. And we also use the delayMicroseconds() function to insert a 100µs time delay after each state change. And we also insert a 100ms time delay after each pulse.

Simulation

Here is the simulation result for this project on the Proteus simulator (it can be helpful if you don’t have an oscilloscope in your lab yet).

Arduino-delayMicroseconds-Example-Simulation

The resulting pulse is nearly 100µs as we expect with a slight error of around 1.5µs.

???? Also Read
Arduino Proteus Library Simulation Guide

This article will give more in-depth information about using proteus ISIS for Arduino projects simulation.

Testing Results

Here is the result of testing this project on my Arduino UNO board as captured by my DSO (digital storage oscilloscope). It’s nearly the same as what we’ve seen in the simulation environment.

Arduino-delayMicroseconds-Example-Test

Interesting Finding

The behavior I’ll describe here is consistent and easily observable in both simulation environment and in real-world testing. Which is a significant fluctuation in the resulting pulse width (around 10µs), and it’s sporadic in time.

It turned out to be due to having interrupts that pause the delayMicroseconds() function execution causing it to take more time than expected in a non-deterministic way. One solution can be to disable interrupts before the “short microseconds delay” and re-enable interrupts afterward. Which is not recommended actually. But for the sake of testing, I’ll modify the code and run the test again.

Here is the modified code example.

After running the example code above, it did eliminate the sporadic fluctuation in the delay microseconds period (pulse width). But as stated earlier, disabling interrupts for critical sections in your code is something that you need to be very careful with or avoid as much as possible.


Arduino delayMicroseconds Accuracy Tests

In this section, we’ll do some further test procedures to measure the accuracy of the Arduino delayMicroseconds function.

delayMicroseconds Accuracy Measurement Test1

In this testing procedure, we’ll use the built-in timer-based micros() function to find out the execution time of the delay generated by the delayMicroseconds function. Given that the micros() function output is always multiples of 4, we’ll select a time delay of 100µs so it doesn’t affect the results we’ll be getting.

Here is the code example for this test.

And here is the result for running this code on my Arduino UNO board.

Arduino delayMicroseconds Accuracy Test

Do you remember from the previous example the sporadic fluctuation in the generated time delay? Here it’s also visible again. And we’ll eliminate it with the exact same way of creating a critical section for the delayMicroseconds function call (by disabling/enabling Interrupts).

And here is the modified test code example.

And here is the result for running the modified test code example above. It’s a little bit better but still, there is a consistent random 4µs error. Which will stay the same whether you increase or decrease the delayMicroseconds time period. I went up to a 600µs time delay and it was printing 600s and 604s as well. So it’s something consistent and I believe it’s inherent in the micros() function implementation itself.

Arduino delayMicroseconds Accuracy Measurement

delayMicroseconds Accuracy Measurement Test2

In this test procedure, I’ll replace the micros() function calls with direct pin access to eliminate the execution time effect of the digitalWrite() function and also avoid any effect introduced by the implementation of the micros function itself.

And I’ll run the test code on both the simulation environment and a real Arduino board with an oscilloscope to check if things are going to align or not.

Here is the code example for this test procedure. Note that I’m using PORTD bit 3 which corresponds to Arduino digital IO pin number 3.

And here is the result of running this test code in both simulation and on a real Arduino UNO board.

Real Arduino + DSO Result Proteus Simulation Result
Arduino delayMicroseconds Accuracy Measurement 2 Arduino delayMicroseconds Accuracy Test2

Which is quite an interesting result. Both pulses are very similar 98.6µs and 98.5µs. And also less than the expected 100µs pulse width.

Then, I proceeded with changing the delayMicroseconds input time delay down to 1µs. And here are the results I’ve got on my DSO after measuring the pulse width in each test case.

Test CaseMeasured Pulse Width on DSO
delayMicroseconds(100);98.6µs
delayMicroseconds(10);9µs
delayMicroseconds(2);0.9µs
delayMicroseconds(1);0.2µs

To conclude this accuracy testing section, we can say that the Arduino delayMicroseconds function is pretty much accurate for general-purpose delay applications. However, there still are some fundamental limitations to what you can expect from this function and you’d be better off implementing your own delay routine if you need to have an extremely accurate and predictable behavior in your target application.

Otherwise, the delayMicroseconds will serve you well in most applications where a delay of a few µs is required and nobody is really concerned about the accuracy (or consistency) of the generated delay intervals.


Parts List

Here is the full components list for all parts that you’d need in order to perform the practical LABs mentioned here in this article and for the whole Arduino Programming series of tutorials found here on DeepBlueMbedded. Please, note that those are affiliate links and we’ll receive a small commission on your purchase at no additional cost to you, and it’d definitely support our work.

This is the main DSO that I usually use in my lab in case you need one for your lab. You definitely need to check it out!

Amazon.com   – ebay.comAliExpress

Download Attachments

You can download all attachment files for this Article/Tutorial (project files, schematics, code, etc..) using the link below. Please consider supporting my work through the various support options listed in the link down below. Every small donation helps to keep this website up and running and ultimately supports our community.


Concluding Remarks

To conclude this tutorial, we can say that the Arduino delayMicroseconds function is pretty much accurate for general-purpose delay applications. However, there still are some fundamental limitations to what you can expect from this function and you’d be better off implementing your own delay routine if you need to have an extremely accurate and predictable behavior in your target application.

Using the delay() and delayMicroseconds() functions is pretty much easy. But you need to be very careful and use them reasonably because it can lead to major timing issues in your system. And in most cases, you’d be better off avoiding using delays altogether.

In the following cases, it’d be acceptable to use delays in your Arduino projects:

  • During initialization, to make sure that a software component has completed initialization successfully
  • If you purposefully need the CPU to be blocked waiting for a certain event
  • If the delay time is very short like 10µs or something in that range

If you’re just getting started with Arduino, you need to check out the Arduino Getting Started [Ultimate Guide] here. And follow this Arduino series of tutorials to learn more about Arduino Programming.

???? Also Read
Getting Started With Arduino Programming For Beginners

This is the ultimate guide for getting started with Arduino for beginners. It’ll help you learn the Arduino fundamentals for Hardware & Software and understand the basics required to accelerate your learning journey with Arduino Programming.


FAQ & Answers

Which timer does delayMicroseconds() use?

The delayMicroseconds() function doesn’t use a hardware timer, instead, it used the CPU clock cycles count. On the other hand, the [ delay() – micros() – millis() ] functions use Timer0 to work (this is for Arduino UNO, Mega, etc).

What is delayMicroseconds () in Arduino?

The Arduino delayMicroseconds() function is a built-in function that pauses the CPU for a short time interval (in µs). Currently, the largest value that will produce an accurate delay is 16383. Larger values can produce an extremely short delay. For delays longer than a few thousand microseconds, you should use the  delay() function instead.

How accurate is Arduino delayMicroseconds?

You can get down to 1µs time delay using the Arduino delayMicroseconds() function. It’s pretty much accurate but you need to pay attention to the fact that any enabled interrupt can interfere with the operation of the delayMicroseconds function which is going to prolong the delay period. And potentially causing it to generate inconsistent time delay intervals. Creating a critical section around the delayMicroseconds function call will solve the issue but needs careful implementation.

What is Arduino delayMicroseconds max value?

Currently, the largest value that will produce an accurate delay is 16383. Larger values can produce an extremely short delay. For delays longer than a few thousand microseconds, you should use the  delay() function instead.

Share This Page With Your Network!
Join Our +25,000 Newsletter Subscribers!

Stay Updated With All New Content Releases. You Also Get Occasional FREE Coupon Codes For Courses & Other Stuff!

Photo of author
Author
Khaled Magdy
Embedded systems engineer with several years of experience in embedded software and hardware design. I work as an embedded SW engineer in the Automotive & e-Mobility industry. However, I still do Hardware design and SW development for DSP, Control Systems, Robotics, AI/ML, and other fields I'm passionate about.
I love reading, writing, creating projects, and teaching. A reader by day and a writer by night, it's my lifestyle. I believe that the combination of brilliant minds, bold ideas, and a complete disregard for what is possible, can and will change the world! I will be there when it happens, will you?

Leave a Comment