Because OCR0A is small it counts up to 4, giving a frequency of 4 MHz (one quarter of the system clock). However the duty cycle can only be 0, 1 or 2, so basically you can have a 33% or 66% duty cycle.
The higher you make OCR0A the lower the frequency but the higher resolution of PWM duty cycle.
Thanks. Looks like the default digitalWrite() has too much overhead. I just tried measuring the uno with the OP code and I topped at 116kHz. The nano that I measured before topped at 70kHz. But seeing there are many different timers I guess it depends on which pin too?
For the sample code you supplied using the TimersHelpers.h library, I downloaded it and tried the sample code. There was an error on debug saying TImer0 is not defined?
If I want to create 50% duty cycle signals at the highest frequency possible (essentially just a normal periodic square wave). Can that be done up to 4MHz (you mentioned 4MHz can only choose between 33% or 66% duty)?
Unzip that file, copy the folder TimerHelpers into the libraries folder (in your Arduino sketches folder) then restart the IDE. Copy the whole folder, not just the contents.
Thanks. I got it to work. The restart seemed to do it.
I also measure 4MHz square wave now too! Cheers!A few noob question:
Does it still source the maximum 50mA current?
Does this code also work for the nano?
Does the code work for all digital pins?
I just tried to move part of the code over to loop and the signal is no longer steady and 50% dutycycle i guess because there is some time lag with the restarting the loop? So this method only works under void setup()?
The timers have fixed output pins. Each timer has two pins dedicated to it which is why you have 6 PWM outputs on the Uno (3 timers x 2 pins each).
I just tried to move part of the code over to loop and the signal is no longer steady and 50% duty cycle i guess because there is some time lag with the restarting the loop? So this method only works under void setup()?
That doesn't make sense. Show your amended code please. Once you have the timer set up it doesn't matter what the code is doing. However if you put that code into loop it might be restarting the timer very rapidly. You don't want that.
The timer (that I showed) runs independently, so that is why I had it in setup. You don't have to keep calling the code. It's all done in hardware.
Also I tried it with the nano recently. I noticed that the uno produces better square waves than the nano. Are the pulses generated by the microcontroller (which are the same for the uno and nano)? Or the crystal?
I was wondering if there is any documentation on how to use the TimeHelper header file?
Sorry didnt mean to ignore the advice. I just tried it for the sake of it. Actually the reason is I intend to be able to modulate the frequency so maybe Id want it in the loop instead.
If you were looking for frequency modulated RF @ 4 Mhz you might well consider an Analog Devices AD9851 DDS solution (Faster X2 to 60 MHz) or the readily available from Ebay AD9850 assemblies. they require a 40 bit serial load SPI I think and can operate from nearly DC to 30 MHZ, both sine and square wave outputs too. While NCO's aren't new this one is interesting and at about 7 to 9 dollars an inexpensive way to go. There are a couple of sketches I've found for mine but not yet the time to test them... Nor a good frequency counter either... Yet.
Thanks for the great sketch and information. I am having trouble understanding your sketch, maybe because I am fairly new to Arduino/C++. What I am trying to do is utilize the output from your sketch as a loop antenna that I can then detect by a secondary coil or loop. No need for accuracy, just detection of proximity. Can you advise where/how I get your output? Thanks very much.