How to change the internal logic of an IR sensor module?

I have an IR sensor module() and i have attatched it to my arduino connecting the GRD and VCC pin of the sensor to the arduinos 5V and GND. I then connected the output pin of the sensor to the digital pin 8 of the arduino.

The usual logic of the sensors is that when you place it in front of a white surface the output goes HIGH (1) and the indicator light comes on. but in my case it does the opposite. I ran an experiment to test my hypothesis. I tried this code with my left sensor to see what output i would get in the serial monitor.

//Start of Code

int ls, rs;

void setup() {

pinMode(8, INPUT);

Serial.begin(9600);

Serial.println("Sensor is ready");

}

void loop() {

int ls = digitalRead(8);

Serial.println(ls)

delay(1000); // If you dont but delay serial monitor will print multiple values of POT

}

//End of Code

When i placed the sensor in front of a white surface the serial monitor was displaying a LOW(0) output instead of high even though the indicator LED light was on, proving that there is something wrong with the logic of the sensors. Is there any way i can change the output i get with my sensors?

What makes you sure of the default output state of such modules?
You can invert the signal in code.

I wonder what's the purpose of that IR receiver. Remote control uses a carrier frequency of about 40kHz, a white surface should not result in any output, and it does not have an indicator LED. Do you have an IR light barrier module? Or a motion sensor? Any link to the data sheet were helpful.

Please present code in Code Tags </>.

An open collector output for such sensor modules is quite usual. This will give exactly the “inverted” effect which you have observed.

See if this works like you want:

void setup() {
pinMode(8, INPUT);
Serial.begin(9600);
}
void loop() {
Serial.println(! digitalRead(8));
delay(1000);
}

Hi @nivekoman,

To make life easier for those who are willing to help you, please give information about your project, the components used, if possible images and links.
This IR sensor of yours, what model is it? Do you have an image, is there a link that we can see or see its characteristics.......?

RV mineirin

Also please go back and edit your code to place it in code tags after removing the unnecessary blank lines.

As stated, sensor states are arbitrary. "0=off 1=on" is only a convention. So you can also:

#define IR_ON 0
#define IR_OFF 1

void setup() {
pinMode(8, INPUT);
Serial.begin(9600);
}
void loop() {
int ir_reading = digitalRead(8);
if ( ir_reading == IR_ON ) {
  Serial.println(1);
else {
  Serial.println(0);
}
delay(1000);
}

Hi, @nivekoman
Welcome to the forum.

Can you please post a link to data/specs of the IR sensor?

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

Thank you @anon57585045. Its my first time submitting to this forum. I will henceforth put the code in the code tags and remove black lines. Ive tried the code and its giving the output i wanted.

Thanks @6v6gt

Hi @TomGeorge. Ive decided to change my code instead of trying to change the output of my sensor module itself. :grinning:

Hi @DrDiettrich, thans for the advice. I'll invert the signal in code instead. The project i am working on is a line follower robot. sorry im new to the forum and didnt read the instruction before posting.

It works, thank you @JCA34F :smiley:

Hi @ruilviana , thanks for the advice. :+1: I will do this henceforth. I have solved my query for now.

Hi, @nivekoman

Just so we know which PIR you are working with can to post a link to specs/data or where you purchased it?

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

Just so you know, this is not a code tag.

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