How to Turn Off IR Sensor Using Arduino code

Basically, I want to turn off the IR sensor based on the relay value. What I am doing is sending msg from the transmitter to the receiver and when the receiver gets the msg or value it turns on the relay. I want when the relay is on my IR sensor should be off. Can anyone help?

@rahul.singh1 please help me with this


Do you already have the components with you?

For you to achieve this, the only thing you have to do, is connect 5v supply to the common pin of the relay, Make sure no other wire is connected to the common pin.
and connect the Normally Open (NO) pin of relay to the 5v pin of the IR sensor. Make sure no other wire is connected to the NO pin and the 5v pin of the IR sensor.

Keep all other connections the same.

Thank you for your answer, @vinayak.joshi Sir. Yes, I have all the components. I tried it it worked but a little differently as I wanted my relay to on (green and red light on) and when it is on, at that time IR sensor must be off for that instead of NO I used NC and it worked what I got what I wanted, but sir the problem is not solved. Actually, COM And NO of the relay are already in use as I have to connect it to my vehicle’s battery. As I will be giving vehicles battery supply to the COM pin so that, it would be 12 V battery of the vehicle and we want 5v. It will work or not? If not Sir Please suggest any alternative?

Hi @pushpakgoswami645,

Apologies. I should have told you to use NC instead of NO.

Also if you want to control the flow of power at 2 different power levels to 2 different devices, it is best ot use 2 different relays for this. Making use of 1 might not be possible, (IDK if it is possible).

You can use the same input signal to toggle both the relay’s on at the same time.

Thank you for your response @vinayak.joshi Sir. I tried with one relay and at COM I have given 2 power supply one from a vehicle battery and one from the board that is 5V and it worked. Thanks for the help.

@vinayak.joshi Can’t we do it with code?

Hi @pushpakgoswami645,

@vinayak.joshi is on leave today. I have informed him about your query. He will be replying you by tomorrow EOD.

Do let me know in case you need further assistance.

Hi @pushpakgoswami645,

What is the voltage of your vehicle battery? If the vehicle battery voltage if 5v or the vehicle battery circuit and your sensor circuit are independent then I can understand that this might work without much issues. But if neither is the case, then essentially you are short circuiting 2 different voltage levels together. The issue might not be visible immediately, but eventually either your battery will drain out faster or the 5v supply circuit will blow up.

As for doing this via code, I personally do not know of any such method, nor have I ever attempted anything of the sort, nor have I seen anyone doing this. So I believe that it is not possible. You are welcome to try, but it might be risky for your hardware.

@vinayak.joshi thank you for the answer

This problem can be solved in two ways

  1. with code : since you are using arduino, you might have initiated a digital pin to relay and ir sensor, say you have initiated D0 pin to ir sensor and D1 pin to relay in the code, the moment you get a message you turn on the relay and turn off ir sensor together in conditional loop.
    i.e., if(val == msg){
    digitalWrite(relay, HIGH);
    digitalWrite(ir, LOW);
  2. with external hardware : you could use a inverter(NOT gate) that basically inverts your signal, therefore you could turn off ir sensor when relay output is high without tweaking the code.