You are currently viewing Building a Line Following Robot with KY-033 Sensors for Beginners
This is the 3D-rendered design of the Arduino path-following robot equipped with KY-033 sensors for accurate line tracking.

Building a Line Following Robot with KY-033 Sensors for Beginners

In this step-by-step tutorial, we’ll design, 3D print, assemble and program a line-following robot using the KY-033 IR sensors. This robot can autonomously follow a predefined white line on a black surface or a black line on a white surface, making it an excellent hands-on project for learning robotics, sensor integration, and Arduino programming.

A 3D-rendered line-following robot designed with an orange and blue chassis, featuring OmArTronics branding, two wheels, and a support caster wheel, suitable for DIY robotics projects.

Focusing on the KY-033 sensor setup, this project introduces essential skills in 3D design, assembly, and control programming. By the end, you’ll have a functional line-following robot that’s both engaging to build and a great introduction to robotics!

In the next tutorial, we’ll enhance precision and stability with the TCS34725 RGB-Sensor and PID control for advanced line-following.

Components Needed:

3D-Printed Parts

  • Chassis: Houses the motors, sensors, and Arduino board.
  • Sensor Holder: Aligns the sensors for precise line detection.
  • Arduino Holder: Mounts the Arduino securely to the chassis.

Motors and Wheels

  • 2 DC Motors with Gearboxes: Drive the robot.
  • 2 Wheels: Provide mobility.
  • 1 Support Wheel: Balances the robot for smooth movement.

Electronics

  • Arduino Uno: Microcontroller for programming and control.
  • Arduino Motor Shield Rev3: Drives the motors and distributes power.
  • KY-033 Line-Tracking Sensors: Detect the black line using IR.

Accessories

  • Jumper Wires: For connecting components.
  • Screws and Inserts: To securely mount parts.
  • Battery (e.g., 7.4V LiPo): Powers the robot.
  • Power Switch: Optional, for easy on/off control.

Step 1: Designing the Line-Following Robot with KY-033 Sensors in Autodesk Inventor

We started by designing the robot in Autodesk Inventor. The design ensures all components fit securely while maintaining a lightweight and compact structure. Key features include:

  1. Chassis: A sturdy base for mounting the motors, sensors, and Arduino.
  2. Sensor Holder: Holds the KY-033 sensor for precise line detection.
  3. Arduino Holder: Provides easy access to the Arduino for programming and wiring.

You can download the STL files for these 3D-printed parts from Cults3D.

Step 2: 3D Printing the Components

After finalizing the design, we used a Creality Ender 3 printer to 3D print the components. Materials like PLA or ABS are recommended for durability. Ensure the parts are cleaned and prepared before assembly.

Step 3: Assembling the Line-Following Robot with KY-033 Sensors

1. Mounting the Motors and Wheels

  • Secure the motors to the chassis using screws and inserts.
  • Attach the wheels to the motor shafts and ensure smooth rotation.
  • Mount the support wheel at the rear for balance.

2. Installing the Electronics

  • Mount the Arduino Uno and Motor Shield to the chassis.
  • Attach the KY-033 sensor to the sensor holder.
  • Connect the Battery and secure all components.

3. Wiring the Components

  • Wire the motors to the Motor Shield.
  • Connect the sensors to the Arduino pins.
  • Ensure the battery is safely connected and add a switch for convenience.
Wiring diagram for a line-following robot using Arduino Uno, motor shield, DC motors, sensors, and a 9V battery

Step 4: Programming the Line-Following Robot with KY-033 Sensors

The KY-033 sensors are used to detect the white line on a black surface using IR light. The logic behind the approach is straightforward:

  • Both sensors on the line: The robot moves straight.
  • Left sensor on the line: The robot turns left to adjust back to the path.
  • Right sensor on the line: The robot turns right to adjust back to the path.
  • Both sensors off the line: The robot rotates in place to search for the path.

Here’s the complete code for the KY-033 sensors approach:

Flowchart for the decision-making logic of a line-following robot with KY-033 sensors, controlling the robot's movements based on sensor inputs.
/**
 * Author: Omar Draidrya
 * Date: 2024/12/06
 * Line-Following Robot with KY-033.
 */

// Motor control pins
#define DIR_L 12  // Left motor direction
#define DIR_R 13  // Right motor direction
#define PWM_L 3   // Left motor speed (PWM)
#define PWM_R 11  // Right motor speed (PWM)

// Sensor pins
int sensorLeft = 2;    // Left sensor
int sensorRight = 4;   // Right sensor

void setup() {
  // Configure motor pins as outputs
  pinMode(DIR_L, OUTPUT);
  pinMode(DIR_R, OUTPUT);
  pinMode(PWM_L, OUTPUT);
  pinMode(PWM_R, OUTPUT);

  // Configure sensor pins as inputs
  pinMode(sensorLeft, INPUT);
  pinMode(sensorRight, INPUT);

  // Serial communication for debugging
  Serial.begin(9600);
}

