Today's problem for us : generate a clock of 5 kHz with an Arduino Nano.
We thought about using the library Timer to use the function : int oscillate(int pin, long interval, int valeurDeDepart, int nombreDeFois).
The problem is that in order to generate a 5kHz signal we need it to switch every 100 us and the function take as argument an int os ms ...
Does anyone have an idea of how we could do it ?
The ultimate goal is to make a sweep frequency from 5kHz to 50kHz so if by any chance someone know something about that with an Arduino that's nice
You can use the Simple timer output to generate the desired frequency.
const byte LED = 3;Â // Timer 2 "B" output: OC2B
const long frequency = 50000L;Â // Hz
void setup()
{
 pinMode (LED, OUTPUT);
 TCCR2A = bit (WGM20) | bit (WGM21) | bit (COM2B1); // fast PWM, clear OC2B on compare
 TCCR2B = bit (WGM22) | bit (CS21);    // fast PWM, prescaler of 8
 OCR2A = ((F_CPU / 8) / frequency) - 1;  // zero relativeÂ
 OCR2B = ((OCR2A + 1) / 2) - 1;      // 50% duty cycle
 } // end of setup
void loop()
 {
 // do other stuff here
 }
// With a 16 MHz clock, this can generate frequencies in the range 62.5 kHz to 4 MHz (8 MHz ought to work but has artifacts on the measured output).
unsigned long currentMicros;
unsigned long previousMicros;
unsigned long elapsedMicros;
unsigned long halfPeriod =100UL; // period = 200uS = 5KHz
void setup(){
pinMode (D2, OUTPUT);
}
void loop(){
while (1){ // gets rid of loop "jitter" (easy to see with a 'scope)
currentMicros = micros(); // capture the current "time"
elapsedMicros = currentMicros - previousMicros; // how long's it been since last toggle?
if (elapsedMicros >= halfPeriod){
PIND = PIND | 0b00000100; // toggle D2 by writing 1 to its input register
previousMicros = previousMicros + halfPeriod; // set up next toggle time
   }
 }
}
68tjs:
You can read the datasheet and use Timer in CTC mode.
You have only to choose good pre-scaler value and configure 3 registers.
Datasheet is most powerfull than Wiring/arduino libraries.
The "Tone" function provides high level access to the Timer in CTC mode. I'm not sure if it reproduces 5 kHz exactly from a 16 MHz clock which would depend upon the timer pre-scale value, so you may have to go down a coding level anyway.