How Can I trigger IR transmitter with PIR motion sensor?

Hi all,
This is my first post here!
I'm using IR transmitter and Receiver in my Arduino project. To trigger IR sensors I'm using PIR sensor, if motion is detected IR sensors will be activated. With PIR , IR receivers works perfect, but IR transmitter doesn't work, it doesn't respond. Below there is my code to trigger IR transmitter when motion is detected. What am I doing wrong?
Thanks in advance!

#include <IRremote.h>
#define PIN_IR 3
int pirPin = 9;  // pin for PIR output
int pirStat = 0;  // PIR status
IRsend irsend;

void setup()
{
  pinMode(pirPin, INPUT);
  pinMode(PIN_IR, OUTPUT);
  irsend.enableIROut(37.9);
  irsend.mark(0);
  Serial.begin(9600);
}
void loop() {
  pirStat = digitalRead(pirPin);
  if (pirStat == HIGH) {
    digitalWrite(PIN_IR, HIGH);
    Serial.println("Motion detected");
    }
   else {
    Serial.println("Are you there?");
    digitalWrite(PIN_IR, LOW);
    }

}

Your description is a bit confusing but it appears that you have a problem between the IR transmitter and the IR receiver. The PIR is not the issue. How were you able to come to this conclusion >> IR receivers works perfect ?

If the IR library is managing the pin, I don’t believe that you can do this:

digitalWrite(PIN_IR, HIGH) ;

What code are you attempting to send to the IR receiver?

Sorry for poor description.
I'm making a people counter device, using one IR transmitter (Tx) and two IR receiver (Rx). It works as Whenever someone passes between Tx and Rx the sensors are blocked so it counts.
I'm using below code for Tx and it works fine. But Tx emits infrared rays all the time which I don't want because it runs on battery. So I want to use PIR sensor to trigger Tx. If motion detected Tx goes ON otherwise OFF

#include <IRremote.h>
#define PIN_IR 3
IRsend irsend;
void setup()
{
  //Serial.begin(9600);
  irsend.enableIROut(37.9);
  irsend.mark(0);
}
void loop() {
}

OK.
So you are using a PIR sensor to detect people.
If your detector detects someone's presence, it transmits an IR signal. That IR signal is received by two IR receivers.
Your only problem is that the IR transmitter is consuming too much current.

The IR signal you are sending doesn't look very complex. Are you simply intending to send a short burst of a 38kHz carrier signal to the two receivers ? Can you supply also the code of the IR receivers (if these are Arduino based) ?

Thanks a lot for your comments and willingness to help!

"Are you simply intending to send a short burst of a 38kHz carrier signal to the two receivers ?"
Yes, that's what I'm doing.

Can you supply also the code of the IR receivers (if these are Arduino based) ?
Sure, it is Arduino based. But, IR transmitter and receivers are on separate Arduino boards.
My code may look very complicated, but it works very well!

int led_Out = 5; // OUT indicate light
int led_In = 4;  // IN indicate light
int IR1 = 6;     // IR sensor 1
int IR2 = 7;     // IR sensor 2
int pirPin = 9;  // pin for PIR output
int irPin = 8;    // IR receivers

int sensor1 = HIGH;
int sensor2 = HIGH;
int IN = 0;
int pirStat = 0;  // PIR status
int t;
int dt = 0; //delay time
int wt = 3000; //wait time
int waited_time = 0; // time_threashold to break while_loop
int IRs_StartedTime;    // time when IRs are activated
int IRs_RunDuration = 0;  // IRs active duration


void setup() {
  pinMode(pirPin, INPUT);
  pinMode(IR1, INPUT);
  pinMode(IR1, INPUT);
  pinMode(irPin, OUTPUT);
  pinMode(led_Out, OUTPUT);
  pinMode(led_In, OUTPUT);
  Serial.begin(9600);
}

