Light-following robot with two LDR sensors on Arduino: design worksheet and code

A ₹1,474 design worksheet for a small robot that steers toward a torch using two light-dependent resistors, with the voltage-divider readings worked out and a dead band to stop wobble.

About the author · Editorial & verification standards

  • Parts₹1,474
  • Bench time3–4 h
  • LevelBeginner
  • ReviewedNov 2026

A ₹1,474 robot that drives toward a torch. Two light-dependent resistors (LDRs) face left and right, the sketch compares their brightness, and the wheel speeds change so the robot turns toward the brighter side. The voltage-divider readings are worked out so you know what to expect on the serial monitor.

Scope: beginner. This is a design worksheet with illustrative costs. It has not been bench-tested by this site. The divider table below was computed by script. Light levels in a room vary a lot, so the thresholds in the sketch are starting values to be tuned. It is a good first robot because the whole control law is one subtraction.

How it works

Each LDR is wired in a voltage divider with a 10 kilohm resistor. With the LDR on the 5 V side, the analog pin sees 5 V × 10k / (10k + LDR). Bright light lowers the LDR resistance, which raises the voltage. For example 1 kilohm gives about 4.55 V (reading near 930), 10 kilohm gives 2.5 V (about 512) and 100 kilohm gives about 0.45 V (about 93). The exact LDR resistances depend on your part and on the light.

The sketch takes the difference between the left and right readings. If it is smaller than a dead band the robot goes straight. If it is larger, the brighter side’s wheel is slowed and the other is sped up, so the robot curves toward the light. If both sides are dark the robot stops.

Bill of materials

PartQtyUnitTotal
Arduino Uno R3 (or compatible)1₹550₹550
LDR (photoresistor)2₹15₹30
10 kilohm resistors2₹2₹4
L298N motor driver board1₹180₹180
2WD chassis kit with two gear motors and wheels1₹450₹450
Battery holder with two 18650 cells1₹210₹210
Jumper wires1₹50₹50
Parts total₹1,474

Scroll the table sideways to see every column.

Prices checked Nov 2026. Shipping and tools are not included.

Prices are illustrative figures for Indian sellers in November 2026, before shipping, and have not been checked against current listings.

The numbers

LDR resistanceAnalog pin voltageReading (0 to 1023)
1 kΩ (bright)4.55 V930
5 kΩ3.33 V682
10 kΩ2.50 V512
50 kΩ0.83 V170
100 kΩ (dim)0.45 V93
Reading = 1023 × 10k / (10k + LDR). The LDR values are examples; real parts differ.

With a dead band of 40 counts, a left reading of 600 against a right reading of 580 counts as equal, and the robot drives straight. A left of 700 against a right of 500 gives a difference of 200 and a steering correction of 100 PWM steps in the sketch below.

Power and wiring

PartConnects toNote
Left LDR dividerA0LDR to 5V, 10k to GND, pin at the middle
Right LDR dividerA1Same arrangement
L298N ENA / IN1 / IN2D5 / D7 / D8Left motor
L298N ENB / IN3 / IN4D6 / D9 / D4Right motor
BatteryL298N 12V input and Uno VINCheck your pack voltage against the board limits
Put a small card barrier between the two LDRs so each sees mostly its own side.

The sketch

// Light-following robot: Uno + 2 LDR dividers + L298N
// Design worksheet: thresholds are starting values.

const int LDR_L = A0, LDR_R = A1;
const int L_EN = 5, L_IN1 = 7, L_IN2 = 8;
const int R_EN = 6, R_IN3 = 9, R_IN4 = 4;

const int BASE = 130;               // forward PWM
const int DEAD = 40;                // counts of difference treated as equal
const int DARK = 150;               // below this on both sides: stop
const float GAIN = 0.5;             // PWM steps per count of difference

void drive(int left, int right) {
  left = constrain(left, 0, 255);
  right = constrain(right, 0, 255);
  digitalWrite(L_IN1, HIGH); digitalWrite(L_IN2, LOW);
  digitalWrite(R_IN3, HIGH); digitalWrite(R_IN4, LOW);
  analogWrite(L_EN, left);
  analogWrite(R_EN, right);
}

void setup() {
  pinMode(L_EN, OUTPUT); pinMode(L_IN1, OUTPUT); pinMode(L_IN2, OUTPUT);
  pinMode(R_EN, OUTPUT); pinMode(R_IN3, OUTPUT); pinMode(R_IN4, OUTPUT);
}

void loop() {
  int l = analogRead(LDR_L);
  int r = analogRead(LDR_R);
  if (l < DARK && r < DARK) { drive(0, 0); delay(50); return; }

  int diff = l - r;                 // positive: light is on the left
  if (abs(diff) < DEAD) {
    drive(BASE, BASE);
  } else {
    int steer = constrain((int)(diff * GAIN), -100, 100);
    drive(BASE - steer, BASE + steer);   // slow the side facing the light
  }
  delay(20);
}

If the robot turns away from the light, swap the two LDR pins or flip the sign of the steering. Matching the two LDRs matters: if one reads higher in the same light, subtract an offset measured with both lit equally. The sketch has been proofread but not compiled on this site.

Test plan

  1. Read the sensors: print both readings and cover each LDR in turn to see which number falls.
  2. Equal light test: shine a torch from straight ahead and check the readings are close; note any offset between them.
  3. Direction test: move the torch left and right and see which wheel slows.
  4. Dead band: raise or lower DEAD until the robot drives straight toward a steady light.
  5. Room light: try it in a dim room and then with the lights on, and note when it stops or ignores the torch.
  6. Following: walk with the torch and see how closely it follows at slow speed.

What usually goes wrong

SymptomLikely causeFix
Turns away from the lightLDR pins or steering sign reversedSwap the pins or flip the steer sign
Weaves from side to sideGain too high or dead band too smallLower GAIN, raise DEAD
Never movesBoth readings below DARKLower DARK or use a brighter torch
Drives past the lightNo stop conditionStop when both readings pass a bright threshold
Ignores the torch in a bright roomRoom light swamps the torchShade the LDRs with short tubes

Upgrades

  • Stop at the light: halt when both readings are above a bright value.
  • Shaded tubes: add tubes in front of each LDR for a narrower view.
  • Light avoider: reverse the steering to run away from light.
  • Light logger: print readings and plot how the beam looks from the robot.

Cost and parts notes

The ₹1,474 total is an illustration and does not include shipping. Use the cost calculator to compare sellers.

Common questions

A light-dependent resistor, a part whose resistance falls as more light hits it.

The Arduino reads voltage, not resistance, so the divider turns the changing resistance into a changing voltage.

Sunlight is very bright and may saturate both sensors, so the robot will not steer. Shading tubes help.

Raise BASE in small steps and retune GAIN.

Last reviewed: November 2026. This is a design worksheet with illustrative costs. It has not been bench-tested by this site; library names, pins and limits must be checked against your board and the current library versions, and measurements from your build are welcome.

More builds

Next builds to try

Free: the final-year project pack

A shortlist with BOMs, a report outline that matches the usual chapter scheme, a costing sheet and 40 viva questions. Built for the February–April submission season.

The Build Note

One short email when something is worth your bench time.

A new build sheet, a part price that moved, or a mistake worth avoiding. No daily drip, no recycled news.

One short email when a build or a price is worth your time. Unsubscribe in one click.