How to Detect TSOP1838 input signal using PIC Controller

Discussion in 'Embedded Systems and Microcontrollers' started by asking, Sep 16, 2013.

  1. asking

    Thread Starter New Member

    Dec 6, 2011
    15
    0
    [​IMG]
    [​IMG]

    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.
     
  2. JohnInTX

    Moderator

    Jun 26, 2012
    2,341
    1,022
    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.
     
    asking likes this.
  3. asking

    Thread Starter New Member

    Dec 6, 2011
    15
    0
    Thanks for suggestion, will try it.
     
  4. Shagas

    Active Member

    May 13, 2013
    802
    74
    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
     
    asking likes this.
  5. asking

    Thread Starter New Member

    Dec 6, 2011
    15
    0
    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 ?
     
  6. JohnInTX

    Moderator

    Jun 26, 2012
    2,341
    1,022
    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: Sep 17, 2013
    asking likes this.
  7. Shagas

    Active Member

    May 13, 2013
    802
    74
    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.
     
    asking likes this.
  8. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    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.
     
    asking likes this.
  9. asking

    Thread Starter New Member

    Dec 6, 2011
    15
    0
    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


    [​IMG]
     
  10. asking

    Thread Starter New Member

    Dec 6, 2011
    15
    0
    thanks for your explanation :) i hope it helps..
     
  11. asking

    Thread Starter New Member

    Dec 6, 2011
    15
    0
    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
     
  12. asking

    Thread Starter New Member

    Dec 6, 2011
    15
    0
    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....
     
  13. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    Detecting a signal is a simple as seeing it "every so often."

    Consider this psudecode:

    Code ( (Unknown Language)):
    1.  
    2. Begin:    start timer
    3.  
    4. Loop:     if input = {we get signal} then goto Begin
    5.  
    6.           if timer < timeout then goto Loop
    7.  
    8. 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.
     
    asking likes this.
  14. asking

    Thread Starter New Member

    Dec 6, 2011
    15
    0
    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

    Code ( (Unknown Language)):
    1. void interrupt()
    2. {
    3.   //Timer0 Interrupt Handler
    4.   if (INTCON.TMR0IF == 1) // timer 0 interrupt
    5.   {
    6.     PORTB.F5 = ~PORTB.F5;
    7.     INTCON.TMR0IE = 1;
    8.     INTCON.TMR0IF = 0;   // clear the flag
    9.   }
    10.  
    11. }
    12.  
    13. void main()                      //MAIN;
    14. {
    15.  // Set Interrupt Enable bits
    16.   INTCON.T0IE = 1;  // bit 5 Timer0 Overflow Interrupt Enable
    17.   // setup the TMR0 configuration registers
    18.   OPTION_REG.T0CS = 1;  // bit 5  TMR0 Clock Source Select bit...0 = Internal Clock (CLKO) 1 = Transition on T0CKI pin
    19.   OPTION_REG.T0SE = 1;  // bit 4 TMR0 Source Edge Select bit 0 = low/high 1 = high/low
    20.   OPTION_REG.PSA = 0;   // bit 3  Prescaler Assignment bit...0 = Prescaler is assigned to the WDT
    21.   OPTION_REG.PS2 = 0;   // bits 2-0  PS2:PS0: Prescaler Rate Select bits
    22.   OPTION_REG.PS1 = 0;
    23.   OPTION_REG.PS0 = 0;
    24.   //TMR0 = 0;
    25.  
    26.  
    27.   //Set global Interrupt Enable bits
    28.   INTCON.GIE = 1;     // global interrput enable
    29.  TRISB = 0x00;                   //SET PORTB TO BE OUTPUT;
    30.  PWM1_Init(38000);
    31.  PWM1_Set_duty(150);
    32.      PWM1_Start();
    33.  TRISA = 0b00010000;
    34.  
    35.  
    36.  while(1){                       //INFINITE LOOP;
    37.           PWM1_Start();
    38.          Delay_ms(5);
    39.         PWM1_Stop();
    40.         Delay_ms(15);
    41.           }
    42. }                                //END;
     
  15. JohnInTX

    Moderator

    Jun 26, 2012
    2,341
    1,022
    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: Sep 19, 2013
  16. JohnInTX

    Moderator

    Jun 26, 2012
    2,341
    1,022
    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.
     
    Last edited: Sep 19, 2013
  17. asking

    Thread Starter New Member

    Dec 6, 2011
    15
    0
    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
     
  18. JohnInTX

    Moderator

    Jun 26, 2012
    2,341
    1,022
    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: Sep 19, 2013
  19. asking

    Thread Starter New Member

    Dec 6, 2011
    15
    0
    Perfect...your idea will match my project i think...thanks i will try and let you know..thanks again..
     
Loading...