Section outline

  • To make a robot follow a line on the ground, we need to help it “see” the line. This is done using infrared (IR) sensors that detect differences in surface color.

    • 🔍 How IR Sensors Work

      • IR sensors emit invisible light and detect how much is reflected back.
      • White surfaces reflect IR light, while black surfaces absorb it.
      • This lets the sensor detect if it’s over a white or black area.

      🎯 Application in Line Following

      Robots can use multiple IR sensors (usually 2 or 3) to detect their position relative to a black line on a white background:

      • If the center sensor is on the line, go straight.
      • If the left sensor sees the line, turn left.
      • If the right sensor sees the line, turn right.
    • 🧰 Components Needed

      • 1 x Arduino Uno
      • 2 or 3 x IR sensors (like TCRT5000 modules)
      • Jumper wires
      • Small black electrical tape for the line

      📐 Wiring the Sensors

      • Connect VCC of each IR sensor to 5V on Arduino.
      • Connect GND to GND.
      • Connect OUT pins to Arduino digital pins (e.g., D2, D3, D4).
    • 💻 Sample Code to Read Sensor Values

      int leftSensor = 2;
      int centerSensor = 3;
      int rightSensor = 4;
      
      void setup() {
        pinMode(leftSensor, INPUT);
        pinMode(centerSensor, INPUT);
        pinMode(rightSensor, INPUT);
        Serial.begin(9600);
      }
      
      void loop() {
        int left = digitalRead(leftSensor);
        int center = digitalRead(centerSensor);
        int right = digitalRead(rightSensor);
      
        Serial.print("Left: ");
        Serial.print(left);
        Serial.print("  Center: ");
        Serial.print(center);
        Serial.print("  Right: ");
        Serial.println(right);
      
        delay(200);
      }
      

      📘 How to Test

      • Upload the code and open the Serial Monitor.
      • Move the sensor over the black line and white background.
      • You should see changes in the printed values (0 or 1).

      🔧 Logic Output

       

      Left Center Right Robot Action
      0 1 0 Go Forward
      1 0 0 Turn Left
      0 0 1 Turn Right