Need Help Receiving IR Pulses

Hi, I've set up a breadboard with an IR emitter and receiver, and I'm trying to emit and receive certain pulse durations. The emission works fine, but I'm unable to receive more than illogical values from trying to read pulseIn durations. Can anyone see anything wrong in the code / how can I fix this?
Clarification: I'm using an external remote to test the receiver, not the LED

My Circuit ( the power source is the PC it's plugged into):

My Code:

// Variables
int irReceiver = 7;
int irEmitter = 2;
int irValue = 0;
String stringTrigger = "send";
double duration_ms;
 
int pulseWidth = 20;
 
// Functions
int sendIRFunction(int x) {
  Serial.println((String)"Sending "+ x + "ms");
  int repeat = 1000/x;
  while (repeat > 0) {
    digitalWrite(irEmitter, HIGH);
    delay(x);
    digitalWrite(irEmitter, LOW);
    repeat -= 1;
    Serial.println(repeat);
  }
}
 
// Basic setup
void setup() {
  pinMode(irReceiver, INPUT);
  pinMode(irEmitter, OUTPUT);
  Serial.begin(9600);
}
 
// Waits for "send" input before calling send command
void loop() {
  while (Serial.available() > 0) {
    if (Serial.readString() == stringTrigger) {
      sendIRFunction(pulseWidth);
    }
  }
  readIR();
}
 
// Loop to constantly print received signals
void readIR() {
  unsigned long duration = pulseIn(irReceiver, LOW, 110000);
  duration_ms = duration/1000;
  if (duration_ms > 0) {
    Serial.println((String)"Receiving " + duration_ms);  
  }
//  delay(100);
}

Best read the posting requirements here at the head of this forum.

Think you will do better if you copy your code from your iDE using the code brackets and pasting here.
A circuit diagram would help as well as details on any parts used and include details of power supply.

Thanks for the feedback, I'll update the post

Hi @fossilized4444 ,

yeah, transmitting light, then trying to receive it..
gonna need a faster board, faster than speed of light..
maybe a dual core running concurrent threads..
sorry.. ~q

Thanks for the response,
I've been testing with a handheld remote, and the receiver seems to read nothing still. The only times I do get some values are extremely random, and I can't connect it to anything logical

You have a problem with the processor, it cannot do two things concurrently. It can transmit or receive but not both at the same time. You make this more problematic by using delay();

that should do something..
is it one of the generic ones..
like this one here..
~q

Ok so I can just get around this using 2 boards right?

Yeah it is

That should make easy work of it for you. You can use the most inexpensive Arduino available to you as most of them will work.

Reassess your project.

  • Is it necessary to simultaneously send and receive?
  • If you use two processors, what needs to be communicated between them, and how will you do that?
1 Like

@fossilized4444
I think you have the signal and ground wires on the receiver backwards.

This is what the data sheet says

shouldn't there be a delay after setting the pin LOW?

Should there? What would it help with? Receiving is the only thing I'm having issues with right now

the emitter may be Off for such a short period of time that it's difficult to detect/measure the width of the pulse when it is On

Hopefully you are using devices that both use a 38kHz off-on IR signal. So your ON signal needs to be at least three of the IR signal off-on rate in order to actually modulate the 38kHz signal for your receiver to sense. You figure the delay needed.

1 Like

I'm not measuring from the emitter, I'm measuring from an external remote

I am using an external remote to test the receiver

Having this -

image

inside your loop is probably playing havoc with the timing.