I would like to generate a TTL signal using an Arduino.
As far as I understand, a TTL signal is just a predefined controlled signal that repeats at a certain interval (e.g. 2 μs).
An Arduino circuit arguably has a very fast clock (e.g. 16 MHz), so what I thought is I can basically just draw a square signal for whatever desirable interval (important to be down to the microsecond).
I used the following code, choosing a pin and changing its signal state between 0 and 1 and then using Serial.println to draw the signal out. Everything is inside a while loop that times everything to be generated exactly within 1 second.
#include <arduino-timer.h> // Variables unsigned long v = -1; int freq = 100.0; // Hz unsigned long d = floor(1.0/freq*1000000.0) ; // us Timer<1, micros> timer; // create a timer with 1 task and microsecond resolution void setup() { unsigned long repeatEvery = 5000000; // Microseconds Serial.begin(9600); pinMode(3, OUTPUT); timer.every(repeatEvery, func1); // XXXXX microsec } void func1() { unsigned long uSec = 1000000; // Microseconds (= 1 sec) PORTD = B00000000; v = digitalRead(3); unsigned long counter = 0, i = 0; auto currentTime = micros(); auto prevTime = currentTime; d = 0; // no delay to check the speed of the execution do { PORTD = B00001000; v = digitalRead(3); // Serial.println(v); PORTD = B00001000; v = digitalRead(3); // Serial.println(v); delayMicroseconds(d); // first frame PORTD = B00000000; v = digitalRead(3); // Serial.println(v); PORTD = B00000000; v = digitalRead(3); // Serial.println(v); delayMicroseconds(d); // second frame currentTime = micros(); i = (unsigned long)(currentTime - prevTime); counter++; } while(i < uSec); Serial.print(">>>Counter:"); Serial.println(counter); Serial.print(">>>delay (ms):"); Serial.println(d); } void loop() { timer.tick(); } The code above will give output as counter = 47620 // how many times the instructions were repeated and if the Serial.println lines were uncommented to draw the signal then the execution time slows down to counter = 86
I am not sure if the whole train of thought is wrong and I should do something else or use a different circuit.
Any hint or help would be highly appreciated.
PS: my Arduino by the way is a UNIROI that is supposed to be the same as an Arduino R3.

TTL signal is just a predefined controlled signal that repeats at a certain interval... no TTL is a logic level that is 5 V when high and 0 V when low ... it does not necessarily repeat ... it could easily always be high