MODULE 05

Robotics & Automation

Build real robots from scratch

Motors & ServosSensorsLine FollowerRobotic ArmPID Control

Robotics Introduction

A robot is an autonomous or semi-autonomous machine that senses its environment, processes that information, and acts on the world. Every robot needs three things: Sensors (perceive), Controller (think), Actuators (act).

SENSE Ultrasonic IR / Camera Encoders THINK Arduino / ESP32 Process Data Make Decisions ACT DC Motors Servo Motors Grippers FEEDBACK LOOP
πŸ”‘ The Feedback Loop: Great robots don't just act β€” they continuously sense the results of their actions and adjust. This feedback loop is what separates a robot from a simple machine. PID control is the mathematical framework for this.

DC Motors & Motor Drivers

DC motors are the workhorses of wheeled robots. They can't be controlled directly from Arduino pins β€” they need a motor driver IC like the L298N or L293D to handle the high current.

L298N MOTOR DRIVER β€” CONNECTIONS

L298N Motor Driver 2A per channel Arduino IN1 D8 IN2 D9 ENA ~ OUT1 β†’ Motor+ OUT2 β†’ Motor- VCC: 7-12V Battery
// L298N Motor Control #define IN1 8 #define IN2 9 #define ENA 10 // PWM pin for speed void motorForward(int speed) { analogWrite(ENA, speed); // 0-255 digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW); } void motorBackward(int speed) { analogWrite(ENA, speed); digitalWrite(IN1, LOW); digitalWrite(IN2, HIGH); } void motorStop() { analogWrite(ENA, 0); } void setup() { pinMode(IN1,OUTPUT); pinMode(IN2,OUTPUT); pinMode(ENA,OUTPUT); } void loop() { motorForward(200); delay(2000); motorStop(); delay(500); motorBackward(150); delay(2000); motorStop(); delay(500); }

Servo Motors

Servo motors rotate to a precise angle (0°–180Β°) on command β€” perfect for robotic arms, grippers, steering mechanisms, and pan-tilt camera mounts.

0Β° – 180Β°
Rotation Range
3 Wires
VCC Β· GND Β· Signal
PWM
Control Method
// Servo control with Arduino's Servo library #include <Servo.h> Servo myServo; void setup() { myServo.attach(9); // Signal wire to pin 9 } void loop() { // Sweep 0Β° to 180Β° for (int angle = 0; angle <= 180; angle += 5) { myServo.write(angle); delay(15); } // Return to 0Β° for (int angle = 180; angle >= 0; angle -= 5) { myServo.write(angle); delay(15); } } // Control via potentiometer (joystick-like) void loop() { int pot = analogRead(A0); // 0-1023 int angle = map(pot, 0, 1023, 0, 180); // scale to degrees myServo.write(angle); }
πŸ”Œ Wiring: Servo wires are color-coded: Red = 5V, Brown/Black = GND, Orange/Yellow/White = Signal. For multiple servos, use an external 5V supply for VCC β€” Arduino's 5V pin can only supply ~500mA total.

Robot Sensors

Sensors are the robot's eyes and ears. Each sensor type is suited to different tasks β€” choosing the right sensor is half the battle.

ULTRASONIC SENSOR (HC-SR04) β€” DISTANCE

// HC-SR04: measures distance 2cm to 400cm #define TRIG 7 #define ECHO 6 long getDistance() { digitalWrite(TRIG, LOW); delayMicroseconds(2); digitalWrite(TRIG, HIGH); delayMicroseconds(10); digitalWrite(TRIG, LOW); long duration = pulseIn(ECHO, HIGH); return duration * 0.034 / 2; // cm } void setup() { Serial.begin(9600); pinMode(TRIG, OUTPUT); pinMode(ECHO, INPUT); } void loop() { Serial.print("Distance: "); Serial.print(getDistance()); Serial.println(" cm"); delay(200); }

IR SENSOR β€” LINE DETECTION

// IR sensor: detects black/white surface // Output: LOW when detects black line, HIGH on white #define IR_LEFT 4 #define IR_RIGHT 5 void setup() { pinMode(IR_LEFT, INPUT); pinMode(IR_RIGHT, INPUT); } void loop() { bool left = !digitalRead(IR_LEFT); // true = on line bool right = !digitalRead(IR_RIGHT); if (left && right) goForward(); else if (left) turnLeft(); else if (right) turnRight(); else motorStop(); }

πŸš— Line Follower Robot

The classic beginner robot! Uses IR sensors to detect a black line on white paper and follows it autonomously. A rite of passage for every robotics student.

COMPONENTS NEEDED

Arduino Uno L298N Motor Driver 2x DC Motors + Wheels 2x IR Sensor Modules Chassis + Caster wheel 7.4V LiPo Battery
  • 1

    Mount two DC motors onto the chassis. Attach wheels. Add a caster ball at the front for balance.

  • 2

    Mount two IR sensor modules at the front bottom of the chassis, spaced about 3cm apart, facing down.

  • 3

    Connect motors to L298N OUT1/OUT2 and OUT3/OUT4. Connect L298N control pins to Arduino D8,D9,D10 and D5,D6,D7.

  • 4

    Connect IR sensor OUT pins to Arduino D4 (left) and D5 (right). VCC=5V, GND=GND.

  • 5

    Upload the code, place robot on a black-tape track, and watch it follow the line!

