Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

IR sensor Arduino code question
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

IR sensor Arduino code question

by elin05 on Wed Jul 15, 2020 6:28 pm

I have a question about the IR-sensing portion of the NeoPixel Basketball Hoop Arduino code:

Code: Select all | TOGGLE FULL SIZE
boolean isBallInHoop() {
  // Pulse the IR LED at 38khz for 1 millisecond
  pulseIR(1000);
 
  // Check if the IR sensor picked up the pulse (i.e. output wire went to ground).
  if (digitalRead(IR_SENSOR) == LOW) {
    return false; // Sensor can see LED, return false.
  }
 
  return true; // Sensor can't see LED, return true.
}

I know that this works because I have already tested it myself. However, I cannot understand why it is able to work.

Doesn't pulseIR(1000) and digitalRead(IR_SENSOR) happen sequentially? I would assume that after pulseIR(1000) returns, the IR signal as stopped and thus digitalRead(IR_SENSOR) would read high (i.e. it can't see the LED).

elin05
 
Posts: 58
Joined: Tue Jun 11, 2013 4:27 pm

Re: IR sensor Arduino code question

by adafruit_support_mike on Wed Jul 15, 2020 8:08 pm

The TSOP38238 has to see between 7 and 15 IR pulses before sending its output low, then waits for 6 to 7 missing pulses before sending its output high again. That gives the microcontroller at least 150us to read the output after it stops emitting pulses.

adafruit_support_mike
 
Posts: 61422
Joined: Thu Feb 11, 2010 2:51 pm

Re: IR sensor Arduino code question

by elin05 on Fri Jul 17, 2020 10:01 pm

adafruit_support_mike wrote:The TSOP38238 has to see between 7 and 15 IR pulses before sending its output low, then waits for 6 to 7 missing pulses before sending its output high again. That gives the microcontroller at least 150us to read the output after it stops emitting pulses.


Got it, thank you!

elin05
 
Posts: 58
Joined: Tue Jun 11, 2013 4:27 pm

Please be positive and constructive with your questions and comments.