30% off your first 3 months

Get the deal

Build a light-seeking robot with two light sensors

by DuckyHelperNov 12, 20265 min read
Robotics

TL;DR:

  • Each light sensor controls the speed of one motor, so brighter light on one side makes the wheels turn differently.

  • Crossed wiring (left sensor to right motor) turns the robot toward the light. Uncrossed turns it away.

  • The behavior comes from the wiring, with almost no program logic.

A robot that chases a flashlight looks smart, but it can be built with no decisions at all. The trick is in how the sensors connect to the motors. This build is a good way to see how a simple link between sensing and moving can look like a goal.

What you will build

You will make a two-wheel robot with a light sensor at the front left and another at the front right. The more light a sensor sees, the faster its connected motor spins. Put a lamp in a dim room and the robot turns to face it and drives toward it.

Parts and tools

  • A two-wheel chassis with a caster, two motors, a motor driver, a microcontroller board and a battery pack.

  • Two light-dependent resistors (LDRs) or two small light sensor modules.

  • Two fixed resistors for a voltage divider if you use bare LDRs; use the value your sensor guide suggests.

  • A desk lamp or flashlight, and a dim room.

  • Card or heat-shrink tubing to shade each sensor so it looks forward, not sideways.

Wiring and assembly

Mount the sensors at the front, angled outward a little, so each one sees mostly its own side. A small card divider between them helps a lot. A bare LDR changes its resistance with light, so put it in a voltage divider with a fixed resistor and read the middle point with an analog input.

  1. Build the two dividers: Wire each LDR in series with its resistor between power and ground. Connect the junction to an analog input pin, one for the left sensor and one for the right.

  2. Check the readings: Print both values to the serial monitor. Cover each sensor in turn. Which direction the number moves depends on how you wired the divider, so note it.

  3. Connect the motors: Wire the driver as in any two-wheel robot. Test that both wheels drive forward.

Crossed versus uncrossed wiring

There are two ways to link sensors to motors, and they behave very differently. These small machines are often called Braitenberg vehicles, after the scientist who described them in a thought experiment.

Wiring

Left sensor drives

Light on the left

Result

Uncrossed

Left motor

Left motor speeds up

Robot turns right, away from light

Crossed

Right motor

Right motor speeds up

Robot turns left, toward light

A differential-drive robot turns toward the side with the slower wheel.

Check the crossed case. Light on the left makes the left sensor read higher, which speeds up the right motor. The right wheel pushes harder than the left, so the robot swings to the left, and now faces the lamp. When the lamp is straight ahead both sensors match, both wheels run at the same speed, and the robot drives straight at it.

The code, step by step

Scale each analog reading (0 to 1023 on many boards) down to a motor speed (0 to 255) by dividing by 4. In the example below, the lamp is on the left, the left sensor reads 800 and the right reads 400.

const int LEFT_SENSOR = A0;
const int RIGHT_SENSOR = A1;

void setMotors(int left, int right) {
  // direction and PWM for your driver here
}

void loop() {
  int l = analogRead(LEFT_SENSOR);    // 0 to 1023, brighter = bigger
  int r = analogRead(RIGHT_SENSOR);

  int leftSpeed  = r / 4;   // crossed: right sensor drives left motor
  int rightSpeed = l / 4;   // crossed: left sensor drives right motor

  setMotors(leftSpeed, rightSpeed);
}

With the example readings, the right motor gets 800 / 4 = 200 and the left motor gets 400 / 4 = 100. The right wheel is twice as fast, so the robot turns left, toward the lamp. As it turns, the two readings come closer, and the turn eases off by itself.

Test, measure and fix

  • Turns away from the lamp: you have the wiring effect reversed. Swap the two lines that assign leftSpeed and rightSpeed, or check whether bright light makes your reading go up or down.

  • Does not move in dim light: a reading near zero gives almost no motor power, and motors need a minimum to start. Add a minimum speed such as 60 when the value is above a small cutoff.

  • Confused by room light: shade the sensors and darken the room, or subtract the average ambient reading.

Make it better

Ambient light is the main enemy of this build. A sensor in a bright room reads high from every direction, so the difference between left and right, which is what steers the robot, becomes tiny. Subtracting each sensor's reading taken at start-up gives you a cleaner signal.

It also helps to write down what you expect before each test. If you shine the lamp on the left sensor only, predict which wheel speeds up and which way the robot turns, then check. Matching your prediction to what happens is the fastest way to catch a swapped wire.

Add a minimum speed, a stop when both readings are very high (it has arrived), and a third sensor at the back so it can find the lamp when it starts facing away. You can also test the opposite behavior on purpose: uncrossed wiring makes a robot that hides in the dark.

Stay safe

Lamps and batteries: Use a normal desk lamp or flashlight, not mains-powered wiring you built yourself, and keep the lamp out of the robot's path so it cannot be knocked over. Use only low-voltage battery packs made for your kit.

Frequently asked questions

Is this really intelligent?

No. It has no plan or memory. Its behavior comes entirely from how sensors connect to motors, which is why it is such a clean example of reactive control.

Why do I need a divider between the sensors?

Without one, each sensor sees light from both sides and the difference between left and right, the thing the robot steers by, gets small.

Get started

DuckyHelper can
do more

It sees your screen and works in any app you use. Try it for 10 minutes, no card.

Get it for Mac, Windows or Linux

Share

Questions and jobs

Do anything

Ask it a question or hand it a job, in any app.

Doing jobs for you is in early access on Mac. Asking works on Mac, Windows and Linux.