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.