Not sure If I understand How Ir works

Hello Internet.

I am using an IR Receiver Sensor 1838r and a Ir led.

For my project I want to create my own simple wireless serial communication system, so I need to code the 0s and 1s with an infrared signal and I'm not sure how to send a 0 to the receiver.

From what I have been reading, I understand that if the receiver receives a signal of 38khz at its output there will be a logical 0 and if nothing is received at that frequency in its output there will be a logical 1.
Is that correct?

I am trying to make a project following this reasoning, I have an arduino MEGA continuously reading the receiver output value and on the other hand an UNO arduino sending with a led a signal of 38Khz, which I generate with the code that I found in this forum and that I leave below.
So the arduino MEGA should be receiving all the time a 0, however this is not so and I can not understand why.

/* Code to pulse pin 3 with a modulated signal
* Can be used to drive an IR LED to keep a TSOP IR reciever happy
* This allows you to use a modulated reciever and a continious beam detector
* By Mike Cook Nov 2011 - Released under the Open Source licence
*/
volatile byte pulse = 0;

ISR(TIMER2_COMPB_vect){  // Interrupt service routine to pulse the modulated pin 3
    pulse++;
  if(pulse >= 8) { // change number for number of modulation cycles in a pulse
    pulse =0;
    TCCR2A ^= _BV(COM2B1); // toggle pin 3 enable, turning the pin on and off
  }
}

void setIrModOutput(){  // sets pin 3 going at the IR modulation rate
  pinMode(3, OUTPUT);
  TCCR2A = _BV(COM2B1) | _BV(WGM21) | _BV(WGM20); // Just enable output on Pin 3 and disable it on Pin 11
  TCCR2B = _BV(WGM22) | _BV(CS22);
  OCR2A = 51; // defines the frequency 51 = 38.4 KHz, 54 = 36.2 KHz, 58 = 34 KHz, 62 = 32 KHz
  OCR2B = 26;  // deines the duty cycle - Half the OCR2A value for 50%
  TCCR2B = TCCR2B & 0b00111000 | 0x2; // select a prescale value of 8:1 of the system clock
}

void setup(){
  setIrModOutput();
  TIMSK2 = _BV(OCIE2B); // Output Compare Match B Interrupt Enable
}

void loop(){
// do something here
}

That code is meant to be uploaded on an UNO.

You will see 200us @ 38Khz then nothing for 200us etc. coming out on pin 3.

Make sure you have the IR LED's polarity correct and a series resistor of ~220 ohms.

You should always show us a good image of the wiring and schematic.

Show use the Mega sketch.

The mega code is a simple analog read that's why I didn't post it.

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // print out the value you read:
  Serial.println(sensorValue);
  delay(1);        // delay in between reads for stability
}

The polarity is right I have checked it with my phone camera. For the led I am using an 80 ohms resistor because I read ir leds liked to get more current, should i change it?

I have made a simple schematic, leave it below.

@larryd how did you get the 200us number?

If you are driving the IR LED from the UNO pin 80 ohms is too low.
An UNO o/p safe current is ~20ma.
IR voltage drop is about 1.5V
R = (5v -1.5v)/.02A = 175 ohms
Try a series resistor of 150 to 220 ohms.

@larryd how did you get the 200us number?

I have measured that.

"I have made a simple schematic, leave it below."
Where?

-I duplicate the post below by error and I think I can´t delete it so that´s my solution-

I am sorry, :sweat_smile: I forgot to attach the picture :sweat_smile:

I am now using the 220 resistor, no change on the readings of the MEGA.

Yesterday I found this webpage--> IR Transmitter and Receiver Circuit Diagram

I think that the webpage confirms my theory, when the sensor recibes a 38kHz signal it goes from high to low, but mine isn't doing it.

I have also change the led for a new one just because, checked it with my phone camera and it is working.

I have also try different receivers without result.

Click on the image to make it bigger.

Hi,

You're barking up the wrong tree with analogRead. Try this:

/* Measures the pulses from a telly remote. Ignores the spaces.
 *  Arduino Pro Mini 5V, 16MHz, ATMega 328P
 */

const int IR_pin = 9;            // The output from the IR sensor
unsigned long duration;

void setup()
{
  Serial.begin(115200);
  pinMode(IR_pin, INPUT);
}

void loop()
{
  duration = pulseIn(IR_pin, LOW);
  if (duration)
  {
  Serial.println(duration);  
  }                        
}

If you don't have an amplifier for your emitter, the emitter and receiver need to be almost touching.

Hi @kayel, I will try this along this weekend thanks!

I was using analogRead because I want to keep it very simple and only code a 1 if the UNO is not emitting a signal and a 1 if it is.

I thought, because of this proyect I post earlier IR Transmitter and Receiver Circuit Diagram that that was the way the Reciber works, ouptputing a 1 when it recibes no signal and a 0 when it recibes a 38khz signal.

Can anyone explain me how the receiver works?

Also for the amplifier I could make a simple one this weekend with a transistor, @kayel do you know how much current should flow through the led for working on a distance of about 1 meter?

Thank you guys for your responses I appreciate that a lot. :slight_smile:

An IR receiver can be idle-high or low and the pins are not always in the same order (see Vishay site). If my little sketch doesn't work, try replacing:

"duration = pulseIn(IR_pin, LOW);"

by:

"duration = pulseIn(IR_pin, HIGH);"

Have a look at Adafruit learning, there's a good explanation of how IR works.

No idea about how much current for the emitter.

You need to create a 38KHz carrier wave using PWM and send bursts of that to your receiver.

The Uno sketch shown does the 38kHz modulation on it’s IR LED necessary for the Mega’s IR receiver to decode.

