How to Detect TSOP1838 input signal using PIC Controller

Thread Starter

asking

Joined Dec 6, 2011
15



1st Image represent contious signal from PIC Driven IR @ 38.3KHZ (output from TSOP1838 out pin)
2nd Image Represent when Signal is break (beam break Detector) (output from TSOP1838 out pin)

I need help, how to use Change on Interrupt function of PIC to detect if the beam is broken or not ? signal is having 8.7hz frequency so every 0.114 Seconds it changes so its just like led blinking... how to detect the straight line and stay normal while 8.7hz signal is received ?

i am trying to make Beam break detector. But i am not able to differentiate the signal output from TSOP1838.

Please help.
 

JohnInTX

Joined Jun 26, 2012
4,787
Run a timer/counter as a missing pulse detector i.e. set a timer up to interrupt the processor when it overflows. Instead of interrupt on change set up one of the external INTx pins to interrupt the processor on a falling edge. Set a flag indicating presence of signal for the main program to examine.

Clear the timer and enable its overflow interrupt on each external interrupt i.e. on each pulse of IR. When the signal goes away, the timer counts up to overflow, interrupts the processor and clears the flag indicating no signal. Clear the timer overflow IE to prevent further interrupts until the signal returns.

So one timer, two interrupts and you are rocking.

Have fun.
 

Shagas

Joined May 13, 2013
804
First of all , check that your TSOP is working correctly before you start programming.
When the TSOP is recieving the 38khz carrier then the VOUT should be 0 volts.
If there is no carrier then the output on the TSOP is 5 volts.

Why are you sending the carrier at 8.7 hz?
I made this project recently and I made it so the Led is continuously sending out the carrier and when the beam breaks then the TSOP output goes to 5V . The TSOP output is connected to an external interrupt which is configured to sense any logical change.
When it senses the first logical change from 0v to 5 volts that means the beam has been broken and if it goes back to 0 volts then it interrupts again so you know that the beam is not broken anymore.

Works pretty good but I'm using an AVR
 

Thread Starter

asking

Joined Dec 6, 2011
15
I am feeding 38.3KHZ signal (10us burst) to IR LED and on the Receiver side TSOP1838 i am getting 8.7HZ output. i should get 5V on continuous signal reception on TSOP out pin ?

But i am not able to find it on datasheet about it details... Please help me m confused... did you connected any capacitor on out and ground pin of TSOP ?
 

JohnInTX

Joined Jun 26, 2012
4,787
According to the datasheet (see Suitable Data Format) the modulated IR signal should not be continuous - there must be a >15ms gap (no IR) at least each 90ms.

Also, TSOP1838 is listed as obsolete. TSOP4838 is listed as a possible replacement.

You should decouple the power and ground of the TSOP but a capacitor on the output won't fix your IR problem.
 
Last edited:

Shagas

Joined May 13, 2013
804
I am feeding 38.3KHZ signal (10us burst) to IR LED and on the Receiver side TSOP1838 i am getting 8.7HZ output. i should get 5V on continuous signal reception on TSOP out pin ?

But i am not able to find it on datasheet about it details... Please help me m confused... did you connected any capacitor on out and ground pin of TSOP ?
First of all , make sure which TSOP you have exactly so you know the precise carrier frequency and so you can find the datasheet etc.

As mentioned above , if your one is obsolete then get a replacement which has a datasheet.

But nevertheless if you know that your TSOP reciever is rated for 38khz then okay .

How I would proceed:

First i would make sure that your Pic is actually outputting 38khz with 50% duty cycle!
Make sure that there is nothing else on that carrier, you might have some low frequency modulation issue . Check your software aswell.

You are getting some 8 herz on the output which means something is wrong.
As I mentioned : When the TSOP 'sees' a carrier it goes down to 0 volts at output. When it doesn't see a carrier or if the signal is too weak then it shoots up to 5 volts . You either get a 0 or 5 volts on the output (according to my experiments) so if you are constantly shining 38khz on the led to the TSOP then the TSOP will have a constant output of 0 volts.

Also , what wavelength is your Ir diode? I think the recommended is 980nm but I don't think that this is your problem.

Probe anything you can with your oscilloscope , including the PSu to find the problem.
 

ErnieM

Joined Apr 24, 2011
8,377
Vishay Data Sheet said:
Some examples for such disturbance signals which
are suppressed by the TSOP18... are:
• DC light (e.g. from tungsten bulb or sunlight),
• Continuous signal at 38kHz or at any other
frequency,

• Signals from fluorescent lamps.
• Continuous IR signal (e.g. 1ms burst, 2ms pause)
It is possible the low frequency output is the TSOP1838 attempting to eliminate the "disturbance" your continuous wave input is causing. Easy way around that is to just turn the IR busts on and off, something like burst of 20 cycles followed by off for 20 cycles... then the occasional 90 mS signal gap. The off times and gaps are there for the sensor (which is actually quite sophisticated with automatic gain control) to differentiate between signal and ambient noise.

