# Timing

**URL:** <https://forum.arduino.cc/t/timing/471455>\
**Category:** Programming\
**Created:** [July 22, 2017, 11:30am UTC](https://forum.arduino.cc/t/timing/471455 "2017-07-22T11:30:46Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![pAbel](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/pabel/32/1187404_2.png) [@pAbel](https://forum.arduino.cc/u/pAbel)\
**Post date:** [July 22, 2017, 11:30am UTC](https://forum.arduino.cc/t/timing/471455/1 "2017-07-22T11:30:46Z")

</div>

Hii

My question is if i make a pin high and suddenly make it low for how much time it will be in high state.

For example

digitalWrite (10, HIGH);  
digitalWrite (10,LOW);

In this pin 10 will be set for how much time?

Thanks

---

<div class="post-metadata">

**Author:** ![JaBa](https://avatars.discourse-cdn.com/v4/letter/j/6de8d8/32.png) [@JaBa](https://forum.arduino.cc/u/JaBa)\
**Post date:** [July 22, 2017, 11:53am UTC](https://forum.arduino.cc/t/timing/471455/2 "2017-07-22T11:53:29Z")

</div>

Undefined. First off, was it high to begin with? Second, the amount of time it takes to process the command and set the pin low depends on the clock Speed of the cpu. Third, what else is attached to the pin? Something that is trying to pull it one way or the other will affect the Timing. To find out, you will Need to put a scope on it.

---

<div class="post-metadata">

**Author:** ![pAbel](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/pabel/32/1187404_2.png) [@pAbel](https://forum.arduino.cc/u/pAbel)\
**Post date:** [July 22, 2017, 11:59am UTC](https://forum.arduino.cc/t/timing/471455/3 "2017-07-22T11:59:45Z")

</div>

first low then high then again low.

It is arduino uno so clock speed is 16MHz.

Nothing is connected to it.

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [July 22, 2017, 1:37pm UTC](https://forum.arduino.cc/t/timing/471455/4 "2017-07-22T13:37:03Z")

</div>

Someone measured digitalWrite() at about 6 microseconds. I would expect a pulse time near that.

If you need faster pulse times, use direct port manipulation (about 0.4 microseconds) or hardware timers (maybe down to 0.0625 microseconds?).

---

<div class="post-metadata">

**Author:** ![CrossRoads](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/crossroads/32/214033_2.png) [@CrossRoads](https://forum.arduino.cc/u/CrossRoads)\
**Post date:** [July 22, 2017, 2:02pm UTC](https://forum.arduino.cc/t/timing/471455/5 "2017-07-22T14:02:39Z")

</div>

Using direct port manipulation, you can get it down to one or two clock cycles, 62.5 or 125ns.

```cpp
// assuming it was low already, pulse D8 on an Uno high for the shortest time period:
PORTB = 0b00000001;
PORTB = 0b00000000;

```

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [July 22, 2017, 4:18pm UTC](https://forum.arduino.cc/t/timing/471455/6 "2017-07-22T16:18:09Z")

</div>

> CrossRoads:  
> Using direct port manipulation, you can get it down to one or two clock cycles, 62.5 or 125ns.
> 
> ```arduino
> // assuming it was low already, pulse D8 on an Uno high for the shortest time period:
> 
> ```

PORTB = 0b00000001;  
PORTB = 0b00000000;

> ```arduino
> 
> ```

Shouldn't that be:

```arduino
// assuming it was low already, pulse D8 on an Uno high for the shortest time period:
PINB = 0b00000001;  // Toggle D8
PINB = 0b00000001;  // Toggle it back

```

[/quote]

---

<div class="post-metadata">

**Author:** ![PaulMurrayCbr](https://avatars.discourse-cdn.com/v4/letter/p/74df32/32.png) [@PaulMurrayCbr](https://forum.arduino.cc/u/PaulMurrayCbr)\
**Post date:** [July 23, 2017, 9:15am UTC](https://forum.arduino.cc/t/timing/471455/7 "2017-07-23T09:15:47Z")

</div>

> johnwasser:  
> Shouldn't that be:
> 
> ```arduino
> // assuming it was low already, pulse D8 on an Uno high for the shortest time period:
> 
> ```

PINB = 0b00000001;&nbsp; // Toggle D8  
PINB = 0b00000001;&nbsp; // Toggle it back

> ```arduino
> 
> ```

PINX is for input only, afaik.

---

<div class="post-metadata">

**Author:** ![Whandall](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/whandall/32/222457_2.png) [@Whandall](https://forum.arduino.cc/u/Whandall)\
**Post date:** [July 23, 2017, 9:23am UTC](https://forum.arduino.cc/t/timing/471455/8 "2017-07-23T09:23:58Z")

</div>

> PaulMurrayCbr:  
> PINX is for input only, afaik.

Writing to PINx toggles all bits that are written with one (at least on a 328 or 2560).

---

<div class="post-metadata">

**Author:** ![GoForSmoke](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/goforsmoke/32/1127097_2.png) [@GoForSmoke](https://forum.arduino.cc/u/GoForSmoke)\
**Post date:** [July 23, 2017, 9:26am UTC](https://forum.arduino.cc/t/timing/471455/9 "2017-07-23T09:26:44Z")

</div>

> PaulMurrayCbr:  
> PINX is for input only, afaik.

Not so, read the AVR datasheets. A write to a PINx register bit will toggle the matching PORTx pin.  
It's a feature. You don't have to check a pin or use a mask to achieve fast pin change.

IIRC Nick Gammon uses that trick in his Arduino VGA tutorial.

BTW, the Arduino functions are slow because they assume a need to set the port up first which with direct port manipulation is the responsibility of the programmer.

---

<div class="post-metadata">

**Author:** ![westfw](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/westfw/32/213443_2.png) [@westfw](https://forum.arduino.cc/u/westfw)\
**Post date:** [July 23, 2017, 9:36am UTC](https://forum.arduino.cc/t/timing/471455/10 "2017-07-23T09:36:50Z")

</div>

See [Maximum pin toggle speed - Frequently-Asked Questions - Arduino Forum](http://forum.arduino.cc/index.php?topic=4324.0) for extensive (but perhaps out-of-date) discussion.

---

<div class="post-metadata">

**Author:** ![GoForSmoke](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/goforsmoke/32/1127097_2.png) [@GoForSmoke](https://forum.arduino.cc/u/GoForSmoke)\
**Post date:** [July 23, 2017, 9:47am UTC](https://forum.arduino.cc/t/timing/471455/11 "2017-07-23T09:47:04Z")

</div>

LOL, I was running IDE 0023 clear up until IDE 1.05 was out just because the changes that killed my breadboard duino programming method, iffy as it was.

No longer kicking and screaming, Nick saved the day!

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [July 23, 2017, 12:05pm UTC](https://forum.arduino.cc/t/timing/471455/12 "2017-07-23T12:05:48Z")

</div>

In response to a question in this Forum, the execution time of various Arduino Instructions were measured and reported. The experimental setup was:

Timer-1 of TC1 was started at clkTc1 = 16 MHz just before the execution of the target instruction (say, digitalWrite(4, HIGH). The Timer-1 was stopped just after the execution of the target instruction. The execution time of the target instruction was equal to clcTC1 pulses so accumulated by Timer-1. The following timings were measured:

**1.** digitalWrite(4, HIGH); : 59 cycles ===\> 3.687 uS  
**2.** digitalWrite(4, LOW); : 61 cycles ===\> 3.812 us  
**3.** DDRD = 0b00001000; : 4 cycles ===\> 0.250 us

//--------------------------------------------------------------------------------------------------

[exet.doc](https://forum.arduino.cc/uploads/short-url/w2Iye3aVDAhZlDjSTBe2V7hcNK8.doc) (44 KB)

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [July 23, 2017, 2:59pm UTC](https://forum.arduino.cc/t/timing/471455/13 "2017-07-23T14:59:02Z")

</div>

> GolamMostafa:  
> **1.** digitalWrite(4, HIGH); : 59 cycles ===\> 3.687 uS  
> **2.** digitalWrite(4, LOW); : 61 cycles ===\> 3.812 us  
> **3.** DDRD = 0b00001000; : 4 cycles ===\> 0.250 us

Note: Setting the DDR (Data Direction Register) is equivalent to pinMode(), not digitalWrite(). Setting the register to 0b00001000 will set Pin 4 as an OUTPUT, but it will also set the other 7 pins as INPUT, thus it is not a valid substitute for pinMode(4, OUTPUT). You need bitSet(DDRD, 4);

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [July 23, 2017, 4:46pm UTC](https://forum.arduino.cc/t/timing/471455/14 "2017-07-23T16:46:30Z")

</div>

> You need bitSet(DDRD, 4);

You are absolutely right; but, my examples codes were demonstrative codes and were intended to collect only the execution times. The three instructions of my example were no way linked/related to each other.

---

<div class="post-metadata">

**Author:** ![PaulMurrayCbr](https://avatars.discourse-cdn.com/v4/letter/p/74df32/32.png) [@PaulMurrayCbr](https://forum.arduino.cc/u/PaulMurrayCbr)\
**Post date:** [July 24, 2017, 4:46am UTC](https://forum.arduino.cc/t/timing/471455/15 "2017-07-24T04:46:06Z")

</div>

> Whandall:  
> Writing to PINx toggles all bits that are written with one (at least on a 328 or 2560).

> GoForSmoke:  
> Not so, read the AVR datasheets. A write to a PINx register bit will toggle the matching PORTx pin.

! That's handy. You learn something every day.

---

<div class="post-metadata">

**Author:** ![pAbel](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/pabel/32/1187404_2.png) [@pAbel](https://forum.arduino.cc/u/pAbel)\
**Post date:** [July 24, 2017, 5:02am UTC](https://forum.arduino.cc/t/timing/471455/16 "2017-07-24T05:02:46Z")

</div>

Thanks to all.

I got my answer.

---

<div class="post-metadata">

**Author:** ![pAbel](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/pabel/32/1187404_2.png) [@pAbel](https://forum.arduino.cc/u/pAbel)\
**Post date:** [July 24, 2017, 10:19am UTC](https://forum.arduino.cc/t/timing/471455/17 "2017-07-24T10:19:04Z")

</div>

Hii

I have another doubt. In arduino SPI sclk frequency is how much and what is the low time and high time ?

Same arduino uno board and 16 MHz clock.

---

<div class="post-metadata">

**Author:** ![GoForSmoke](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/goforsmoke/32/1127097_2.png) [@GoForSmoke](https://forum.arduino.cc/u/GoForSmoke)\
**Post date:** [July 24, 2017, 10:25am UTC](https://forum.arduino.cc/t/timing/471455/18 "2017-07-24T10:25:33Z")

</div>

SPI clock has a system clock divider, the default is 4.

If you run at 16MHz the default SPI is 4MHz, one byte takes 8x4 cycles to transmit.

It is best to run SPI through the SPI hardware port. Next best is using a serial port to do master-mode SPI and last is bit-banging your own SPI in software, which won't run nearly as fast.

Do you know that hardware SPI is bi-directional? It can read data bits in as it clocks data bits out.

Nick Gammon has a tutorial on the subject that includes code to make an Arduino work as and SPI slave device.

> **[Gammon Forum : Electronics : Microprocessors : SPI - Serial Peripheral...](http://www.gammon.com.au/spi)**
>
> Gammon Forum : Electronics : Microprocessors : SPI - Serial Peripheral Interface - for Arduino

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 10:38pm UTC](https://forum.arduino.cc/t/timing/471455/19 "2021-05-05T22:38:52Z")

</div>

Closed on May 5, 2021, 10:38pm UTC


