My application requires a frequency sweep in a coil and measuring the resultant resonant frequency from the coil after it has settled.

I can do the second part i.e. wait and count the rising edges of say 100 samples to measure the resonant frequency but am lost when it comes to the sweep generation part.

I am looking to generate frequency sweep using AVR microcontrollers. Frequency range (selectable): starting frequency min = 500 Hz to end frequency max = 5500 Hz. Duration will be anywhere between 125-500ms (selectable).

Searching for frequency sweeps on the internet I got some ideas for a linear sweep. Basically I pick a starting frequency and step frequency size and keep generating the next frequency by adding the start frequency + step frequency and so on until I reach my end frequency. Is this the correct approach?

I am confused about generating log frequency sweep. I've got these parameters: start frequency, end frequency, sweep duration (this determines different combinations of increments (step size) and the number of half cycles for a 50% duty cycle square wave). I am only looking to generate square waves for simplicity, they don't need to be sine waves.

Basically: how do I implement frequency sweeps using AVR microcontrollers (more specifically logarithmic sweeps)?

Any ideas/pseudo code for square wave pulse sweep please?

  • \$\begingroup\$ This is kinda too broad. First of all you didn't specified which AVR part you are targeting. There a lot of different AVRs, including those new megaAVR 0-series, tinyAVR 1-series which has some advanced peripherals for waveform generation. Even the 'classic' parts differs on which features they offer. Next your question would be on how to generate 50% duty square wave within the specified freq. range on that part. Next you will need an algo to calculate a step size which I guess might be more suitable for Stackoverflow. \$\endgroup\$
    – NStorm
    Nov 15, 2021 at 14:24
  • \$\begingroup\$ Log sweeps have constant logarithmic increase in linear time, instead of linear increase. Can you determine what a logarithmic increase means in linear space? \$\endgroup\$
    – Justme
    Nov 15, 2021 at 16:34
  • \$\begingroup\$ I am using atmega328p with 16MHz clock to generate pwm linear sweep. I am stuck with questions/starting point on generating log sweep. I guess i am looking for finding the freq components for log sweep, then i'll just modify my sketch to generate these frequency pulse thus creating log sweep. is this correct approach? For linear sweep I am doing something like this: pastebin.com/embed_js/K7dQYk4N \$\endgroup\$
    – eaterbugs
    Nov 15, 2021 at 20:42

2 Answers 2


Linear sweeps have constant linear increase in linear time. Log sweeps have constant linear ratio in linear time.

If you now add something to frequency increment, multiply.

For example, if you were making musical tones with 12 steps per octave, you would multiply or divide by 1.059463094 to move up or down betwen notes. Ratio of 1.01 would be about 70 steps per octave.

  • \$\begingroup\$ or coversely multiply the period by some easy fraction like 255/256 at regular intervals, as it's the peroid that is the input to the timer/counter. \$\endgroup\$
    – Jasen
    Nov 15, 2021 at 22:03
  • \$\begingroup\$ i think i understand somewhat. So next question is how do i determine the multiplier(k) so that the end freq is generated at the end of the desired interval. From what I see, if the multiplier is too big the end freq is reached a lot quicker as compared to a smaller multiplier. \$\endgroup\$
    – eaterbugs
    Nov 16, 2021 at 0:27

log sweep - fixed duration per octave

as the slope of the log function is 1/x to get a log sweep just increase the frequency by a fixed amount at each cycle, as the frequency goes up the steps become more frequent.

you'll probably want to find a way to do that without doing long division after each cycle.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.