Shop OBEX P1 Docs P2 Docs Learn Events
IR Transmitter/Reciever — Parallax Forums

IR Transmitter/Reciever

FoozahFoozah Posts: 2
edited 2011-10-17 17:41 in Accessories
https://www.parallax.com/StoreSearchResults/tabid/768/List/0/SortField/4/ProductID/178/Default.aspx?txtSearch=ir+transmitter
https://www.parallax.com/StoreSearchResults/tabid/768/List/0/SortField/4/ProductID/177/Default.aspx?txtSearch=ir+transmitter

Currently I am playing with these sensors using Arduino. I want to produce a steady beam and alert when that beam is broken.

[HTML]
#define PIN_IR 3 //analogue
#define PIN_DETECT 5 //analogue
#define PIN_STATUS 13 //digital LED

int curVal = 0; // variable to store the current value of IR sensor
int lastVal = 0; // variable to store the last value of IR sensor
int threshold = 300; // threshold to trigger alarm the bigger this is the easer it is
boolean alarm = false;

void setup()
{
Serial.begin(9600);
pinMode(PIN_DETECT, INPUT);
pinMode(PIN_IR, OUTPUT);
pinMode(PIN_STATUS, OUTPUT);
digitalWrite(PIN_STATUS, LOW);
digitalWrite(PIN_IR, HIGH);
}

void loop()
{
doWatch(); delay(20);
if(alarm) {
digitalWrite(PIN_STATUS, HIGH);
delay(10000);
digitalWrite(PIN_STATUS, LOW);
alarm = false;
}
}

void doWatch() {
curVal = analogRead(PIN_DETECT); // read the input pin
if (abs(curVal - lastVal) >= threshold) alarm = true;
Serial.println(curVal - lastVal); // debug value
lastVal = curVal;
}[/HTML]


I'm just getting constant values between like 10 and -10 no matter where I move the IR transmitter. Anyone have a clue what I am doing wrong here? It seems like the receiver doesn't recognize the transmitter. I'm a newbie so any help is appreciated!