// HIGH means blocked, LOW means open
void loop() {
  pirStat = digitalRead(pirPin);    //
  if (pirStat == HIGH) {            // check PIR, if motion detected
    digitalWrite(irPin, HIGH);      // turn IRs(IR1 and IR2) ON
    IRs_StartedTime = millis();
    while (IRs_RunDuration < 10000 ) {                         //The IRs sensor should run for 10 seconds, once turned ON
      IRs_RunDuration = millis() - IRs_StartedTime;
      sensor1 = digitalRead(IR1);
      sensor2 = digitalRead(IR2);
//          Serial.println(sensor1);
//          Serial.println(sensor2);
      //    if (sensor1 == HIGH) {
      //      Serial.println("sensor1");
      //      }
      //
      //     if (sensor2 == HIGH) {
      //      Serial.println("sensor2");
      //      }

      if (sensor1 == HIGH && sensor2 == LOW) { // (1)
        //    Serial.println("Im in if");
        t = millis();
        delay(dt);
        sensor2 = digitalRead(IR2);
        while (sensor2 == LOW || sensor1 == LOW) {  //Wait until sensor2 becomes HIGH
          sensor2 = digitalRead(IR2);
          sensor1 = digitalRead(IR1);
          waited_time = millis() - t;
          if (waited_time > wt) {
            break;
          }
        }
        if (sensor1 == HIGH && sensor2 == HIGH) { // (2)
          t = millis();
          delay(dt);
          sensor1 = digitalRead(IR1);
          while (sensor1 == HIGH || sensor2 == LOW) {  //Wait until sensor1 becomes LOW
            sensor1 = digitalRead(IR1);
            sensor2 = digitalRead(IR2);
            waited_time = millis() - t;
            if (waited_time > wt) {
              break;
            }
          }
          if (sensor1 == LOW && sensor2 == HIGH) { // (3)
            delay(dt);
            t = millis();
            sensor2 = digitalRead(IR2);
            while (sensor1 == HIGH || sensor2 == HIGH) {  //Wait until sensor2 becomes LOW
              sensor1 = digitalRead(IR1);
              sensor2 = digitalRead(IR2);
              waited_time = millis() - t;
              if (waited_time > wt) {
                break;
              }
            }
            if (sensor1 == LOW && sensor2 == LOW) { // 4
              Serial.println("IN:");
              Serial.println(++IN);
              digitalWrite(led_In, HIGH);
              delay(100);
              digitalWrite(led_In, LOW);
            }
          }
        }
      }


      else if (sensor1 == LOW && sensor2 == HIGH) { // (1)
        //      Serial.println("Im in else if");
        t = millis();
        delay(dt);
        sensor1 = digitalRead(IR1);
        while (sensor1 == LOW || sensor2 == LOW) {  //Wait until sensor1 becomes HIGH
          sensor1 = digitalRead(IR1);
          sensor2 = digitalRead(IR2);
          waited_time = millis() - t;
          if (waited_time > wt) {
            break;
          }
        }
        if (sensor1 == HIGH && sensor2 == HIGH) { // (2)
          t = millis();
          delay(dt);
          sensor2 = digitalRead(IR2);
          while (sensor1 == LOW || sensor2 == HIGH) {  //Wait until sensor2 becomes LOW
            sensor1 = digitalRead(IR1);
            sensor2 = digitalRead(IR2);
            waited_time = millis() - t;
            waited_time = millis() - t;
            if (waited_time > wt) {
              break;
            }
          }
          if (sensor1 == HIGH && sensor2 == LOW) { // (3)
            t = millis();
            delay(dt);
            sensor1 = digitalRead(IR1);
            while (sensor1 == HIGH || sensor2 == HIGH) {  //Wait until sensor1 becomes LOW
              sensor1 = digitalRead(IR1);
              sensor2 = digitalRead(IR2);
              waited_time = millis() - t;
              if (waited_time > wt) {
                break;
              }
            }
            if (sensor1 == LOW && sensor2 == LOW) { // 4
              Serial.println("OUT:");
              Serial.println(--IN);
              digitalWrite(led_Out, HIGH);
              delay(100);
              digitalWrite(led_Out, LOW);
            }
          }
        }
      }
      //  delay(100);
    }
//    digitalWrite(irPin, LOW);  // turn IRs OFF
    IRs_RunDuration = 0;
  }
  else {
    digitalWrite(irPin, LOW);  // turn IRs OFF
  }
}

Ok. I guess you are using IR receivers like say TSOP4838 or something similar.

The IR library is very heavy weight for what you want to do. I’d simply use the tone() function to send a short 38kHz burst to the IR led.

tone(pin, frequency, duration) ;

See tone() - Arduino Reference

digitalWrite(PIN_IR, LOW); did the trick and now it's working fine.
The whole code is below if someone needed in the future

#include <IRremote.h>

#define PIN_IR 3
IRsend irsend;

int pirPin = 9;  // pin for PIR output
int pirStat = 0;  // PIR status
int IRs_RunDuration = 0; // IRs active duration
int IRs_StartedTime;    // time when IRs are activated

void setup()
{
  pinMode(pirPin, INPUT);
  irsend.enableIROut(37.9);
  Serial.begin(9600);
}


void loop() {
  pirStat = digitalRead(pirPin);
  if (pirStat == HIGH) {
    Serial.println("Motion detected");
    IRs_StartedTime = millis();
    while (IRs_RunDuration < 15000 ) {
      irsend.mark(0);
      IRs_RunDuration = millis() - IRs_StartedTime;
    }
    IRs_RunDuration = 0;
  }
  else {
    digitalWrite(PIN_IR, LOW);
    Serial.println("Are you there?");
  }
}

But, this tone(pin, frequency, duration) ; sounds great. I'm gonna try it right away.
Thanks for your comments again!

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