How to make a light tracker using arduino & photoresistors!


Category : Arduino, Electronics


This is how to make a simple light tracker/follower with the Arduino. The components you need apart from the Arduino is:

2x Photoresistors: “PHOTOCELL2 Miniature Photocell”.
2x 470 Ohms resistors.
1x Servo.

And of course some wires, and a breadboard or two :). Next step is to wire it all up, I’ve made a drawing in Fritzing that you can follow. Basically each photoresistor is wired likes this:

PhotoR       470Ohms
+5    o—///–.–///—o GND
Pin 0 & 1 o——-

Program Code:

#include <Servo.h>

Servo myservo;

int pos = 0;  // Variable to store the servo position.
int inputPhotoLeft = 1; // Easier to read, instead of just 1 or 0.
int inputPhotoRight = 0;

int Left = 0; // Store readings from the photoresistors.
int Right = 0; // Store readings from the photoresistors.

void setup()
myservo.attach(9); // Attach servo to pin 9.

void loop()
// Reads the values from the photoresistors to the Left and Right variables.
Left = analogRead(inputPhotoLeft);
Right = analogRead(inputPhotoRight);

// Checks if right is greater than left, if so move to right.
if (Left > (Right +20))
// +20 is the deadzone, so it wont jiggle back and forth.
if (pos < 179)

// Checks if left is greater than right, if so move to left.
if (Right > (Left +20))
// +20 is the deadzone, so it wont jiggle back and forth.
if (pos > 1)
pos -= 1;

// Added some delay, increase or decrease if you want less or more speed.

PS. If you got any questions etc, please take it in my forums post here.

Comments (22)

awesome! cheerz for the tutorial. good stuff.

I’m getting an error for this line of code 8 lines from the bottom: pos–;

error: stray ” in program In function ‘void loop()’:

any ideas?

Yes, I see now. I’ve been a littlebit fast.

At the bottom of the code:

if (pos > 1)

Should be:

if (pos > 1)
pos –;

For some reason you need a space before the two — too.

I’m getting the same error.

should it be:

if (pos > 1)
pos –-;

Yes, damn wordpress is messing up the – -. Alternatively you could use the following code, does the exact same thing:

if (pos > 1)
pos = pos – 1;


if (pos > 1)
pos -= 1;

As you can see I’ve changed the orginal post in the blog to the last example.


I’m having a go at this but i keep getting the error

19: error: Servo.h: No such file or directory In function ‘void setup()’:
In function ‘void loop()’:


In function ‘void setup()’:
error: ‘myservo’ was not declared in this scope In function ‘void loop()’:

I’m new too all this , nice project BTW i made i light you shake to changer the colour with an arduino.

Will this work with the Funnel IO with ATmega168



That`s strange, the first error tells me that you servo libary in the arduino folder is gone. Could you download the latest arduino software and try with that one?

Second error is partly because of the first one, or you might be missing the two top lines:

Servo myservo;

Feel free to add me to msn if you want help. stig_moen A hotmail. com

И придратся не к чему, а я так люблю покритиковать…

mais oui c’est clair !

when I plug in the servo it fail,, it chortes out,, te whole arduinoo?….

Hii All,

The code is perfect but I will make a 3D solar tracking system.So I need more than 2 ldr to read the brıghtness.But arduino has only 6 analog inputs.So I used a 4051 analog mux.Basıcally all the procedure is the same but I cannot read the brıhtness values through the ldrs .
At the end . I want to control the servo according to the ldr values.
How can I read the brıhtness values through the ldrs .
help meee pleaese thanks .

Man SWEET tutorial, I am thinking of sharing this with my electronics class! Thanks a lot Stigern!

You sure this works? The light values being reported by my sensor are larger than that of the values required by the servo. I can’t see anything in your code that converts them to 0-180.

Hey i was following your design and constructed my own solar tracker, but when i ran the code you supplied, my servo just keeps spinning and if i cover up on sensor it spins the other way and vise versa. if i cover both of them it doesn’t stop. Any idea where i could have gone wrong?

Max:It’s hard to tell just from reading what you say, but check the following:

1. Does the ldrs work the same way? Reads almost the same values? Check with serial.print and see.

2. Could you have mixed the inputs? Doublecheck that.

Terminology failure:

This circuit is using Light Derpendant Resistors, NOT photocells. This circuit appears when you search for Photocells and it should not.

Please correct your terminology.

Hi!, a have already did this program but with another code with 3 photoresistors, but now i have to first make the servo to scan te whole 0 to 180 degrees and get in posicion with the spot of most light, any ideas ?

Great tutorial. Cheers!!


Hi what should the distance between the photo sensors be… ty

Hi, it’s not very important. I used 4-5 cm.

Hi could you add a sweep to the code so it makes a 180 degree turn to detect the light… because what it is doing now is that it sets the servo directly to 180 or 1 degree position. If it make a sweep and calculated the brightest point and start from there. Im pretty new to arduino and im not that skilled with codes. but ill be very thankful if you can do it. 🙂

Post a comment