void loop() {
  // Read sensor values
  int left = digitalRead(sensorLeft);
  int right = digitalRead(sensorRight);

  // Output sensor data (debugging)
  Serial.print("Sensor Left: ");
  Serial.print(left);
  Serial.print("  |  Sensor Right: ");
  Serial.println(right);

  // Control logic
  if (left == 1 && right == 1) {
    // Drive straight
    controlMotors(HIGH, HIGH, 105, 105);
  } else if (left == 1 && right == 0) {
    // Turn left
    controlMotors(HIGH, LOW, 105, 85);
  } else if (left == 0 && right == 1) {
    // Turn right
    controlMotors(LOW, HIGH, 85, 105);
  } else {
    // Line lost – stop motors
    // Rotate in place to find the line
    controlMotors(HIGH, LOW, 70, 70);  // Left motor forward, right motor backward
    // delay(500);  // Brief rotation
    // controlMotors(LOW, LOW, 0, 0);
  }
}

// Function to control motors
void controlMotors(int directionL, int directionR, int speedL, int speedR) {
  digitalWrite(DIR_L, directionL);
  digitalWrite(DIR_R, directionR);
  analogWrite(PWM_L, speedL);
  analogWrite(PWM_R, speedR);
}

    

Here’s a detailed breakdown of the code, explaining each section to help you understand how the KY-033 sensor-based line-following robot operates.

1. Motor Control Pins

// Motor control pins
#define DIR_L 12  // Left motor direction
#define DIR_R 13  // Right motor direction
#define PWM_L 3   // Left motor speed (PWM)
#define PWM_R 11  // Right motor speed (PWM)
    
  • These pins are used to control the direction and speed of the motors.
    • DIR_A and DIR_B determine the direction of the motors.
    • PWM_A and PWM_B control the speed using Pulse Width Modulation (PWM).

2. Sensor Pins

int sensorLeft = 2;    // Left sensor
int sensorRight = 4;   // Right sensor
    
  • The KY-033 sensors are connected to digital pins 2 and 4 on the Arduino.
  • These sensors detect the line by reading either HIGH (1) or LOW (0).

3. setup() Function

void setup() {
  // Configure motor pins as outputs
  pinMode(DIR_L, OUTPUT);
  pinMode(DIR_R, OUTPUT);
  pinMode(PWM_L, OUTPUT);
  pinMode(PWM_R, OUTPUT);

  // Configure sensor pins as inputs
  pinMode(sensorLeft, INPUT);
  pinMode(sensorRight, INPUT);

  // Serial communication for debugging
  Serial.begin(9600);
}
    
  • Configures the motor control pins as outputs and the sensor pins as inputs.
  • Initializes serial communication for debugging purposes.

4. loop() Function

int left = digitalRead(sensorLeft);
int right = digitalRead(sensorRight);
    
  • Reads the values from the KY-033 sensors.
    • 1 indicates the sensor detects a line (black surface).
    • 0 indicates the sensor does not detect a line (white surface).

5. Debugging Output

Serial.print("Sensor Left: ");
Serial.print(left);
Serial.print("  |  Sensor Right: ");
Serial.println(right);
    
  • Prints the sensor readings to the serial monitor for real-time debugging.

6. Control Logic

if (left == 1 && right == 1) {
  controlMotors(HIGH, HIGH, 105, 105);
} else if (left == 1 && right == 0) {
  controlMotors(HIGH, LOW, 105, 85);
} else if (left == 0 && right == 1) {
  controlMotors(LOW, HIGH, 85, 105);
} else {
  controlMotors(HIGH, LOW, 70, 70);
}

    
  • This block controls the robot’s movement based on sensor readings:
    • Both sensors detect the line (1, 1): Move straight.
    • Left sensor detects the line, right sensor does not (1, 0): Turn left.
    • Right sensor detects the line, left sensor does not (0, 1): Turn right.
    • Both sensors do not detect the line (0, 0): Rotate to search for the line.

7. Motor Control Function

// Function to control motors
void controlMotors(int directionL, int directionR, int speedL, int speedR) {
  digitalWrite(DIR_L, directionL);
  digitalWrite(DIR_R, directionR);
  analogWrite(PWM_L, speedL);
  analogWrite(PWM_R, speedR);
}
    
  • This function sets the motor direction and speed using the defined pins.

This implementation ensures accurate line-following behavior using KY-033 sensors.

Step 5: Testing the Line-Following Robot

To test the functionality of the robot, a 50mm wide white adhesive tape was placed on a dark surface, forming a curvy line. This setup simulates a typical track for a line-following robot. The testing process was conducted using two different sensor configurations:

  • The robot was started directly on the white line with both KY-033 sensors positioned over the tape.
  • The robot’s behavior was monitored as it followed the curvy track.
  • Adjustments to the code (e.g., motor speed or delay) were made as necessary to ensure smooth and accurate line tracking.

Conclusion

This guide has covered how to build a line-following robot with KY-033 IR sensors, focusing on 3D design, assembly, and programming for basic path tracking. While this setup works well, it has limitations in terms of precision and smoothness. In the next part, we’ll explore the TCS34725 RGB sensor and PID controller to achieve more accurate and stable line tracking.

omartronics

Welcome to OmArTronics, the hub for technology enthusiasts and creative minds! I'm Omar, the founder of this website and YouTube channel, and a passionate engineer with a background in electrical and mechanical engineering. I'm currently pursuing my master's in mechatronics in Germany.

Leave a Reply