Comments

  • WBA ConsultingWBA Consulting Posts: 2,933
    edited 2011-10-12 10:25
    Foozah, welcome to the forums!

    My C is very minimal, so I can't help much with code, but it appears that you are just simply lighting the IR LED. The IR receiver responds to a 38khz carrier and that may be why you are not seeing results you expect. While written for the Basic Stamp, you may want to look at the Infrared Decoding and Detection appnote that is on the LED and Receiver product pages. That may help you better understand the receiver so you can modify your code.
    Good luck
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2011-10-12 17:17
    I PM'd a few guys, but they haven't come to help you. Maybe they're PM'ing with you.
    I have a couple of nanos, but I'm not up to speed on your arduino deal yet.

    Like WBA was getting at, you have to hit that 'receiver' with a 38kHz signal/emission, but not a constant transmission - it has to go out in bursts: several milliseconds of 38kHz, with a brief pause, repeat.

    Though the receiver can be swamped by ambient IR, it's "looking" for a coherent signal
    1/38000 = approx 26 usec (close enough, it doesn't have to be EXACT)

    loop (infinite)
    [several reps]: digitalwrite HI,
    delaymicroseconds(13),
    digitalwrite LO
    delaymicroseconds(13)
    delay(1) //

    The output of the receiver will be digital 0 or 1 depending on presence of 38kHz (or not).
  • ercoerco Posts: 20,255
    edited 2011-10-12 17:21
    PJ Allen wrote: »
    I have a couple of nanos, but I'm not up to speed on your arduino deal yet.

    GASP!

    PJ, does the dark side REALLY have free cookies?

    All you can eat? :)
  • FoozahFoozah Posts: 2
    edited 2011-10-12 23:24
    Cool, so it looks like I am missing this "syncing feature." I will look through the stamp code and try to figure something out.
    Previously, I was just turning to IR transmitter on and assuming the receiver would detect it. I can do the code fine once I understand how these IRs work. If you have any other thoughts, I would greatly appreciate it.
    Basically with the Arduino, setup() executes once in the beginning, then loop() continues until you decide to end the program. I am assuming this syncing feature will have to happen in the loop() since it has to sync every so often.
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2011-10-13 05:38
    Yes, I think you're dialed in.
    A few years ago I made a youtube vid showing how to check a remote with one of these (similar) IR 'receivers'. It's a simple enough circuit (shown at the beginning.)
    So -
    • Set that up, and confirm its operation with (most) any remote.
    • Then, program your micro to emit 38kHz (via your IRED) in an "infinite loop"
    • Aim that at your 'receiver'/ckt.
    • When you interrupt that signal path (waggle your finger between the two) then the rcvr LED will/should change states.

    I'm not advanced in the arduino drill, I only began last week. Its structure is similar to Spin (for our 'Propeller'), but you guys have to type a lot more.
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2011-10-13 08:27
    I think that this is, rudimentarily, what ought to be done for the 'transmitter':
    void loop()
    {
      digitalWrite(outPin, HIGH);   // sets the pin on
      delayMicroseconds(13);        // pauses for [COLOR="red"]13[/COLOR] microseconds      
      digitalWrite(outPin, LOW);    // sets the pin off
      delayMicroseconds(13);        // pauses for [COLOR="red"]13[/COLOR] microseconds      
    }
    
    
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2011-10-13 15:55
    Foozah,

    Success!
    With the programming below, this works with the IR_rcvr setup as noted (#6) in my youtube vid.
    The confirmation LED is on till the beam is broken.
    /*
      OS_38kc_IR
    
     */
     
    int passes = 0;
    
    void setup() 
    {  
      // 38kc bursts xmit on D3 for 38kc IR rcvr  
      // initialize the digital pin as an output.
      // Pin 3 has an IRED connected
      pinMode(3, OUTPUT); 
      delay(1000);  
    }
    
    void loop() 
    {
        if (passes < 20) 
        {
          digitalWrite(3, HIGH);   // set the IRED on
          delayMicroseconds(13);  // wait for 13u sec
          digitalWrite(3, LOW);    // set the IRED off
          delayMicroseconds(13);  // wait for 13 usec
          passes ++;
        }
        else if (passes == 20) 
        {                           // 
          delay(2);              // wait 2msec
          passes = 0;               // reset idx, recycle
        }  
    }
    

    Please update (post) if you have success or need further assistance.
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2011-10-17 17:41
    Foozah,
    You've left me hangin', Bro.
    I haven't looked into the "interrupt" feature, but I cranked out this much
    int passes = 0;
    byte x = 0;
    byte dets = 0;
    
    void setup() 
    {  
      // 38kc bursts xmit on D3 for 38kc IR rcvr 
      // LO means IR is present (active state)
      // initialize the digital pin as an output.
      // D3 has an IRED connected
      pinMode(3, OUTPUT);
      pinMode(4, INPUT); 
      pinMode(5, OUTPUT); 
      delay(1000);   
    }
    
    void loop() 
    {    
       while (passes < 25) //
       {
          digitalWrite(3, HIGH);   // 
          delayMicroseconds(13);   //
          digitalWrite(3, LOW);    //
          delayMicroseconds(2);
          x = digitalRead(4);      // 
          
          if (passes > 15 && x == 1)
            {
              dets ++;
            }
            
          passes ++;
       }    
    
       if (dets == 0)
         {
            delayMicroseconds(120);
         }
       if (dets > 0)
         {
            digitalWrite(5, HIGH);
            delay(2500);
            digitalWrite(5, LOW);
         }
       passes = 0;
       dets = 0;
    }      
    

    I've stripped the comments from the above because the window blows the formatting, making it look like carp. The comments are in the PDE in the ZIP attached. The "Att.Mgr" doesn't allow PDEs, you have to ZIP them.

    Anyway, seems to do the job, but I've only used/tested it on the bench.
    Ought to shorten the IRED HIGH time a couple of usec - the IRED cycle time is 30usec (33kc), which is a bit (>10pct) "off freq", but still effective.

    I see you guys do like place your open braces weird. It's hard for me to keep track of with your convention, but by placing them in the same column, my way, it's lots easier to see and register. It compiles just the same.

    OS_IR_38kc_det.zip
Sign In or Register to comment.