Need Help Making an IR Proximity Sensor with TSOP1738 and IR LED on Arduino Nano

Hi everyone,

I'm working on a project where I want to create an IR proximity sensor using the TSOP1738 IR receiver module and an IR LED connected to an Arduino Nano. I've successfully generated a 38 kHz PWM signal on pin 10 using Timer2 to drive the IR LED. However, I'm unsure how to implement the proximity detection function using the TSOP1738.

Tried to add burst function but that's disturbing my 38Khz Modulated IR LED Signal. what could be wrong ?

I need to enable output GPIO on proximity detection and vice versa.

Here's the code I'm currently using:

const byte LED = 10;  // Timer 1 "B" output: OC1B

// 16 MHz clock divided by 38 KHz frequency desired
const long timer1_OCR1A_Setting = 16000000L / 38000L;

// Define burst parameters
#define BURST_DURATION 50 // Duration of each burst in milliseconds
#define BURST_GAP 1000    // Gap between bursts in milliseconds

void generateIRBurst() {
  // Generate burst of 38 kHz signal
  digitalWrite(LED, HIGH); // Turn on IR LED
  delayMicroseconds(13);           // ON period for 38 kHz
  digitalWrite(LED, LOW);   // Turn off IR LED
  delayMicroseconds(13);           // OFF period for 38 kHz

  // Delay to control burst duration
  delay(BURST_DURATION);

  // Delay to control gap between bursts
  delay(BURST_GAP);
}

void setup() 
 {
  pinMode (LED, OUTPUT);

  // set up Timer 1 - gives us 38.005 KHz 
  // Fast PWM top at OCR1A
  TCCR1A = _BV (WGM10) | _BV (WGM11) | _BV (COM1B1); // fast PWM, clear OC1B on compare
  TCCR1B = _BV (WGM12) | _BV (WGM13) | _BV (CS10);   // fast PWM, no prescaler
  OCR1A =  timer1_OCR1A_Setting - 1;                 // zero relative  
  }  // end of setup

void loop()
  {
  
  OCR1B = (((512) * timer1_OCR1A_Setting) / 1024L) - 1;

   // Generate bursts of 38 kHz PWM signal
  generateIRBurst();
  
  }

I read this

The IR receiver module TSOP1738 is optimized for reception of a On-Off Key modulated IR signal with 38kHz carrier frequency. This does not mean the 38kHz can be continuously enabled, but only in bursts.

and I think your burst parameters are not sufficient to engage the sensor.

Try something like 1 or 2 milliseconds for both the on and off.

A typical IR code pulse is a 600 us burst.

As long as you have the COM1B1 bit set, the timer is controlling the output of D10, and nothing you do with digitalWrites will have any effect. So you set up D10's "normal" mode as OUTPUT, LOW, and just leave Timer1 running. Then when you want to switch D10 from normal mode (idle) to Timer1 mode (sending carrier), you set COM1B1. And to switch carrier output off, you clear COM1B1.

So On would be:

TCCR1A |= _BV(COM1B1);

And to turn it off:

TCCR1A &= ~_BV(COM1B1);

I'm assuming your LED drive circuit is set up so the LED turns ON when D10 is high.

So then the question is how long do you want the carrier to be on, and how often. The typical short burst for an IR remote output is around 600us, which is less than a milli() count. The TSOP should respond to that. So I would think you could turn it on for 500us and the receiver should respond. Or you could turn on carrier, delay(2), and confirm the TSOP is receiving carrier, then turn carrier off. And maybe you could do this once a second, which should be often enough to detect that nothing is in the way. Then you would be using very little power, with nothing heating up.

Could you post a drawing of how your LED drive circuit is hooked up, and your TSOP connections as well.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.