Build it step by step
0 of 6 doneMount the LDRs
Fix two LDRs at the front, left and right, angled outwards by about 30 degrees, with a small card between them so each sees mostly its own side.
Build the dividers
Each LDR from 5V to its analog pin, with 10 kΩ from the pin to GND.
Wire the motors
Follow the wiring table and join all grounds.
Read the sensors
Open the serial monitor and shine a torch at each LDR to see the readings change.
Test steering
Hold the robot and move the torch to the left. The left wheel should slow and the right speed up.
Set it down in a dim room
Put a torch in front and drive it towards the light.
How do you build a light-seeking robot?
Two LDRs on the front make a robot steer towards a torch, like a moth, using the difference between them. You need Arduino Uno R3, LDR Photoresistor × 2, Resistor 10 kΩ × 2, L298N Dual H-Bridge Module, Robot car chassis kit 2WD (acrylic), Battery holder 18650 2S (series), 18650 cell 2600 mAh (branded) × 2, Jumper wires male to male (40 pcs, 20 cm) and Breadboard 400 points (half size). It takes 6 steps and about 75 minutes.
- Level
- Beginner
- Time
- About 75 min
- Parts
- 9
Wiring
| From | To | Why |
|---|---|---|
| Left LDR | Uno 5V and A0, with 10 kΩ from A0 to GND | Left divider, brighter gives a higher reading |
| Right LDR | Uno 5V and A1, with 10 kΩ from A1 to GND | Right divider |
| L298N ENA, IN1, IN2 | Uno D5, D7, D8 | Left motor |
| L298N ENB, IN3, IN4 | Uno D6, D9, D10 | Right motor |
| Battery pack + | L298N 12 V terminal and Uno VIN | Supply from two 18650 cells |
| Battery pack - | L298N GND and Uno GND | Common ground |
Code
Arduino IDE, board "Arduino Uno". No libraries. The thresholds of 900 and 150 depend on your room and LDRs. Read the values on the serial monitor and adjust.
Runs on a virtual Arduino Uno in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
const byte ENA = 5, IN1 = 7, IN2 = 8, ENB = 6, IN3 = 9, IN4 = 10;
const int BASE = 150;
const float GAIN = 0.4;
void drive(int l, int r) {
l = constrain(l, 0, 255); r = constrain(r, 0, 255);
digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH); digitalWrite(IN4, LOW);
analogWrite(ENA, l); analogWrite(ENB, r);
}
void setup() {
for (byte p = 5; p <= 10; p++) pinMode(p, OUTPUT);
Serial.begin(9600);
}
void loop() {
int left = analogRead(A0), right = analogRead(A1);
int diff = left - right; // positive: more light on the left
Serial.print(left); Serial.print(' '); Serial.println(right);
if (left > 900 && right > 900) { // very bright on both: arrived
drive(0, 0);
} else if (left < 150 && right < 150) { // too dark: stay still
drive(0, 0);
} else {
// light on the left: slow the left wheel and speed up the right so it turns left
drive(BASE - GAIN * diff, BASE + GAIN * diff);
}
delay(30);
}What the serial monitor should show
Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual Arduino Uno with its sensors at their default values. Your numbers will follow your real readings, but the format should match.
512 513 512 512 513 512 512 512 511 511 512 511
Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Torch on the left LDR | Left reading rises above the right one |
| Torch on the right | Robot turns right |
| Torch straight ahead | Drives straight |
| Torch very close | Stops when both readings are very bright |
If it does not work
Turns away from the light
Swap the LDR connections or flip the sign of the difference.
Does not move
Both readings are below the dark limit. Lower the threshold or increase the room dimness difference.
Robot wiggles a lot
Reduce GAIN or add shading between the LDRs.
One wheel never turns
Check the L298N enable jumpers and the IN wires.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the light-seeking robot?
Arduino Uno R3, LDR Photoresistor × 2, Resistor 10 kΩ × 2, L298N Dual H-Bridge Module, Robot car chassis kit 2WD (acrylic), Battery holder 18650 2S (series), 18650 cell 2600 mAh (branded) × 2, Jumper wires male to male (40 pcs, 20 cm) and Breadboard 400 points (half size).
How long does the light-seeking robot take to build?
About 75 minutes for a beginner maker, in 6 steps.
How do I know the light-seeking robot works?
Torch on the left LDR: Left reading rises above the right one. Torch on the right: Robot turns right. Torch straight ahead: Drives straight.
Turns away from the light: what should I check?
Swap the LDR connections or flip the sign of the difference.
Does not move: what should I check?
Both readings are below the dark limit. Lower the threshold or increase the room dimness difference.
Robot wiggles a lot: what should I check?
Reduce GAIN or add shading between the LDRs.
One wheel never turns: what should I check?
Check the L298N enable jumpers and the IN wires.
