Reading from two IR sensors on same Arduino Uno

I’m having some trouble reading output of both IR sensors on my Uno. It works perfectly when I try to read just the one sensor. But when I replicate the code to read two, both sensors report same output WHEN THEY ARE NOT SUPPOSED TO DO THAT. Here’s my code… wondering if someone can look at it… Thanks. (One sensor attached at pin 10 and other at 11.

#include <IRremote.h>

int RECV_PIN = 10;
int RECV_PIN1 = 11;

IRrecv irrecv(RECV_PIN);
IRrecv irrecv1(RECV_PIN1);

int counter_clock = 0;
int counter_clock1 = 0;

decode_results results;
decode_results results1;

void setup(){
  irrecv.enableIRIn(); // Start the receiver
  irrecv1.enableIRIn(); // Start the receiver

void loop() {
  if (irrecv.decode(&results)) {
   counter_clock = 0;
   Serial.print("At sensor <10>: ");
   irrecv.resume(); // Receive the next value

   if (irrecv1.decode(&results1)) {
      counter_clock1 = 0;
      Serial.print("At sensor <11>: ");
      irrecv1.resume(); // Receive the next value

Are you saying that you tried a sketch with a sensor in pin 10 and then another sketch with a sensor in pin 11?

EDIT: This library looks to use TIMER2. I am not sure that two sensors can share the same resource.

Its the same sketch... trying to read from both IR sensors.. one attached to pin10 and other to pin 11.

Its the same sketch... trying to read from both IR sensors.. one attached to pin10 and other to pin 11.

Yes, and did you read what I wrote?
How do you know that each sensor by itself works?

How do you know that each sensor by itself works?

I have tried both of them individually and they are both working fine. However, as soon as I use the sketch mentioned above, the outputs get messed up and both sensors show same output (the output of the sensor towards the end of the loop). There's also an LED on the sensors which tells me when the sensor should be showing an output and when not.

Yes, and did you read what I wrote?

I did read it. You said you 'aren't sure' so I don't know what to make of that, sorry.

I am betting that this code in the library


	// Read if IR Receiver -> SPACE [xmt LED off] or a MARK [xmt LED on]
	// digitalRead() is very slow. Optimisation is possible, but makes the code unportable
	uint8_t  irdata = (uint8_t)digitalRead(irparams.recvpin);

means that you canot use 2 receivers, as it is an interrupt routine on the timer.

By the way, it is bad form to start the same thread twice.

@marco_c: thanks for your response. that's helpful!

You mentioned you use another library 'IRLib' for IR projects. Do you think that'll let me use two IR receivers?

Probably not as it uses interrupts as well. Why do you think you need 2 receivers?

I am trying to build an IR room occupancy system using IR 'beam' at the doorway. So I need two receivers so see which IR 'beam' is broken first... Do you have any suggestions what else I can do.. (other than perhaps using two controllers)? Thanks.

IR beams are different from IR remote controls.

Remote controls use a modulated IR beam to encode a signal that tells the receiver what button is pressed. This is complicated as the receiving end needs to be able to decode the signal depending on what transmission standard (protocol and modulation frequency) was used to encode the signal. The receiver package usually has signal demodulating built in so that a clean 0/1 is output to the microcontroller. Hence the IRremote and IRLib libraries to try and simplify this process.

An IR beam is just on all the time (ie, once connected to a power source it is just on) and a receiver will detect when that beam is broken. It is just a digital on and off, like a switch, except that it is triggered by an IR beam and is normally on.

If you are looking for functionality like the second case, then you simply need an IR receiver without all the demodulation decoding. This is a very simple thing to do and you can connect as many as you like.

Make sure that the sending diode has a narrow beam focus or the two devices may interfere.

Edit: Sorry, meant to be decoding not demodulating, otherwise you will get ambient IR triggering your receiver.

One of the "unwritten" rules you have broken here is that you have failed to cite what modules you are using and I am not spending any more time to guess for you.

Clearly all you need to know is whether the beam is broken or not. You do need to use the IR modulated beams and mount the receivers at the end of (matte) black pipes or "silencers". If the LED on the module is indicating reception then you merely need to monitor that status. The module itself is actually decoding the presence of the 38 kHz for you.

you have failed to cite what modules you are using

To be fair, Paul, this is clearly an inexperienced user and we all failed to ask WHY.

To be fair, Paul, this is clearly an inexperienced user

Is that not the majority? :grinning:

That is at least half of what this forum is about.
The XY problem.

@marco_c: Thanks for that. Yeah that is the other option I have been considering but I want to make my system relatively fool proof so ANY infrared signal just doesn't increase the (sensor) system count. That's why I'm trying to decode it to ensure that the IR picked up is the same one sent the emitter.

I have been experimenting this with the Uno and the Mega. But will eventually try to implement this on the photon. I didn't think the controller mattered for the receiver since its just from a digital input... My question had more to do with the library and the code. But yeah, sorry for not mentioning it.

(On a side note: yes I'm new here and I guess inexperienced as well.... But I am sure everyone (including you guys) was inexperienced at some point. So we all learn and I'm learning too... :))

Thanks for all the help, guys!