# 38KHz

Hi guys,
I need to make an IR sensor using TSOP 1738. So can I make the frequency of 38KHz using the PWM pins by tone() or by analog write?????????????????????

Is this possible.
If not can you suggest something????????

I dont know what this is

But it was me who posted this and it is shown as some bingobango guy.

in the upper right there is a search field and if you search for 38Khz you should find many postings as this question comes by every week or two.

Grumpy Mike gives some good code on this link :-

arduino.cc/forum/index.php/topic,131927.0.html

Also, there's a very thorough explanation of what's going on with iR sensors at the Adafruit learning centre

Geoff

This is what I use on an Arduino Uno or other atmega328p system to generate 38KHz or some other frequency. The output is on pin 9, and pin 10 is not available for PWM when using this code. Obviously, you can simplify the calculations at the start for a fixed frequency of 38KHz.

``````// Generate a square wave of a given frequency on the OCR1A pin

#define REQUIRED_FREQUENCY  (38000)
#define REQUIRED_DIVISOR ((F_CPU/REQUIRED_FREQUENCY)/2)

#if (REQUIRED_DIVISOR < 65536)
# define PRESCALER  (1)
# define PRESCALER_BITS  (1)
#elif (REQUIRED_DIVISOR < 8 * 65536)
# define PRESCALER  (8)
# define PRESCALER_BITS  (2)
#elif (REQUIRED_DIVISOR < 64 * 65536)
# define PRESCALER  (64)
# define PRESCALER_BITS  (3)
#elif (REQUIRED_DIVISOR < 256 * 65536)
# define PRESCALER  (256)
# define PRESCALER_BITS  (4)
#elif (REQUIRED_DIVISOR < 1024 * 65536)
# define PRESCALER  (1024)
# define PRESCALER_BITS  (5)
#else
#endif

# define TOP        (((REQUIRED_DIVISOR + (PRESCALER/2))/PRESCALER) - 1)

void setup()
{
pinMode(9, OUTPUT);
digitalWrite(9, LOW);
TCCR1A = 0;
TCCR1B = (1 << WGM12) | PRESCALER_BITS;    // turn on
TCCR1C = 0;
OCR1AH = (TOP >> 8);
OCR1AL = (TOP & 0xFF);
}

void on()
{
TCNT1H = 0;
TCNT1L = 0;
TCCR1A = (1 << COM1A0);
}

void off()
{
TCCR1A = 0;
}

void loop()
{
// Generate a burst 2ms long, then wait 10ms before generating the next one
on();
delay(2);
off();
delay(10);
}
``````