
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
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
| Component | Quantity | Purpose |
|---|---|---|
| Arduino Uno | 1 | Brain of the robot |
| L298N Motor Driver | 1 | Controls motor speed and direction |
| DC Motors | 2 | Drives the wheels |
| IR Sensors | 2 | Detects black line |
| Wheels | 2 | Movement |
| Caster wheel | 1 | Balance and support |
| Chassis | 1 | Robot body |
| Battery pack (9V) | 1 | Power supply |
| Jumper wires | Several | Connections |
Robot Architecture
Let's understand the complete system architecture:
Assembly Steps
Step 1: Prepare the Chassis
- Attach the DC motors to the motor mounts
- Connect the wheels to the motor shafts
- Install the caster wheel at the front
Step 2: Mount the Electronics
- Secure the Arduino on the chassis
- Mount the L298N motor driver
- 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:
// 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
- Use black electrical tape on white paper or cardboard
- Make the line about 2cm wide
- Include straight sections and curves
- Avoid very sharp turns initially
Calibration Tips
- Adjust the
thresholdvalue based on your sensors - Fine-tune
baseSpeedandturnSpeedfor smooth movement - Test on different surfaces and lighting conditions
Troubleshooting
| Problem | Possible Cause | Solution |
|---|---|---|
| Robot doesn't move | Power issue | Check battery and connections |
| Robot moves erratically | Sensor calibration | Adjust threshold value |
| Robot spins in circles | Motor wiring | Swap motor connections |
| Robot loses the line | Speed too high | Reduce baseSpeed |
Upgrades and Improvements
Once your basic robot works, consider these upgrades:
- PID Control - Smoother line following
- Bluetooth Control - Remote start/stop
- Obstacle Avoidance - Add ultrasonic sensors
- 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!

