Arduino PWM Generating Problem

Below is my code for Arduino to output 10 kHz frequency PWM with 50% duty cycle from Pin #11 while using A1 as a Trigger to stop or start the looping of the code.

I need to read A1 to see if it reads ~5V or around ~0V. I need to stop the looping for PWM when A1 is reading ~0V.

Problem is that I could not achieve 10 kHz frequency, but only getting around 4.5 kHz with this particular code.

May I know what is wrong and how can it be corrected?
Should I need to use analogWrite with Timer library?

int TTL1 = 11;
int Trigger = A1;
int TriggerRead = 0;


//This tells the Arduino to treat the pins as output. In this example, all channels are output. 
//For an example with input (closed loop), see the Arduino-Simple_Closed_Loop.ino file section.
void setup() {
  analogReference(DEFAULT);
  pinMode(TTL1, OUTPUT);
  pinMode(Trigger, INPUT);
}

//Define the number of conditions. Define the length of time (in ms) for each condition.
int con9 = 50;  // 1/2 of .0001 seconds for 10,000 Hz  (50 Microseconds per half cycle)

int i;

//This is the code that the Arduino loops through. 
//It will start on condition 1 and then move through the rest of the conditions and then auto restart back at condition 1.
//It writes each pin as High/ON or Low/Off for each condition, and the waits for the duration of condition (set above).
// if ( && and / || or / ! not)
void loop() 
{
  TriggerRead = analogRead(Trigger);
  if (TriggerRead < 130) // <1V in
  {
    digitalWrite(TTL1, LOW);
    TriggerRead = analogRead(Trigger);
  }

  if (TriggerRead > 512) // >2.5V in
  {
    //Condition 1
    digitalWrite(TTL1, HIGH); 
    delayMicroseconds(con9);

    //Condition 2
    TriggerRead = analogRead(Trigger);
    digitalWrite(TTL1, LOW); 
    delayMicroseconds(con9);
  }
}

The analog pins will work just fine as digital pins so you can just use digitalRead() on that pin to get HIGH/LOW.

const int TTL1 = 11;
const int Trigger = A1;


//This tells the Arduino to treat the pins as output. In this example, all channels are output.
//For an example with input (closed loop), see the Arduino-Simple_Closed_Loop.ino file section.
void setup() {
  analogReference(DEFAULT);
  pinMode(TTL1, OUTPUT);
  pinMode(Trigger, INPUT);
}

//Define the number of conditions. Define the length of time (in ms) for each condition.
const long con9 = 50;  // 1/2 of .0001 seconds for 10,000 Hz  (50 Microseconds per half cycle)

int i;

//This is the code that the Arduino loops through.
//It will start on condition 1 and then move through the rest of the conditions and then auto restart back at condition 1.
//It writes each pin as High/ON or Low/Off for each condition, and the waits for the duration of condition (set above).
// if ( && and / || or / ! not)
void loop()
{
  int TriggerRead = digitalRead(Trigger);
  if (TriggerRead  == LOW)
  {
    digitalWrite(TTL1, LOW);
  }
  else 
  {
    //Condition 1
    digitalWrite(TTL1, HIGH);
    delayMicroseconds(con9);

    //Condition 2
    digitalWrite(TTL1, LOW);
    delayMicroseconds(con9);
  }
}

As for why you are not getting 10kHz, there are seveal reasons. First, delayMicrosends() is not exact. Second, your loop() takes time to execute so there is always that lag present. The proper way to do this would be to program one of the Timers and then toggle your output pin. This may be more advanced that you want right now. As a better solution, look at the Blink Without Delay example in the IDE (File->examples->02.Digital->BlinkWithoutDelay) and it will show you how to at least get more accurate timing without using delays.

Here's an example of how Timer1 might be used to do what you want:

const byte pinTTL1 = 11;
const byte pinTrigger = A1;

byte
    inpLast;

void setup( void )
{    
    //analogReference( DEFAULT );
    pinMode( pinTTL1, OUTPUT );
    digitalWrite( pinTTL1, LOW );    
    pinMode( pinTrigger, INPUT_PULLUP );
    inpLast = ~digitalRead( pinTrigger );
    
    //TIMER1
    // fast PWM (WG mode 15)
    // prescaler = 1
    // 10kHz OC on pin 11
    TCCR1A = (1<<WGM11) | (1<<WGM10);   
    //set prescaler CS12..0: 001 (clkio/1)
    TCCR1B = (1<<WGM13) | (1<<WGM12) | (1<<CS10);        
    //set compare register
    OCR1A = 800;    //800 x 1/16MHz == 50uS toggle period (10kHz)
            
}//setup

void loop( void )
{
    byte
        inpNow;

    inpNow = digitalRead( pinTrigger );
    if( inpNow != inpLast )
    {
        inpLast = inpNow;
        if( inpNow == LOW )
            TCCR1A &= ~(1<<COM1A0);         //disconnect timer OC1A from pin (pin goes low)
        else
            TCCR1A |= (1<<COM1A0);          //connect timer OC1A to pin (10kHz)
            
    }//if
        
}//loop