⚑ Track Tips: Use black electrical tape on white cardboard. Make curves gradual (radius >10cm). Adjust IR sensor height to 5–8mm above surface. Calibrate sensitivity with the onboard potentiometer on the IR module.

πŸ”Š Obstacle Avoider Robot

A robot that navigates autonomously using an ultrasonic sensor β€” it detects walls and objects, then chooses the clearest path forward.

// Obstacle Avoiding Robot - Core Logic #include <Servo.h> Servo scanServo; // servo rotates ultrasonic sensor int scan(int angle) { scanServo.write(angle); delay(300); return getDistance(); } void loop() { int frontDist = getDistance(); if (frontDist > 25) { goForward(); } else { motorStop(); delay(300); // Scan left and right int leftDist = scan(150); int rightDist = scan(30); scanServo.write(90); // center if (leftDist > rightDist) { turnLeft(); delay(400); } else { turnRight(); delay(400); } motorStop(); } }

🦾 2-DOF Robotic Arm

Build a 2-axis robotic arm with two servo motors. Control it with two potentiometers β€” one for each joint angle. Then upgrade to serial commands!

// 2-DOF Arm: base rotation + shoulder joint #include <Servo.h> Servo baseServo; // pin 9 β€” rotates base Servo shoulderServo; // pin 10 β€” raises/lowers arm void setup() { baseServo.attach(9); shoulderServo.attach(10); Serial.begin(9600); } void loop() { // Control with potentiometers int baseAngle = map(analogRead(A0), 0, 1023, 0, 180); int shoulderAngle = map(analogRead(A1), 0, 1023, 0, 120); baseServo.write(baseAngle); shoulderServo.write(shoulderAngle); Serial.printf("Base: %dΒ° | Shoulder: %dΒ°\n", baseAngle, shoulderAngle); delay(50); }
πŸ”© Building the Arm: Use servo horns screwed to wooden/acrylic pieces. Hobby servos like MG90S (metal gear) handle small loads well. For a gripper, add a third servo at the end (wrist). Design in Tinkercad and 3D print for a professional look!

πŸ“± Bluetooth RC Car

Control a 4-wheel robot car from your phone via Bluetooth! Use the HC-05 module and the "Arduino Bluetooth Controller" app.

// Bluetooth RC Car β€” HC-05 on pins 0,1 (Serial) void setup() { Serial.begin(9600); // Motor pin setup... } void loop() { if (Serial.available()) { char cmd = Serial.read(); switch (cmd) { case 'F': goForward(); break; case 'B': goBackward(); break; case 'L': turnLeft(); break; case 'R': turnRight(); break; case 'S': motorStop(); break; default: motorStop(); } } }
πŸ“± App Setup: Download "Bluetooth RC Controller" or "Arduino Bluetooth Control" from Play Store. Connect to HC-05 (password: 1234). Map buttons to send F/B/L/R/S characters β€” the car responds instantly!

PID Control

PID (Proportional-Integral-Derivative) is the most widely used feedback control algorithm in engineering. It corrects errors automatically β€” used in everything from drones to industrial ovens.

THE THREE TERMS EXPLAINED

P Proportional Kp Γ— error React NOW to error Bigger Kp = faster I Integral Ki Γ— Ξ£error Fix steady-state drift over time D Derivative Kd Γ— Ξ”error Predict & dampen overshoot
// PID for Line Following Robot float Kp=25, Ki=0.1, Kd=15; float lastError=0, integral=0; void loop() { int leftVal = analogRead(A0); // left IR sensor int rightVal = analogRead(A1); // right IR sensor float error = leftVal - rightVal; // 0 = on track integral += error; float derivative = error - lastError; float correction = Kp*error + Ki*integral + Kd*derivative; lastError = error; int leftSpeed = constrain(200 - correction, 0, 255); int rightSpeed = constrain(200 + correction, 0, 255); // Write speeds to motors... }

Robot Kinematics

Kinematics is the mathematics of robot motion β€” how joint angles relate to the position of the robot's end-effector (hand/gripper). Essential for robotic arm programming.

FORWARD KINEMATICS β€” 2-LINK PLANAR ARM

// Given joint angles β†’ find end-effector position // For a 2-link arm with link lengths L1, L2: #include <math.h> float L1 = 15.0; // cm, upper arm length float L2 = 12.0; // cm, forearm length void forwardKinematics(float theta1, float theta2) { // Convert degrees to radians float t1 = theta1 * PI / 180.0; float t2 = theta2 * PI / 180.0; // End-effector position float x = L1 * cos(t1) + L2 * cos(t1 + t2); float y = L1 * sin(t1) + L2 * sin(t1 + t2); Serial.print("X: "); Serial.print(x); Serial.print(" cm | Y: "); Serial.println(y); }