Generate Frequency using simple HIGH-Delay-LOW-Delay-Repeat


i trying generate frequency resolution of 0.1hz/steps 0 1khz.

the code below:

code: [select]
void setup() {
  // put setup code here, run once:
pinmode(13,output);
}

void loop() {
  // put main code here, run repeatedly:
digitalwrite(13,high);
delay(5000);
digitalwrite(13,low);
delay(5000);
}


by delaying 5 seconds after triggering high , low , shown in oscilloscope frequency

0.1hz (100mhz)  delay = 5000 after high , low;

0.2hz (200mhz)  delay = 2500 after high , low;

up until

0.9hz (901mhz)  delay = 555 after high , low;

when tried generate 10.1 hz, 1/10.1hz , divide 2 have delay @ high , delay @ low, 49 ms after high , 49ms after low. result 10.16 hz fine.

but when tried generate frequency of 49.3hz , have changed delay delaymicroseconds , here's new code:

code: [select]
void setup() {
  // put setup code here, run once:
pinmode(13,output);
}

void loop() {
  // put main code here, run repeatedly:
digitalwrite(13,high);
delaymicroseconds(10142);
digitalwrite(13,low);
delaymicroseconds(10142);
}


the frequency read oscilloscope : 49.01 hz

and if try frequency of 587.3 hz, delay 851 microseconds.

code: [select]
void setup() {
  // put setup code here, run once:
pinmode(13,output);
}

void loop() {
  // put main code here, run repeatedly:
digitalwrite(13,high);
delaymicroseconds(851);
digitalwrite(13,low);
delaymicroseconds(851);
}


the frequency read oscilloscope : 582.1 hz bit diverted required value of 587.3 hz.

lets say, if tries frequency of 800.1 hz delay 624 ms. code is:

code: [select]
void setup() {
  // put setup code here, run once:
pinmode(13,output);
}

void loop() {
  // put main code here, run repeatedly:
digitalwrite(13,high);
delaymicroseconds(625);
digitalwrite(13,low);
delaymicroseconds(625);
}


the frequency read oscilloscope : 793.7 hz . want 800.1 hz.

is there other method generate frequency of 0.1hz of resolution better accuracy?

your calculations seem ignoring fact digitalwrite()s , return top of loop take time. if put 2 x 500 microsecond delays inside loop other commands total loop time more 1000 microseconds.

if calculate difference between expected , measured in microseconds/cycle instead of in frequency might spot constant offset can apply better accuracy.

steve


Arduino Forum > Using Arduino > Programming Questions > Generate Frequency using simple HIGH-Delay-LOW-Delay-Repeat


arduino

Comments

Popular posts from this blog

Flip address is out of range arduino uno r3

Arduino Uno not uploading

Indesign and MathType fonts