Thank you all for the reply.

I was trying to learn what the code was doing, but I am stuck on understanding.
I read :Secrets of Arduino PWM

And how this code works?
I could not grasp functions of variables

I think OCR1 means that it is using Timer1 for A

Is there a way to adjust duty cycle in the code too?

Blackfin:
Here's an example of how Timer1 might be used to do what you want:

const byte pinTTL1 = 11;

const byte pinTrigger = A1;

byte
    inpLast;

void setup( void )
{   
    //analogReference( DEFAULT );
    pinMode( pinTTL1, OUTPUT );
    digitalWrite( pinTTL1, LOW );   
    pinMode( pinTrigger, INPUT_PULLUP );
    inpLast = ~digitalRead( pinTrigger );
   
    //TIMER1
    // fast PWM (WG mode 15)
    // prescaler = 1
    // 10kHz OC on pin 11
    TCCR1A = (1<<WGM11) | (1<<WGM10); 
    //set prescaler CS12..0: 001 (clkio/1)
    TCCR1B = (1<<WGM13) | (1<<WGM12) | (1<<CS10);       
    //set compare register
    OCR1A = 800;    //800 x 1/16MHz == 50uS toggle period (10kHz)
           
}//setup

void loop( void )
{
    byte
        inpNow;

inpNow = digitalRead( pinTrigger );
    if( inpNow != inpLast )
    {
        inpLast = inpNow;
        if( inpNow == LOW )
            TCCR1A &= ~(1<<COM1A0);        //disconnect timer OC1A from pin (pin goes low)
        else
            TCCR1A |= (1<<COM1A0);          //connect timer OC1A to pin (10kHz)
           
    }//if
       
}//loop

YMute:
Thank you all for the reply.

I was trying to learn what the code was doing, but I am stuck on understanding.
I read :http://www.righto.com/2009/07/secrets-of-arduino-pwm.html

May I ask what "WGM##" "OCR1A" "CA10" do?
And how this code works?

I think OCR1 means that it is using Timer1 for A

Is there a way to adjust duty cycle in the code too?

I would recommend downloading the datasheet (in this case, the 2560):

https://ww1.microchip.com/downloads/en/devicedoc/atmel-2549-8-bit-avr-microcontroller-atmega640-1280-1281-2560-2561_datasheet.pdf

and reading through section 17 "16-bit Timer/Counter (Timer/Counter 1, 3, 4, and 5)" (pg 133/435).

You can configure different "waveform generator modes" (WGM) to do different things, including PWM with adjustable duty cycle.

I have downloaded Time1 library for Arduino from "GitHub - PaulStoffregen/TimerOne: TimerOne Library with optimization and expanded hardware support" and installed to Arduino's library folder under 'documents' folder.

I was able to upload to Arduino Uno successfully, but once I power up the Arduino Uno and tried to run, it was not generating pulse on output #11.

Can I get help on why it would not work on Arduino Uno?

Also, regarding understanding of the code, am I understanding the code below correctly?

    TCCR1A = (1<<WGM11) | (1<<WGM10);   // To set both bit0 and bit1
    //set prescaler CS12..0: 001 (clkio/1)
    TCCR1B = (1<<WGM13) | (1<<WGM12) | (1<<CS10);       // To set both bit2 and bit3??  //  CS  -> prescaler setup (prescaler bits?)
    //set compare register
    OCR1A = 800;    //800 x 1/16MHz == 50uS toggle period (10kHz) | **Timer1  |  800 Counts

YMute:
I have downloaded Time1 library for Arduino from "GitHub - PaulStoffregen/TimerOne: TimerOne Library with optimization and expanded hardware support" and installed to Arduino's library folder under 'documents' folder.

I was able to upload to Arduino Uno successfully, but once I power up the Arduino Uno and tried to run, it was not generating pulse on output #11.

Can I get help on why it would not work on Arduino Uno?

Also, regarding understanding of the code, am I understanding the code below correctly?

    TCCR1A = (1<<WGM11) | (1<<WGM10);   // To set both bit0 and bit1

//set prescaler CS12..0: 001 (clkio/1)
    TCCR1B = (1<<WGM13) | (1<<WGM12) | (1<<CS10);      // To set both bit2 and bit3??  //  CS  -> prescaler setup (prescaler bits?)
    //set compare register
    OCR1A = 800;    //800 x 1/16MHz == 50uS toggle period (10kHz) | **Timer1  |  800 Counts

Without looking too deeply at your setup, you need to set the COMnA1 and COMnA0 bits in TCCR1A to have the OC affect the pin.

In your case:

TCCR1A = (1<<WGM11) | (1<<WGM10) | (1<<COM1A0);

I was able to upload to Arduino Uno successfully, but once I power up the Arduino Uno and tried to run, it was not generating pulse on output #11.

Please read the documentation for the TimerOne library.
The pwm output pins for Timer1 on the Uno are pins 9 and 10.