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.

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,
  myservo.write(0);
  // and wait a while until it reaches that point.
  delay(200);

  // 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.
    myservo.write(i);
    delay(50);

    // 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.
 myservo.write(maxstep);
 delay(3000);
}

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.

Advertisements
This entry was posted in Arduino and tagged , , , . Bookmark the permalink.

2 Responses to Arduino First – Light Seeking ‘Robot’

  1. murtaza lukmanjee says:

    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

    • GolezTrol says:

      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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s