Arduino First – Light Seeking ‘Robot’

To get me started, both on this blog and with my brand new Arduino Uno, I’ve built this Light Seeking Robot and I have documented it here for your entertainment.

What it does:

It scans the 180 degrees (a half circle) to measure the amount of light from each direction. It then points in the direction from which the most light comes. After three seconds it repeats these steps.

What it doesn’t do:

It doesn’t walk around. It just points. I hope to build (and show you) a moving/walking/driving one later.

How it works:

It uses a light sensor (LDR) and a servo to seek the direction from which the most light is coming. It moves the servo from 0 to 180 degrees in steps of 10 degrees, looking a bit like a radar. It reads a value from an analog port to measure the amount of light on the sensor. In each iteration it compares the value to the previous value measured and stores the highest value along with its direction.

After the loop, it points the servo to the direction found.

YouTube video

It’s also my first attempt to draw a circuit, let alone in Fritzing. First the wiring, as it is seen in the video. The Servo is connected on 5V, GND and digital pin 9. The LDR is on 5V as well, and is connected analog pin 0 using a 10kΩ resistor (Volt divider).

Light Seeker breadboard wiring
The wiring as in the video
Light Seeker schema
The Light Seeker circuit

The source code is written in Arduino, which is a reasonably convenient IDE in which you write the C-like Arduino scripts.

#include <Servo.h>

Servo myservo;

void setup()
  myservo.attach(9); // attaches the servo on pin 9 to the servo object

void loop()
  int maxval = -1;
  int maxstep = 0;

  // Move the servo to start,
  // and wait a while until it reaches that point.

  // Scan the full range of the servo in steps of 10 degrees.
  for (int i = 0; i < 180; i += 10) {
    // Jump to that position and wait.

    // Read analog pin 0 to get the value of the LDR.
    int val = analogRead(0);

    // If the found value is higher (more light) than the previous reading,
    // store both the value and the angle.
    if (val > maxval)
      maxval = val;
      maxstep = i;

  // Point the servo to the position found, and wait a couple of seconds before repeating.

Steps to take from here:
An improved version would move a second motor to the found spot, and would let the ‘seeker’ motor scan continuously, back and forth.

With two LDRs, you could just measure the difference between the two and adjust direction according to that difference. Unfortunately, the starter kit I bought only had one LDR in it, so I had to improvise.

Thank you for reading my first post. I hope to be back soon with an improved version of the Light Seeker.

3 thoughts on “Arduino First – Light Seeking ‘Robot’

  1. Hi I am trying to use the code you have posted above but gives the error ‘Val’ not declared in the scope. do you have any idea why this may be? thanks

    1. You’re right. The `int` keyword was missing in the line `int val = analogRead(0);`. I hope that was the only error I made when I modified this code for my blog. Let me know if anything else doesn’t work.

  2. can you help me out controlling dual axis servo with LDR ? I’m stuck up with ; at some angle my servo rotates fully & at some angle it responds well!

Leave a Reply

Your email address will not be published. Required fields are marked *