Creating a variable square wave (25-65kHz) on PWM pin

Hi there,

I am flashing code to an ATTINY84 and have stumbled upon a problem.

On the Arduino UNO, using the Tone() function was the easiest way to do this. But analyizing the output on the ATTINY84 PWM pin (pin 8), the scope doesnt read a square wave.

My next thought was to do create a HIGH / LOW loop on pin 8 utilizing the delayMicroseconds functions but the lowest it will go do with 1 microsecond between HIGH and LOW, the scope reads 5.55 kHZ.

This frequency needs to be 25-26kHz that changes with an AnalogRead function (that I have connected to another pin using a potentiometer as a voltage divider).

Any tips? Thanks! :slight_smile:

create a HIGH / LOW loop on pin 8 utilizing the delayMicroseconds functions

Did you use digitalWrite()? That's pretty slow for what you need, use PORT registers commands (adapt to attiny PB)