Activity 1: Testing the Frequency Sweep
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.
Programming Frequency Sweep for Distance Detection
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.