MyRoboPathOpen Robotics Lab
robotics basics17 min readUpdated 2026-03-14Beginner

Sensing for Beginners: Ultrasonic, IR Obstacle, Line Tracking, Touch, Tilt & Light

Give your robot senses: comprehensive guide to working principles, pinouts, wiring diagrams, and Arduino C++ code for Ultrasonic, IR obstacle, Line reflectance, Touch, Tilt, and Light sensors.

MyRoboPath Engineering Lab
Peer-Reviewed Open-Source Hardware & Firmware Guide

Key Engineering Takeaways

  • The HC-SR04 Ultrasonic sensor calculates distance using speed of sound in air (343 m/s) with formula: Distance = (Time × 0.0343) / 2.
  • Active IR obstacle sensors use modulated infrared LEDs and phototransistors with an onboard LM393 potentiometer comparator for binary obstacle detection.
  • Line tracking sensors (TCRT5000) detect high infrared absorption on black electrical tape versus high reflection on white surfaces.
  • Capacitive touch and ball tilt switches provide instant bounce-free digital triggers for robot collision bumpers and tip-over safety shutoffs.
  • Always average multiple sensor readings (running average or median filter) to prevent false positives caused by acoustic noise or sunlight glare.
Prerequisites
  • Basic Arduino sketch upload experience
Required Hardware / Tools
  • HC-SR04 Ultrasonic Sensor
  • IR Obstacle Sensor Module
  • TCRT5000 Line Tracker
  • LDR Light Sensor + 10kΩ Resistor
  • Breadboard & Jumpers

Beginner Robotics Sensor Summary & Selection

Sensors allow your robot to perceive the environment and make informed navigation choices:

Sensor ModulePhysical PrincipleOutput TypeSensing RangePrimary Robotics Application
HC-SR04 Ultrasonic40 kHz Sound Echo TimingDigital Pulse Width2 cm – 400 cmLong-range forward collision avoidance
IR Obstacle ModuleInfrared Beam ReflectionDigital (HIGH/LOW)2 cm – 30 cmClose-proximity bumper detection
TCRT5000 Line SensorSurface Infrared ContrastAnalog & Digital1 mm – 15 mmHigh-speed line following on tracks
TTP223 TouchCapacitive Field ChangeDigital (HIGH/LOW)0 mm (Touch)User touch buttons, soft bumper triggers
SW-520D Tilt BallGravity Gold Ball ContactDigital (HIGH/LOW)Tilt > 15°Tip-over detection, anti-flip safety
LDR PhotoresistorLight-sensitive semiconductorAnalog (0 - 5V)Ambient LuxLight seeker / Shadow avoider rovers
Robotics sensor pinouts and operating principles diagram
Figure 6.1: Pinouts, wiring schematics, and operating principles for the 6 primary beginner robotics sensors.Visual Guide

1. HC-SR04 Ultrasonic Distance Sensor

How It Works:

  1. 1
    The microcontroller sends a 10-microsecond HIGH pulse to the TRIG pin.
  2. 2
    The sensor emits eight 40 kHz sonic bursts.
  3. 3
    The ECHO pin goes HIGH and stays HIGH until the sound reflects back off an object.
  4. 4
    Formula: Distance (cm) = (Echo Duration in microseconds * 0.0343) / 2
firmware.ino
cpp
const int TRIG_PIN = 11;
const int ECHO_PIN = 12;

void setup() {
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  Serial.begin(115200);
}

float getDistanceCm() {
  // Clear trigger
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  
  // Emit 10us pulse
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  
  // Measure echo return time (timeout 30000us = ~5 meters)
  long duration = pulseIn(ECHO_PIN, HIGH, 30000);
  if (duration == 0) return 999.0; // No echo detected (clear path)
  
  return (duration * 0.0343) / 2.0;
}

void loop() {
  float dist = getDistanceCm();
  Serial.print("Front Distance: ");
  Serial.print(dist);
  Serial.println(" cm");
  delay(100);
}

