Hi guys its my first approach with arduino and i want make some leds flash some at 5khz and others on 7.5 khz. just wanted to know if it is possible to do with arduino. I know i can make it with two 555 timers but i want to use arduino board because on the same board i will implement other things. For the leds i was thinking if i can connect two different pins on two transistors to open and close like a switch on the two frequencies???
It would be better to learn about the atmega timmers and set then up to do that. ![]()
yes you can but why?
For a 50% duty cycle you want an event every 66.6us for the 7.5kHz and 100us for the 5kHz LED.
If you are prepared to tolerate slight error in the frequencies note that setting timer1 or timer2 to /1 clock prescale and enabling timer interrupt will give an interrupt every 31.875us, which if you use every other to toggle one pin and every third interrupt to toggle another pin will give 7.843kHz and 5.229kHz
All this requires some low level coding as the timers are set to /64 prescale to support PWM. Changing the prescaling will affect the PWM outputs of course.
Using delayMicroseconds() is much simpler to code but prevents using the Arduino for other tasks...
you can probably use the frequency function
Do you mean setPwmFrequency() - didn't know that existed till I searched for it... Hides some of the messy details nicely - but for interrupt handling I think you still need to know which timer corresponds to which PWM pins (it differs on the Mega)
You could also try the tone() function. It'll generate a square-wave output on whatever pin you want.
e.g.
tone(4, 5000);
delay(1000);
tone(4, 7500);
Not sure if it will suit what you need though. The accuracy with the 8 bit timer might be pretty rough for 7500 Hz.
tone(4, 5000) produces a 5000 Hz square wave.
tone(4, 7500) produces a 7518 Hz square wave.
If you use the Tone library, you can get more accurate values:
tone1.play(5000) produces a 5000 Hz square wave.
tone1.play(7500) produces a 7505 Hz square wave.
(you'll have to create two Tone objects, and use the second one, as it will use the 16 bit timer)
These are the best frequencies you'll get - based on the 16 MHz MCU clock.
b