I am trying to have two atmega328p chips communicate to each other via IR. I used to do this with my picaxe chips some time ago but i noticed that with arduino things look a little more complex.
Got two "problems":
The frequency. I need to get my IR light modulated at 38KHz but i've read in the reference that with analog.write command the frequency is fixed and it's much lower. Is there a way i can do that with the arduino or do i have to buy an external chip?
Serial communcation. I've attached a led (the IR transmission LED) to the TX pin and i noticed it lights up when i write serial.begin in void setup(). I'd like to keep the LED off when not in use.
Is there a way i can avoid having the led on at all times? I want my project to send a signal when the user presses a button. Should i use the serial.begin ONLY when the user presses that button (instead of putting it in the setup void function) and have it disabled with serial.end when the button is released? I just hope the serial.begin command doesn't take too much time to set itself because i'd like the signal to be sent exactly when the user presses the button.
38kHz is a period of a shade over 26us.
As long as you don't do it for too long and don't want to do anything else at the same time, then some "delayMicroseconds" between you "digitalWrite"s will sort you out.
You'll have to experiment to get the delay just right.
actually i do want to do something else at the same time. To transmit data i also need to send the serial data to the LED while i also modulate it with the PWM.
With all modulation, the information you modulate has to be at a much lower frequency than the carrier or modulation frequency.
The simplest way to modulate an IR LED is to change the PWM frequency to 38KHz and write a 50% value to it.
However, to modulate PWM on this you need to work out what frequency of PWM you want. Most modulated IR receivers will only operate a a certain baud rate so you need to look this up and see if it is compatible with what you want. You can then bit bang the PWM by writing alternately 0% and 50% to the modulation either from a timing loop or from an interrupt service routine.
Edit.
If you use a 555 to provide the modulation you still might have to modify the basic PWM frequency, I believe it defaults to 400Hz and that might be too fast for your receiver.
it gives you the command pwm(pin, duty, period) so i guess it would work great for me. Just one thing: if i set the duty to 50% the period is 26us right (as Groove said)?
If you change TCCR0B, it affects millis() and delay(). They will count time faster or slower than normal if you change the TCCR0B settings. Below is the adjustment factor to maintain consistent behavior of these functions:
so i guess this means that if i use the previous library, which uses timer 1, i shouldn't have problems with it.
Just one thing: if i set the duty to 50% the period is 26us right (as Groove said)?
The period is independent of the duty cycle. So any percent will give you the same period.
so i guess this means that if i use the previous library, which uses timer 1, i shouldn't have problems with it.
I don't know as I haven't used that library, it depends on how they implement it. Simply changing the timer prescaler will give you problems but if it is done in another way it could not.
I would check that they still give the right delay after you change it.