Yes, the Uno sketch is exactly what's needed. I didn't read it because it looked complicated compared to using PWM. My bad.

So, use digitalRead(A0) (or any other pin) instead of analogRead(A0) and put the emitter and receiver very close to each other and/or amplify the signal and it should work.

Kayel is correct. Instead, use a ir sensor with a built in amp like the tcrt5000 ir sensor. Go on Amazon and look up it sensor and get an all-in-one or sensor with a digital output (will be a four pin board of 5v, gnd, analog in, and digital in

Hello Everyone,
I have tried a pair of things but the results still aren't the expected ones.

Reading the documentation @kayel suggested I found that the receiver should behave like this:

IR detectors are digital out - either they detect 38KHz IR signal and output low (0V) or they do not detect any and output high (5V).

But on my tests it behave like I will explain below:

I am using this code on the UNO (because I thought it was easier to read than the other one) but I get the same results with the code I was using earlier.

#define cr_pin 8

void setup() {
  
  tone(cr_pin, 38000);  /* For modulation at 38kHz */
}

void loop() {
  
      tone(cr_pin, 38000); 
}

On the MEGA I am using:

#define IR_pin 8


void setup()
{
  Serial.begin(9600);
  Serial.print("hi");
  pinMode(8, INPUT);
}

void loop()
{
  int valor;

  valor = digitalRead(8);
  Serial.println(valor);
                            
}

All of this is very normal so for example if I put the uno in front of the mega with emitter and receiver facing each other, in normal position the emitter should be emitting and the receiver should be outputting a 0 and when I cover the emitter, the receiver should output a 1.
This is not happening.

But what is happening is that when they are facing each other on the Pin8 of the MEGA I am getting always 1 and when I cover the receiver I also get a 1, but when I cover and uncover the receiver I get a 0 for a short period of time.

For a better understanding, the reading on the pin 8 looks like this:

1
1
1
1
1
--------------I cover the led
1
1
1
1
--------------uncover it
0
0
0
0
0
0
0
0
1
1
1
1
1
1
1
1

That looks very strange to my like if my sensor only responds to positive flanks (or rising edges, I don't know the proper translation :sweat_smile: ), but according to the datasheet it shouldn't be like this.

Do you have any ideas?

Also using the PulseIn() method @kayel suggested I got a similar behavior.

I want to fix that, because it is driving me nuts viewing not knowing how the receiver works.

@UsernameD I have bought these because I think they will fit the purpose, but even though I still want to be able to do it with the first sensor, only because of learning how to.

This is the original reading from the serial in case it is of any help.

1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
0
0
0
1
1
1
1
1
1
1
1
1
1
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
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1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1

I found a datasheet for a part with the same numbers but different letters. Anyway, check out the schematic at item 3. It shows a series resistor to the sensor and an external pullup. I recently pulled an IR sensor board from a roadside TV and it had a circuit very similar to the one shown on the data sheet. Maybe you need a pullup resistor.

First off, there's nothing wrong with your understanding of how the IR sensor works, except that some sensors are normally low and go high when they receive a signal, instead of being normally high and going low when they receive a signal.

If I were you I'd start by doing this test with your receiver and a telly remote:

If that doesn't work, either your receiver is broken or the pins are not in the order you think they are. Check your telly remote is working too!

I use the VS1838B and the pinout is the same as in the Adafruit tuto. It's normally high and goes low when it receives a signal. I couldn't find a VS1838r.

You don't need a pull up/down resistor, the receiver does that.

With my sketch you should have got something like the serial output below:

Hi,

I have tested the pair of things kayel suggested.

The test adafruit suggests went fine, at normal the led was very dimmed (I had to turn off the lights on my room to appreciate that the led was ON) and when I send an ir code with a remote it flashed turning off and back on again a couple of times so everything ok.

Also I used an Ir library to test its connection to the mega and when I pulsed the button on my remote it gave me the NEC code on the serial so we can say the sensor is working.

So these tests aren't telling us why it isn't behaving like it should.

I also made the test @kayel suggests but this time with my emitter on the uno.

I got something like this: on the serial

NOTHING

(I cover the led)

NOTHING

(I uncover it)

185949
12281

NO CHANGES

(I cover the led)

NO CHANGES

(I uncover it)

185975
214576

and so on

So we can deduce it is still behaving like the other day only responding to rising edges and not at it should.

Later this week I will test the circuit @dougp suggested.

@antoniobeta34

The answer to your question is probably here:

https://forum.arduino.cc/index.php?topic=406847.0

Many IR receivers can't detect a continuous carrier.

Hi again guys.

I a couple of days ago I received this https://www.ebay.es/itm/10x-LED-IR-5-emisor-5-receptor-3mm-infrarrojo-infrared-diode-940nm/322892446138?ssPageName=STRK%3AMEBIDX%3AIT&_trksid=p2060353.m2749.l2648

I am connecting the phototransistor using a pull up configuration and also I am driving the led using a simple driver (schematic attached) (I am using a 80 ohms resistor instead)

I have been testing it and it works as expected but its range is very limited, about 5 cm, and i need at the very least to double that.

For what i have been reading I could make the 80 ohms resistor lower, about 10ohms if I drive the led on a small duty cycle. Also i have read that modulating the signal should also help with that.

I will try the modulating way because I want my final project to be powered by an attiny on a coin cell battery.

Do you have any advice/interesting links for a how to on the modulation?

Also @kayel your link was very interesting and I think that maybe the TSSP form vishay will work fine but they are expensive and difficult to find in china, because of that i will try to make the modulation my self and if it doesn't work i will buy and try them. Thank you again.