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).
π 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.
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;
voidsetup() {
myServo.attach(9); // Signal wire to pin 9
}
voidloop() {
// 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)voidloop() {
int pot = analogRead(A0); // 0-1023int 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.
// IR sensor: detects black/white surface
// Output: LOW when detects black line, HIGH on white#define IR_LEFT 4
#define IR_RIGHT 5
voidsetup() {
pinMode(IR_LEFT, INPUT); pinMode(IR_RIGHT, INPUT);
}
voidloop() {
bool left = !digitalRead(IR_LEFT); // true = on linebool right = !digitalRead(IR_RIGHT);
if (left && right) goForward();
else if (left) turnLeft();
else if (right) turnRight();
elsemotorStop();
}
π 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 UnoL298N Motor Driver2x DC Motors + Wheels2x IR Sensor ModulesChassis + Caster wheel7.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.
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)voidsetup() {
Serial.begin(9600);
// Motor pin setup...
}
voidloop() {
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
// PID for Line Following Robotfloat Kp=25, Ki=0.1, Kd=15;
float lastError=0, integral=0;
voidloop() {
int leftVal = analogRead(A0); // left IR sensorint rightVal = analogRead(A1); // right IR sensorfloat 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 lengthfloat L2 = 12.0; // cm, forearm lengthvoidforwardKinematics(float theta1, float theta2) {
// Convert degrees to radiansfloat t1 = theta1 * PI / 180.0;
float t2 = theta2 * PI / 180.0;
// End-effector positionfloat 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);
}