Building Your First Robot: A Step-by-Step Guide
robotics

January 5, 2026

6 min read

Building Your First Robot: A Step-by-Step Guide

Complete guide to building a simple line-following robot. Learn about motors, sensors, and programming logic for autonomous robots.
Paras Yadav
Paras Yadav

Founder & Lead Instructor at Ishvara Electricals. Passionate about robotics and STEM education.


Introduction

Building a robot is one of the most exciting projects for anyone interested in STEM. In this guide, we'll build a line-following robot that can autonomously follow a black line on a white surface.

This project combines electronics, programming, and mechanical assembly - the three pillars of robotics!

Project Overview

Our robot will use infrared sensors to detect a black line and adjust its motors accordingly to follow the path.

Components Required

ComponentQuantityPurpose
Arduino Uno1Brain of the robot
L298N Motor Driver1Controls motor speed and direction
DC Motors2Drives the wheels
IR Sensors2Detects black line
Wheels2Movement
Caster wheel1Balance and support
Chassis1Robot body
Battery pack (9V)1Power supply
Jumper wiresSeveralConnections

Robot Architecture

Let's understand the complete system architecture:

Assembly Steps

Step 1: Prepare the Chassis

  1. Attach the DC motors to the motor mounts
  2. Connect the wheels to the motor shafts
  3. Install the caster wheel at the front

Step 2: Mount the Electronics

  1. Secure the Arduino on the chassis
  2. Mount the L298N motor driver
  3. Position the IR sensors at the front

Step 3: Wiring

Connect the components according to this pin mapping:

Arduino Pin → Component ──────────────────────────── 5V → IR Sensors VCC GND → IR Sensors GND A0 → Left IR Sensor OUT A1 → Right IR Sensor OUT 6 → L298N ENA (PWM) 7 → L298N IN1 8 → L298N IN2 9 → L298N IN3 10 → L298N IN4 11 → L298N ENB (PWM)

The Control Logic

The robot's decision-making follows this state machine:

Arduino Code

Here's the complete code for the line-following robot:

cpp
// Pin definitions
const int leftSensor = A0;
const int rightSensor = A1;

const int ENA = 6;  // Left motor speed
const int IN1 = 7;  // Left motor direction
const int IN2 = 8;
const int IN3 = 9;  // Right motor direction
const int IN4 = 10;
const int ENB = 11; // Right motor speed

// Threshold for line detection
const int threshold = 500;

// Motor speeds
const int baseSpeed = 150;
const int turnSpeed = 100;

void setup() {
  // Set pin modes
  pinMode(leftSensor, INPUT);
  pinMode(rightSensor, INPUT);

  pinMode(ENA, OUTPUT);
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
  pinMode(ENB, OUTPUT);

  Serial.begin(9600);
}

void loop() {
  // Read sensor values
  int leftValue = analogRead(leftSensor);
  int rightValue = analogRead(rightSensor);

  // Print values for debugging
  Serial.print("Left: ");
  Serial.print(leftValue);
  Serial.print(" Right: ");
  Serial.println(rightValue);

  // Decision logic
  if (leftValue > threshold && rightValue > threshold) {
    // Both sensors on line - move forward
    moveForward();
  }
  else if (leftValue > threshold && rightValue < threshold) {
    // Left sensor on line - turn left
    turnLeft();
  }
  else if (leftValue < threshold && rightValue > threshold) {
    // Right sensor on line - turn right
    turnRight();
  }
  else {
    // No sensor on line - stop
    stopMotors();
  }

  delay(10);
}

void moveForward() {
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  analogWrite(ENA, baseSpeed);
  analogWrite(ENB, baseSpeed);
}

void turnLeft() {
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  analogWrite(ENA, turnSpeed);
  analogWrite(ENB, baseSpeed);
}

void turnRight() {
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
  analogWrite(ENA, baseSpeed);
  analogWrite(ENB, turnSpeed);
}

void stopMotors() {
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
  analogWrite(ENA, 0);
  analogWrite(ENB, 0);
}

Testing and Calibration

Creating a Test Track

  1. Use black electrical tape on white paper or cardboard
  2. Make the line about 2cm wide
  3. Include straight sections and curves
  4. Avoid very sharp turns initially

Calibration Tips

  • Adjust the threshold value based on your sensors
  • Fine-tune baseSpeed and turnSpeed for smooth movement
  • Test on different surfaces and lighting conditions

Troubleshooting

ProblemPossible CauseSolution
Robot doesn't movePower issueCheck battery and connections
Robot moves erraticallySensor calibrationAdjust threshold value
Robot spins in circlesMotor wiringSwap motor connections
Robot loses the lineSpeed too highReduce baseSpeed

Upgrades and Improvements

Once your basic robot works, consider these upgrades:

  1. PID Control - Smoother line following
  2. Bluetooth Control - Remote start/stop
  3. Obstacle Avoidance - Add ultrasonic sensors
  4. Speed Control - Variable speed based on track complexity

Conclusion

Congratulations! You've built your first autonomous robot. This project teaches fundamental concepts of robotics including sensing, control, and actuation.

The skills you've learned here apply to more advanced robotics projects, from maze-solving robots to industrial automation systems.


Want to build more advanced robots? Join our Robotics Foundation course for comprehensive hands-on training!

Related Articles