Displaying Both Distances

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.

Example Sketch – DisplayBothDistances

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.

Serial Monitor output tests the distance detection zone of each IR LED/receiver pair

  • Enter, save, and upload DisplayBothDistances to your Arduino.
  • Use a box, book, bottle, or similar object as a target for the left distance detector.
  • Start by moving the object toward and away from the BOE Shield-Bot until you find the small range where a small movement will result in a change in distance measurement.
  • Find and record the midpoint of each distance detection zone.
  • Repeat for the right IR detector. 
  • If it turns out that the detection range of one side is twice as far as the other (or more), check the resistors connected to the IR LEDs.  You may have a mismatch there; make sure both resistors are 2 kΩ (red-black-red).  If there isn’t a mismatch, try adjusting IR LED resistor values until the detection ranges of both sides are in the same neighborhood.
 * 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

Your Turn – More Distance Tests

  • Try measuring the detection range for objects with different colors and textures.  Which colors and surfaces are easiest to detect? Which are most difficult?