# Fastest output possible

**URL:** <https://forum.arduino.cc/t/fastest-output-possible/318289>\
**Category:** Programming\
**Created:** [June 16, 2015, 4:05am UTC](https://forum.arduino.cc/t/fastest-output-possible/318289 "2015-06-16T04:05:38Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![notfet](https://avatars.discourse-cdn.com/v4/letter/n/9de0a6/32.png) [@notfet](https://forum.arduino.cc/u/notfet)\
**Post date:** [June 16, 2015, 4:05am UTC](https://forum.arduino.cc/t/fastest-output-possible/318289/1 "2015-06-16T04:05:38Z")

</div>

I need an output to test a clock circuit on a machine preferably 16MHz but since the Arduino clock is that speed I know that is not possible. However since it is for testing I will take the fastest I can get. I know that pwm works by taking the clock speed and dividing it by a multiplier 1,2,4,etc and counting 0-254 but that is too slow. I chose the simplest code I could think of...

```cpp
void setup(){
  DDRA = B00000001;
}

void loop(){
  PORTA = B00000001;
  PORTA = B00000000;}

```

thinking that since it is only 2 lines that I would have a clock speed of 8 MHz However my scope shows 1 MHz. Is this because I am using the Arduino Mega? Is there a way to make an output faster?

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [June 16, 2015, 4:13am UTC](https://forum.arduino.cc/t/fastest-output-possible/318289/2 "2015-06-16T04:13:41Z")

</div>

You may be able to get 16Mhz by connecting the clock output (XTAL2) to a cmos inverter and then use its output for your project.

.

---

<div class="post-metadata">

**Author:** ![cattledog](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/cattledog/32/217816_2.png) [@cattledog](https://forum.arduino.cc/u/cattledog)\
**Post date:** [June 16, 2015, 4:29am UTC](https://forum.arduino.cc/t/fastest-output-possible/318289/3 "2015-06-16T04:29:28Z")

</div>

I don't know if it is any faster, or if the compiler makes it all the same in the end, but you can use the avr special write sequence to PINX to toggle a pin or pins.

```cpp
void setup(){
  DDRA = B00000001;
}

void loop(){
  //PORTA = B00000001;
 // PORTA = B00000000;
PINA = 1;}

```

---

<div class="post-metadata">

**Author:** ![notfet](https://avatars.discourse-cdn.com/v4/letter/n/9de0a6/32.png) [@notfet](https://forum.arduino.cc/u/notfet)\
**Post date:** [June 16, 2015, 4:33am UTC](https://forum.arduino.cc/t/fastest-output-possible/318289/4 "2015-06-16T04:33:23Z")

</div>

Since that pin is not accessible, It would mean soldering it to a proto-type board on top of an arduino Uno.Not sure where I could access it on my Mega. I could try that but would a 3" lead to the inverter affect the frequency? If at all possible I would like to use the internal output by using better code that I could think of. But I will try it. And I have never heard of PINX could you give an example?

---

<div class="post-metadata">

**Author:** ![cattledog](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/cattledog/32/217816_2.png) [@cattledog](https://forum.arduino.cc/u/cattledog)\
**Post date:** [June 16, 2015, 4:44am UTC](https://forum.arduino.cc/t/fastest-output-possible/318289/5 "2015-06-16T04:44:19Z")

</div>

```cpp
void setup() {
 //sketch written for port/pin architecture of Arduino Uno atmega 328
 //blink led
  DDRB = B00100000; //set pin 13, led to output (PORTB, pin5)
}

void loop() {

 PINB = B00100000;
 
 delay(200);
}

```

---

<div class="post-metadata">

**Author:** ![notfet](https://avatars.discourse-cdn.com/v4/letter/n/9de0a6/32.png) [@notfet](https://forum.arduino.cc/u/notfet)\
**Post date:** [June 16, 2015, 4:52am UTC](https://forum.arduino.cc/t/fastest-output-possible/318289/6 "2015-06-16T04:52:20Z")

</div>

That ended up slower 531.9KHz

---

<div class="post-metadata">

**Author:** ![nickgammon](https://avatars.discourse-cdn.com/v4/letter/n/45deac/32.png) [@nickgammon](https://forum.arduino.cc/u/nickgammon)\
**Post date:** [June 16, 2015, 4:53am UTC](https://forum.arduino.cc/t/fastest-output-possible/318289/7 "2015-06-16T04:53:09Z")

</div>

On the Uno this will output 8 MHz on pin 9:

```cpp
void setup ()
  {
  // set up 8 MHz timer on pin 9
  pinMode (9, OUTPUT); 
  // set up Timer 1
  TCCR1A = bit (COM1A0);  // toggle OC1A on Compare Match
  TCCR1B = bit (WGM12) | bit (CS10);  // CTC, no prescaling
  OCR1A =  0;      // output every cycle
  }  // end of setup

void loop () { }

```

For the Mega2560 it is pin 11:

```cpp
void setup ()
  {
  // set up 8 MHz timer on pin 11
  pinMode (11, OUTPUT); 
  // set up Timer 1
  TCCR1A = bit (COM1A0);  // toggle OC1A on Compare Match
  TCCR1B = bit (WGM12) | bit (CS10);  // CTC, no prescaling
  OCR1A =  0;      // output every cycle
  }  // end of setup

void loop () { }

```

---

<div class="post-metadata">

**Author:** ![nickgammon](https://avatars.discourse-cdn.com/v4/letter/n/45deac/32.png) [@nickgammon](https://forum.arduino.cc/u/nickgammon)\
**Post date:** [June 16, 2015, 5:03am UTC](https://forum.arduino.cc/t/fastest-output-possible/318289/8 "2015-06-16T05:03:22Z")

</div>

> ```arduino
> PORTA = B00000001;
> 
> ```

PORTA = B00000000;

> ```arduino
> 
> thinking that since it is only 2 lines that I would have a clock speed of 8 MHz However my scope shows 1 MHz. Is this because I am using the Arduino Mega? Is there a way to make an output faster?
> 
> ```

That is a lot more than 2 cycles.

Loop is doing this (cycle counts in brackets):

```arduino
0000012c <loop>:
 12c:  81 e0          ldi    r24, 0x01      ; 1  (1)
 12e:  82 b9          out    0x02, r24      ; 2  (1)
 130:  12 b8          out    0x02, r1        ; 2  (1)
 132:  08 95          ret  (4)

```

7 cycles there.

And to call loop takes more cycles in main:

```arduino
 144:  f3 df          rcall  .-26            ; 0x12c <loop>  (3)
 146:  20 97          sbiw    r28, 0x00      ; 0  (2)
 148:  e9 f3          breq    .-6            ; 0x144 <main+0xe>  (1/2)
 14a:  0e 94 00 00    call    0      ; 0x0 <__vectors>  (4)
 14e:  fa cf          rjmp    .-12            ; 0x144 <main+0xe>  (2)

```

9 cycles there.

Total of 16 cycles. Thus your 1 MHz output. (And the duty cycle would not be 50%).

---

<div class="post-metadata">

**Author:** ![cattledog](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/cattledog/32/217816_2.png) [@cattledog](https://forum.arduino.cc/u/cattledog)\
**Post date:** [June 16, 2015, 5:09am UTC](https://forum.arduino.cc/t/fastest-output-possible/318289/9 "2015-06-16T05:09:11Z")

</div>

```cpp
PINA = B00000001;

```

Nick--Why is this slower than the two line PORTA code?

---

<div class="post-metadata">

**Author:** ![Coding\_Badly](https://avatars.discourse-cdn.com/v4/letter/c/f14d63/32.png) [@Coding\_Badly](https://forum.arduino.cc/u/Coding_Badly)\
**Post date:** [June 16, 2015, 5:36am UTC](https://forum.arduino.cc/t/fastest-output-possible/318289/10 "2015-06-16T05:36:08Z")

</div>

> I need an output to test a clock circuit on a machine preferably 16MHz but since the Arduino clock is that speed I know that is not possible.

~~Of course it is. Enable the Clock output on PORTE7 fuse.~~

Never mind. That pin is not brought out to a header. (Unless you want to try your hand soldering an itty bitty wire.)

---

<div class="post-metadata">

**Author:** ![nickgammon](https://avatars.discourse-cdn.com/v4/letter/n/45deac/32.png) [@nickgammon](https://forum.arduino.cc/u/nickgammon)\
**Post date:** [June 16, 2015, 5:52am UTC](https://forum.arduino.cc/t/fastest-output-possible/318289/11 "2015-06-16T05:52:27Z")

</div>

> cattledog:
> 
> ```arduino
> PINA = B00000001;
> 
> ```
> 
> Nick--Why is this slower than the two line PORTA code?

In the set/clear scenario, the overhead of calling loop is spread over one cycle. In other words:

set pin / clear pin ... then 9 cycles overhead.

In the toggle scenario, the overhead of calling loop is doubled (one cycle is a set or clear). Thus you effectively have 18 cycles of overhead rather than 9.

With the PINA = 1 method loop is actually slightly shorter:

```arduino
000000ac <loop>:
  ac:  80 e2          ldi    r24, 0x20      ; 32  (1)
  ae:  83 b9          out    0x03, r24      ; 3  (1)
  b0:  08 95          ret  (4)

```

So one "cycle" is going to be (9 + 6) \* 2 = 30 clock cycles.

```arduino
 1 / (1/16e6 * 30) = 533333 Hz

```

> notfet:  
> That ended up slower 531.9KHz

Pretty close to the prediction. 🙂

---

<div class="post-metadata">

**Author:** ![nickgammon](https://avatars.discourse-cdn.com/v4/letter/n/45deac/32.png) [@nickgammon](https://forum.arduino.cc/u/nickgammon)\
**Post date:** [June 16, 2015, 5:56am UTC](https://forum.arduino.cc/t/fastest-output-possible/318289/12 "2015-06-16T05:56:51Z")

</div>

What is a lot faster is to do this:

```cpp
 while (true)
  PINB = bit (5);

```

That gets you up to 2.6 MHz.

```cpp
  ac:  80 e2          ldi    r24, 0x20      ; 32  (1)
  ae:  83 b9          out    0x03, r24      ; 3  (1)
  b0:  fe cf          rjmp    .-4            ; 0xae <loop+0x2>  (2)

```

Theory ... time taken to do the 3 cycles in that loop (twice):

```cpp
 1 / (1/16e6 * 6) = 2666666 Hz

```

---

<div class="post-metadata">

**Author:** ![nickgammon](https://avatars.discourse-cdn.com/v4/letter/n/45deac/32.png) [@nickgammon](https://forum.arduino.cc/u/nickgammon)\
**Post date:** [June 16, 2015, 5:57am UTC](https://forum.arduino.cc/t/fastest-output-possible/318289/13 "2015-06-16T05:57:48Z")

</div>

The hardware timer is still faster, of course.

---

<div class="post-metadata">

**Author:** ![DrAzzy](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@DrAzzy](https://forum.arduino.cc/u/DrAzzy)\
**Post date:** [June 16, 2015, 6:23am UTC](https://forum.arduino.cc/t/fastest-output-possible/318289/14 "2015-06-16T06:23:52Z")

</div>

If you have an ISP programmer or second arduino (to run arduinoasisp) you could set the CLKO fuse (low fuse for '328(p) would be 0xBF if using external 16mhz crystal) and get the system clock output on arduino pin 8 (physical pin 14, PB0).

---

<div class="post-metadata">

**Author:** ![Coding\_Badly](https://avatars.discourse-cdn.com/v4/letter/c/f14d63/32.png) [@Coding\_Badly](https://forum.arduino.cc/u/Coding_Badly)\
**Post date:** [June 16, 2015, 6:46am UTC](https://forum.arduino.cc/t/fastest-output-possible/318289/15 "2015-06-16T06:46:43Z")

</div>

> DrAzzy:  
> low fuse for '328(p) ... and get the system clock output on arduino pin 8 (physical pin 14, PB0).

Wrong processor. See original post and reply #9.

---

<div class="post-metadata">

**Author:** ![DrAzzy](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@DrAzzy](https://forum.arduino.cc/u/DrAzzy)\
**Post date:** [June 16, 2015, 7:16am UTC](https://forum.arduino.cc/t/fastest-output-possible/318289/16 "2015-06-16T07:16:18Z")

</div>

Oops - somehow I managed to miss that...

---

<div class="post-metadata">

**Author:** ![notfet](https://avatars.discourse-cdn.com/v4/letter/n/9de0a6/32.png) [@notfet](https://forum.arduino.cc/u/notfet)\
**Post date:** [June 16, 2015, 11:05am UTC](https://forum.arduino.cc/t/fastest-output-possible/318289/17 "2015-06-16T11:05:27Z")

</div>

Nick your explanation is clear I see how my example was 1 MHz and after loading your code The scope is 8 MHz. 🙂 🙂 I still may try the soldering method unless there is something faster. But as per post#1 this may be the fastest software I can expect.

---

<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:** [June 17, 2015, 2:37am UTC](https://forum.arduino.cc/t/fastest-output-possible/318289/18 "2015-06-17T02:37:10Z")

</div>

> [@](#):
>
> ~~Of course it is. Enable the Clock output on PORTE7 fuse.~~
> 
> Never mind. That pin is not brought out to a header. (Unless you want to try your hand soldering an itty bitty wire.)

That pin can be available if you build/breadboard your own duino.

---

<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, 9:20pm UTC](https://forum.arduino.cc/t/fastest-output-possible/318289/19 "2021-05-05T21:20:38Z")

</div>

Closed on May 5, 2021, 9:20pm UTC


