Here's a graph from one specific brand of IR detector’s datasheet (Panasonic PNA4602M; a different brand may have been used in your kit).
The graph shows that the IR detector is most sensitive at 38.5 kHz—its peak sensitivity —at the top of the curve. Notice how quickly the curve drops on both sides of the peak. This IR detector is much less sensitive to IR signals that flash on/off at frequencies other than 38 kHz. It’s only half as sensitive to an IR LED flashing at 40 kHz as it would be to 38 kHz signals. For an IR LED flashing at 42 kHz, the detector is only 20% as sensitive. The further from 38 kHz an IR LED’s signal rate is, the closer the IR receiver has to be to an object to see that IR signal’s reflection.
The most sensitive frequency (38 kHz) will detect the objects that are the farthest away, while less-sensitive frequencies can only detect closer objects. This makes rough distance detection rather simple: pick some frequencies, then test them from most sensitive to least sensitive. Try the most sensitive frequency first. If an object is detected, check and see if the next-most sensitive frequency detects it. Depending on which frequency makes the reflected infrared no longer visible to the IR detector, your sketch can infer a rough distance.
Frequency Sweep is the technique of testing a circuit’s output using a variety of input frequencies.
The next diagram shows an example of how the BOE Shield-Bot can test for distance using frequency. In this example, an object is in Zone 3. That means that the object can be detected when 38000 and 39000 Hz is transmitted, but it cannot be detected with 40000, 41000, or 42000 Hz. If you were to move the object into Zone 2, then the object would be detected when 38000, 39000, and 40000 Hz are transmitted, but not with 41000 or 42000 Hz.
The irDistance function from the next sketch performs distance measurement using the technique shown above. Code in the main loop calls irDistance and passes it values for a pair of IR LED and receiver pins. For example, the loop function uses int irLeft = irDistance(9, 10) to check distance to an object in the left side of the BOE Shield-Bot’s detection zone “field of vision.”
// IR distance measurement function int irDistance(int irLedPin, int irReceivePin) { int distance = 0; for(long f = 38000; f <= 42000; f += 1000) { distance += irDetect(irLedPin, irReceivePin, f); } return distance; }
The irDetect function returns a 1 if it doesn’t see an object, or a zero if it does. The expression distance += irDetect(irLedPin, irReceivePin, f) counts the number of times the object is not detected. After the for loop is done, that distance variable stores the number of times the IR detector did not see an object. The more times it doesn’t see an object, the further away it must be. So, the distance value the function returns represents the zone number in the drawing above.
It’s important to test that the detection distances are roughly the same for both IR LED/receiver pairs. They don’t have to be identical, but if they are too far apart, the BOE Shield-Bot might have difficulty following an object in front of it because it will keep trying to turn to one side. More subsystem testing!
A common cause of mismatched distance measurement is mismatched resistors used with the IR LEDs. For example, if one side’s IR LED has a 1 kΩ resistor and the other has a 2 kΩ resistor, one side will need objects to be much closer to see them. Another possibility, though rare, is that one IR detector is far more sensitive than the other. In that case, a larger resistor can be used in series with the IR LED on that side to make its IR headlight dimmer and correct the mismatched measurements.
This screencapture shows some detection zone measurements from DisplayBothDistances in the Serial Monitor. Though there’s fluctuation in the values, commonly called noise, what matters is that the numbers match, or are off by only 1, in each pair of measurements.
/* * Robotics with the BOE Shield - DisplayBothDistances * Display left and right IR states in Serial Monitor. * Distance range is from 0 to 5. Only a small range of several centimeters * in front of each detector is measureable. Most of it will be 0 (too * close) or 5 (too far). */ void setup() // Built-in initialization block { tone(4, 3000, 1000); // Play tone for 1 second delay(1000); // Delay to finish tone pinMode(10, INPUT); pinMode(9, OUTPUT); // Left IR LED & Receiver pinMode(3, INPUT); pinMode(2, OUTPUT); // Right IR LED & Receiver Serial.begin(9600); // Set data rate to 9600 bps } void loop() // Main loop auto-repeats { int irLeft = irDistance(9, 10); // Measure left distance int irRight = irDistance(2, 3); // Measure right distance Serial.print(irLeft); // Display left distance Serial.print(" "); // Display spaces Serial.println(irRight); // Display right distance delay(100); // 0.1 second delay } // IR distance measurement function int irDistance(int irLedPin, int irReceivePin) { int distance = 0; for(long f = 38000; f <= 42000; f += 1000) { distance += irDetect(irLedPin, irReceivePin, f); } return distance; } // IR Detection function int irDetect(int irLedPin, int irReceiverPin, long frequency) { tone(irLedPin, frequency, 8); // IRLED 38 kHz for at least 1 ms delay(1); // Wait 1 ms int ir = digitalRead(irReceiverPin); // IR receiver -> ir variable delay(1); // Down time before recheck return ir; // Return 1 no detect, 0 detect }