Section outline

  •  

    Now that your line follower robot is physically assembled, it's time to program it so it can follow a line intelligently. This section covers sensor logic, writing your first complete code, uploading it, and step-by-step testing and debugging.

    • 🔍 How the Robot Makes Decisions

      Line follower robots use sensors to detect dark and light surfaces. Typically, black tape on a white background is used. The logic is:

      • IR sensor gives LOW when over black (absorbs IR)
      • IR sensor gives HIGH when over white (reflects IR)

      With two sensors (left and right), we can determine direction:

      • Left = 0, Right = 0 → On track (go straight)
      • Left = 0, Right = 1 → Off to right (turn left)
      • Left = 1, Right = 0 → Off to left (turn right)
      • Left = 1, Right = 1 → Off track (stop or reverse)

       

    • 💡 Sample Arduino Code for Line Following

      This sample sketch helps your robot follow a black line using two IR sensors and an L298N motor driver.

      
      // Pin configuration
      int leftSensor = A0;
      int rightSensor = A1;
      
      int motorLeft1 = 9;
      int motorLeft2 = 8;
      int motorRight1 = 7;
      int motorRight2 = 6;
      
      void setup() {
        pinMode(leftSensor, INPUT);
        pinMode(rightSensor, INPUT);
        
        pinMode(motorLeft1, OUTPUT);
        pinMode(motorLeft2, OUTPUT);
        pinMode(motorRight1, OUTPUT);
        pinMode(motorRight2, OUTPUT);
      }
      
      void loop() {
        int left = digitalRead(leftSensor);
        int right = digitalRead(rightSensor);
      
        if (left == 0 && right == 0) {
          // Move forward
          forward();
        } else if (left == 0 && right == 1) {
          // Turn left
          turnLeft();
        } else if (left == 1 && right == 0) {
          // Turn right
          turnRight();
        } else {
          // Stop
          stopMotors();
        }
      }
      
      void forward() {
        digitalWrite(motorLeft1, HIGH);
        digitalWrite(motorLeft2, LOW);
        digitalWrite(motorRight1, HIGH);
        digitalWrite(motorRight2, LOW);
      }
      
      void turnLeft() {
        digitalWrite(motorLeft1, LOW);
        digitalWrite(motorLeft2, HIGH);
        digitalWrite(motorRight1, HIGH);
        digitalWrite(motorRight2, LOW);
      }
      
      void turnRight() {
        digitalWrite(motorLeft1, HIGH);
        digitalWrite(motorLeft2, LOW);
        digitalWrite(motorRight1, LOW);
        digitalWrite(motorRight2, HIGH);
      }
      
      void stopMotors() {
        digitalWrite(motorLeft1, LOW);
        digitalWrite(motorLeft2, LOW);
        digitalWrite(motorRight1, LOW);
        digitalWrite(motorRight2, LOW);
      }
      

      🔌 Uploading the Code

      1. Connect Arduino to your computer using a USB cable.
      2. Open the Arduino IDE and paste the code above.
      3. Select the correct board and port under the Tools menu.
      4. Click the Upload button (arrow icon).
      5. Wait for "Done Uploading" message at the bottom.
    • 🧪 Testing the Robot

      1. Place the robot on a black line path (preferably black tape on white chart paper).
      2. Power on the robot (use batteries, not USB).
      3. Observe how it responds:
        • If it moves forward on line, it’s working!
        • If it turns wrongly, check sensor connections and readings.

      🐞 Debugging Tips

      • Use the Serial Monitor to print sensor readings and debug.
      • Check sensor orientation – sometimes it may need flipping.
      • Double-check motor driver wiring; wrong pin order may reverse logic.
      • Use fresh batteries – weak power leads to inconsistent behavior.

      ✅ What You’ve Achieved

       

      By the end of this section, you've written your first logic-based Arduino program, uploaded it to your robot, tested sensor responses, and debugged real-world issues. This is a major step in robotics programming!