20 on then 20 off is 1 mS. You may find that 20 pulses on followed by the 90 mS gap works too. That means you get to detect your beam 10 times a second, still very responsive to human time scale events.
 

Thread Starter

asking

Joined Dec 6, 2011
15
First of all , make sure which TSOP you have exactly so you know the precise carrier frequency and so you can find the datasheet etc.

As mentioned above , if your one is obsolete then get a replacement which has a datasheet.

But nevertheless if you know that your TSOP reciever is rated for 38khz then okay .

How I would proceed:

First i would make sure that your Pic is actually outputting 38khz with 50% duty cycle!
Make sure that there is nothing else on that carrier, you might have some low frequency modulation issue . Check your software aswell.

You are getting some 8 herz on the output which means something is wrong.
As I mentioned : When the TSOP 'sees' a carrier it goes down to 0 volts at output. When it doesn't see a carrier or if the signal is too weak then it shoots up to 5 volts . You either get a 0 or 5 volts on the output (according to my experiments) so if you are constantly shining 38khz on the led to the TSOP then the TSOP will have a constant output of 0 volts.

Also , what wavelength is your Ir diode? I think the recommended is 980nm but I don't think that this is your problem.

Probe anything you can with your oscilloscope , including the PSu to find the problem.
Dear,

Actually i got it why i am getting 8.7HZ frequency output. I read the datasheet of TSOP1838B and its written that there should no continous 38KHZ Signal else it will not accept. So i kept 90-100ms Burst signal of 38.8KHZ hence i am getting 8.7HZ in output. But i think now i will try as you said continous signal of 38.3KHZ with 50% duty cycle. lets see


 

Thread Starter

asking

Joined Dec 6, 2011
15
It is possible the low frequency output is the TSOP1838 attempting to eliminate the "disturbance" your continuous wave input is causing. Easy way around that is to just turn the IR busts on and off, something like burst of 20 cycles followed by off for 20 cycles... then the occasional 90 mS signal gap. The off times and gaps are there for the sensor (which is actually quite sophisticated with automatic gain control) to differentiate between signal and ambient noise.

20 on then 20 off is 1 mS. You may find that 20 pulses on followed by the 90 mS gap works too. That means you get to detect your beam 10 times a second, still very responsive to human time scale events.
thanks for your explanation :) i hope it helps..
 

Thread Starter

asking

Joined Dec 6, 2011
15
can anyone tell me exact wiring details of TSOP1838 ? and what are chance of blowing up tsop by reversing the out and vcc ? GND is common inbetween 1st and 3rd leg...but i am not able to identify as in google images there are different results for each...unique search
 

Thread Starter

asking

