Let's Make Robots!

PWM at 38KHZ with Arduino

Hi guys.

I'm trying to modulate my IR led at 38KHz so that the receiver will pick up the signal. I've googled around and all i've seen is some people modding the wiring.c file, but this way the only get a limited number of possible frequencies. Some other guys say that you have to use a 555 cause you can't do that with arduino. Then i've found this: http://www.arduino.cc/playground/Code/Timer1 and i thought it could work, since it gives you the pwm command which also lets you specify the period....but it doesn't work, my IR receiver doesn't pick it. Maybe i just specified a wrong period? (i wrote 26, which is 26 micro seconds)

 Did i get something wrong? Do you know another method to achieve custom frequencies on PWM?

this is the circuit (not really needed though) 

thank you! Immagine_0.png

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Most newer multimeters have a way to measure frequency which could be used for verifying the frequency that you are outputing once you get that set up. I've successfully done this when testing my setup. as far as coding it, oddbot has a good advice.

Another way to see if a sig is going out would be the usual digital camera or phone camera to see if the emmiter is on. I would recommend using a transistor(pnp or npn depending on if it's active hi or low)and tie that to an led. This could help with debugging and seeing if you are getting a signal or not.

I don't have any :( . Some time ago i wanted to buy one of these but i saw that most medium-cost ones only measure up to 20KHz or so, whereas the only frequency my project uses is 38KHz. I already use the LED and the camera trick, quite handy i have to say. I've also seen some kind of plastic card which illuminates (as if IR light was visible) if you point IR light at it, but i don't remember where i saw it. That would be even more useful (more than the camera i mean...).
I've never heard of the card, thats interesting, I'll have to see if I can find anything on it.
Here is one: http://www.newport.com/Infrared-and-Ultraviolet-Sensor-Cards/139709/1033/catalog.aspx . It would be cool if some major electronics online store sold it.

I use an osram SFH5110 and the signal must look like this for it to work

600 |600|                         usecs

____|''''''''|_____|"""""|_____

 duty cycle 50% of 38khz carrier

On receipt of this signal the normally high output goes low. I'm pretty sure that most Ir receivers of the 3 terminal type work this way. Just sending a constant 38khz carrier won't work. You have to turn it on and off at a 600usec interval. 

No i'm not only sending the 38KHz carrier. I'm ANDing it with the serial output. The arduino's serial is normally high when not in use, as someone told me in another post, so it might work without any modification.

Here is a code sample for the IR communication between an Arduino an a PC (taken from the Asurino Lib). It uses Pin 11 and Timer 2 for the carrier frequency. The Hardware interface is the same I mentioned in another post.

Maybe its better to strart with a simpler circuit. I am not an expert for FETs but I think you should always use a resistor beetween gate and microcontroller (1kOhm). Also I would mention a resistor for current limit in series to the IR LED(10..100Ohm).

ir_interface.jpg

This is the sketch. You just need to adjust the IR_CLOCK_RATE to your needs:

#define IR_CLOCK_RATE    36000L

#define pwmPin 11   // IR Carrier


void setup()  {
  // set the data rate for the Serial port
  Serial.begin(2400);
 
  // toggle on compare, clk/1
  TCCR2A = _BV(WGM21) | _BV(COM2A0);
  TCCR2B = _BV(CS20);
  // 36kHz carrier/timer
  OCR2A = (F_CPU/(IR_CLOCK_RATE*2L)-1);
  pinMode(pwmPin, OUTPUT);
 
}

void loop()
{
  char i;
  for(i='A'; i<='Z'; i++)
  {
    Serial.print(i);
  }
  delay(40);
}
 

Cool! So all those TCCR2A and stuff actually go into the main program and not in the core files! Thanks for the code RbotoFreak!
I would never change the Arduino core files. It is alwasy better to overwrite the hardware registers in your own setup function.
I agree. Just didn't know you could do that in the IDE.