2. Active Infrared (IR) Obstacle Sensor

How It Works:

An IR emitter LED shoots invisible 950nm light. If an obstacle is in front, light reflects into the IR receiver phototransistor. An onboard potentiometer adjusts sensitivity threshold:

  • No Obstacle: Output pin is HIGH (5V).
  • Obstacle Detected: Output pin drops to LOW (0V) and onboard indicator LED turns ON.
firmware.ino
cpp
const int IR_OBSTACLE_PIN = 4;

void setup() {
  pinMode(IR_OBSTACLE_PIN, INPUT);
  Serial.begin(115200);
}

void loop() {
  if (digitalRead(IR_OBSTACLE_PIN) == LOW) {
    Serial.println("WARNING: Obstacle detected within 10cm!");
  }
  delay(50);
}

3. TCRT5000 Line & Surface Reflectance Sensor

Line Tracking Mechanics:

  • White Background: Reflects strong IR light → Analog reading is LOW (< 200).
  • Black Line: Absorbs IR light → Analog reading is HIGH (> 800).
firmware.ino
cpp
const int LEFT_LINE_PIN = A1;
const int RIGHT_LINE_PIN = A2;
const int THRESHOLD = 500; // Calibrated midpoint value

void loop() {
  int leftVal = analogRead(LEFT_LINE_PIN);
  int rightVal = analogRead(RIGHT_LINE_PIN);

  if (leftVal > THRESHOLD && rightVal > THRESHOLD) {
    // Both on line -> Forward
  } else if (leftVal > THRESHOLD && rightVal <= THRESHOLD) {
    // Left on line, Right off -> Steer Left
  } else if (leftVal <= THRESHOLD && rightVal > THRESHOLD) {
    // Right on line, Left off -> Steer Right
  }
}

4. Capacitive Touch, SW-520D Tilt & LDR Light Sensors

Tilt / Tip-Over Safety Switch:

The SW-520D contains a tiny gold ball that closes internal contacts when upright. If the robot flips on its side or tips past 45°, the circuit opens immediately, triggering an emergency motor kill:

firmware.ino
cpp
const int TILT_PIN = 3;

void setup() {
  pinMode(TILT_PIN, INPUT_PULLUP);
}

void loop() {
  if (digitalRead(TILT_PIN) == HIGH) {
    // Robot has tilted/flipped over!
    killAllMotors();
    Serial.println("EMERGENCY: Robot inverted!");
  }
}

Noise Filtering & Calibration Best Practices

Real-world sensor data is inherently noisy. Use this 3-sample median filter to eliminate false spikes:

firmware.ino
cpp
float getFilteredDistance() {
  float a = getDistanceCm();
  delay(10);
  float b = getDistanceCm();
  delay(10);
  float c = getDistanceCm();
  
  // Return median value of 3 samples
  if ((a <= b && b <= c) || (c <= b && b <= a)) return b;
  if ((b <= a && a <= c) || (c <= a && a <= b)) return a;
  return c;
}

Frequently Asked Questions

Why does my ultrasonic sensor fail against soft fabric or curtains?

Ultrasonic sound waves are absorbed by soft, fluffy materials like acoustic foam, curtains, and pet fur instead of bouncing back. In addition, angled smooth surfaces (like a wall at 45 degrees) reflect the sound away from the sensor like a mirror. Combine ultrasonic with close-range IR sensors for robust obstacle coverage.

How do I calibrate my line sensors under different room lighting?

Ambient sunlight contains intense infrared radiation that shifts sensor baselines. Always write a 3-second startup calibration routine during setup() that sweeps the robot across the track while recording minimum and maximum analog values, calculating an adaptive dynamic threshold.

Tags:#Ultrasonic HC-SR04#IR Sensor#Line Tracking#TCRT5000#Touch Sensor#Tilt Switch#LDR