VS1838b sensor as detector

Hi everyone
I want to use the VS1838b sensor as an detector of ir beam modulated in 38khz wave
but i couldn't get it to work
also i tried many software methods to generate the 38khz signal like sending 1 for 13 microseconds and 0 for 13 microseconds also i tried 26 microseconds each
another thing the output of the sensor in idle is different each time i don't know why
sometimes it give me 1's and sometimes 0's
can anyone help me
thanks

Have you got a link to the data sheet I can't find one.

It is vs1838b not v1838b sorry

How are you wiring it up and what software are you using to look at the result and to modulate your IR.
Please post the code correctly.

Grumpy_Mike:
How are you wiring it up and what software are you using to look at the result and to modulate your IR.
Please post the code correctly.

I connect one leg of the sensor with 5v from arduino nano and the other to ground
the third leg is connected to input pin 2 and test it with tv remote it gave me ones on idle once i press the remote key it gave me some code with ones and zeros so i think it works fine.
and connect the ir led to output pin 12 and use this code to modulate the 38khz

int i=0;
void setup() {
Serial.begin(115200);
pinMode(12, OUTPUT);
}

void loop() {
while(i<30){
digitalWrite(12,1);
delayMicroseconds(13);
digitalWrite(12,0);
delayMicroseconds(13);
i=i+1;}
i=0;
delay(500);
}

it gave me ones all the time

So what part of:-

Please post the code correctly.

are you stuck on.

Please read the how to use this forum sticky and find out.

why on earth have that stocking great delay at the end?

it gave me ones all the time

So where in the code are you reading this?

Sorry for dummy post i am new in this forum thing
i put this delay to keep track of data
pin 12 is connected to the ir led
pin 4 is connected to output of sensor

int i=0;
void setup() {
  Serial.begin(115200);
  pinMode(12, OUTPUT);
  pinMode(4, INPUT);
  }

void loop() {
 while(i<30){
  digitalWrite(12,1);
  delayMicroseconds(13);
  digitalWrite(12,0);
  delayMicroseconds(13);
  i=i+1;}
  i=0;
  Serial.println(digitalRead(4));
delay(500);
}

Thanks for posting it correctly.

pin 4 is connected to output of sensor

Yes but the point I am making is the code never looks at it so how can you tell what it is doing?

This line read and prints the output of the sensor connected at pin 4

Serial.println(digitalRead(4));

Thanks for clarifying that I missed it.

You said of this code

it gave me ones all the time

Yes what were you expecting?
You have just sent a pulse consisting of 30 cycles of 38KHz modulation. That is being interpreted by the sensor as a logic one which is what you are receiving. The sensor will hold that state for a short time once the IR stops and then it will revert to a zero.

If you block the sensor from the emitter it should show a logic zero.

That code will do nothing more. It looks like everything is working.

The problem is that it gave me ones even if i remove the led off
i think it should give me zero when it detect 38khz
so maybe the modulation technique is wrong!!

so maybe the modulation technique is wrong!!

How can it possibly be the modulation technique when you have removed the LED?
No LED should give you a zero.

Look at the data sheet. The way to wire this sensor up is with a series resistor on the power supply and a large decoupling capacitor directly across the chip.
The sheet also recommends a pull up resistor, so you could always enable the internal one on pin 4.

@minaedward: Your code generates the signals on the output, and after it finishes, it reads the detector input, so of course the sensor won't pick anything up!

I would do this:

  1. Generate a continuous 38kHz square wave with the Arduino, similar to what you're doing now but remove the delay.
  2. Hook up the output of the detector to the base of an NPN transistor. Connect the collector and emitter of the transistor to an LED powered by 5V with a resistor.

Turn everything on and aim the emitter at the detector. The LED should stay off until you break the beam with your hand or something opague to IR.

BTW, on my Uno R3, my scope shows that a delay of 7 usec is required for the ON and OFF cycles to achieve 38 kHz. I guess that means it takes ~6 usec to perform the digitalWrite command...

Good luck,
Brandon

It gives you one because of the pullup. The output of the sensor is zero when there is IR. (inverted)

split the program in 2 parts: make a receiving one for a start and test it with a normal tv remote,

then work on the rest of the program.