Joined Dec 6, 2011
15
Finally i checked with contant 38.3KHZ signal beam on IR receiver TSOP1838B but its not detecting...output is always 5V high. If i break the beam for few miliseconds there's change in the Oscilloscope signal and then again constant 5V signal. Means data is correct. It's rejecting continous beam of 38KHZ signal. So i kept again 90ms burst signal of 38khz beam and it works fine. Means i need to do tedious job of finding the missing edge detector or reset the timer every falling edge. But i am noob i haven no idea exactly how i will identify 8.7HZ signal is present or not :( can anyone help me to build the mikroc or C code for detecting continous 8.7HZ signal.

thanks....
 

ErnieM

Joined Apr 24, 2011
8,377
Detecting a signal is a simple as seeing it "every so often."

Consider this psudecode:

Rich (BB code):
Begin:    start timer

Loop:     if input = {we get signal} then goto Begin

          if timer < timeout then goto Loop

Alarm:    Enable {INTRUDER ALERT INTRUDER ALERT}
An 8.7Hz signal has a period of .114 seconds. If you make the timer timeout greater then that then it not trip when it sees IR, and will trip after a maximum delay of the timeout time.
 

Thread Starter

asking

Joined Dec 6, 2011
15
I am stucked... how to detect my PWM output signal from TSOP ? how to write logic for that in mikroc ?

i have written one code which will generate 38KHZ PWM signal and which will detected through TSOP1838. But i don't know how to detect, i mean how to sense 8.7HZ signal is available or not... Please help me to edit my code..i want to make beam break detector

Rich (BB code):
void interrupt()
{
  //Timer0 Interrupt Handler
  if (INTCON.TMR0IF == 1) // timer 0 interrupt
  {
    PORTB.F5 = ~PORTB.F5;
    INTCON.TMR0IE = 1;
    INTCON.TMR0IF = 0;   // clear the flag
  }

}

void main()                      //MAIN;
{
 // Set Interrupt Enable bits
  INTCON.T0IE = 1;  // bit 5 Timer0 Overflow Interrupt Enable
  // setup the TMR0 configuration registers
  OPTION_REG.T0CS = 1;  // bit 5  TMR0 Clock Source Select bit...0 = Internal Clock (CLKO) 1 = Transition on T0CKI pin
  OPTION_REG.T0SE = 1;  // bit 4 TMR0 Source Edge Select bit 0 = low/high 1 = high/low
  OPTION_REG.PSA = 0;   // bit 3  Prescaler Assignment bit...0 = Prescaler is assigned to the WDT
  OPTION_REG.PS2 = 0;   // bits 2-0  PS2:PS0: Prescaler Rate Select bits
  OPTION_REG.PS1 = 0;
  OPTION_REG.PS0 = 0;
  //TMR0 = 0;


  //Set global Interrupt Enable bits
  INTCON.GIE = 1;     // global interrput enable
 TRISB = 0x00;                   //SET PORTB TO BE OUTPUT;
 PWM1_Init(38000);
 PWM1_Set_duty(150);
     PWM1_Start();
 TRISA = 0b00010000;


 while(1){                       //INFINITE LOOP;
          PWM1_Start();
         Delay_ms(5);
        PWM1_Stop();
        Delay_ms(15);
          }
}                                //END;
 

JohnInTX

Joined Jun 26, 2012
4,787
So, you have one PIC that generates the pulsed-38KHz modulated IR to transmit and also needs to be the missing pulse (of demodulated IR by the TSOPxxx) detector and then raise an alarm?

Which PIC are you using (sorry if you already said). But if it has two PWMs, you can generate the pulsed 38KHz with the PWMs alone by driving the anode of the LED (through a resistor) with the 38KHz output and pulling down the cathode of the LED with PWM2 running at some pulsing rate. Simple. No interrupts. Nice.

So far, it seems you have the right idea about the basics, though.
 
Last edited:

JohnInTX

Joined Jun 26, 2012
4,787
can anyone tell me exact wiring details of TSOP1838 ? and what are chance of blowing up tsop by reversing the out and vcc ? GND is common in between 1st and 3rd leg...but i am not able to identify as in google images there are different results for each...unique search
I was going to refer you to the datasheet link in a previous post but when I checked it out it went to a page with the message 'Your require pages is cannot open by blow'.

That is why old coots like me like hardcopy.

So...here is your part and the suggested current one. The pinouts are in these documents. Whether its popped due to mis-wiring, its hard to tell. Scope it and see.
 

Attachments

Last edited:

Thread Starter

asking

Joined Dec 6, 2011
15
Yes i got the Correct Pinouts now. The TSOP i am using is 1838B and i got correct pins.

for Microncontroller part, its PIC16F628A i will usse it's PWM module to generate the 38KHZ signal and i want it to sense the same. Please guide me with some coding idea...

thanks
 

JohnInTX

Joined Jun 26, 2012
4,787
The '628 has only one CCP/PWM module. Pity.

And I don't know what your PIC freq is so... we may have to bump a few things around..

But here's something to think about: The PWM uses TMR2/PR2 to create the PWM timebase. You should be able to set it up so that the PWM outputs a continuous 38KHz by a combination of the Tcyc, TMR2 Prescaler and PR2 to set the period of 1/38KHz. . Next, set up CCP1 in PWM mode and set the values of CCPR1H/L for a 50% duty cycle based on the 1/2 the number of TMR2/PR2/Prescaler counts that make up a whole 1/38KHz cycle.

You should now have a continuous 38KHz IR carrier freq square wave on CCP1 output. Next, add pulses.

I would try to use the TMR2 postscaler, set at 1:16, to generate a TMR2 interrupt every 16 cycles of 38KHz IR output by the PWM. 16 cycles of IR is enough to be detected by the TSOP. Connect the anode + resistor to the output of the PWM and connect the cathode to your IO line. Toggle the IO line every TMR2IF. Now, the cathode will be toggled off/on every 16 of the 38KHz IR carrier cycles (for a period of 2*421uS per TMR2*16 postscaler interrupt).

If the TSOP is working right, you should get a square wave out of it with a 842 us period. Connect that to the RB0/INT pin and configure the IRQ for interrupt on a falling edge.

Use timer 1 or 0 as the missing pulse detector. Set it up for some period longer (lots longer maybe) than 842 us and reset it every time you get an interrupt on INT. The INT service routine clears the timer every 842us. When the pulsed IR ceases, the timer will eventually overflow and sets its TMR0/TMR1 IF flag. At this point, you can interrupt the processor or just poll the flag, as you see fit.

Nice.
 
Last edited:
Top