Introduction: Arduino 2-axis Servo Solar Tracker

What is a solar tracker?

A solar tracker can increase the efficiency of a solar panel by up to 100%! It does this by always keeping the panel perpendicular to the incoming rays of sunlight.

here's an equation to prove this:

P = AW sin θ

P = power generated by the solar panel

A = Area of the solar panel

W = is the solar constant, which is equal to 1340 watts per square meter

θ = the angle of the incoming light

Since sin(90) = 1  you get the best performance out of the panel when it is totally perpendicular.


1 - Arduino Uno w/ ability to program it
1 - Breadboard
1 - 2 axis tracking mechanism (i used a magnifying mirror that swiveled up and down)
2 - 360 deg continuous rotation servos
1 - ball bearing tilt switch or similar
5 - 10k resistors
1 - 5V breadboard power supply
3 - cadmium sulfide light sensitive resistors
soldering iron
vice (optional)


Step 1: The Y-axis

Depending on what you use as a tracking mechanism, yours might be a little different than mine.

I picked up a magnifying mirror from Shoppers normally used for shaving or applying makeup. It had a base and a swivel to tilt the mirror up and down.

I cut off one side of the swivel bracket and replaced it with a servo. I measured so that the shaft of the servo would line up with the swivel point of the mirror. I then had to drill a hole for the servo shaft to fit into.

I used hot glue and zip ties to secure the servo to the base and then the servo shaft inside the hole I had drilled.

Step 2: The X-axis

I marked the approximate center of the base and drilled a hole large enough to screw the setscrew for the servo armature in. I then hot glued the armature to the underside of the base making sure not to get glue in the hole i drilled.

Once the glue was dry, I attached the servo and screwed in the setscrew.

Step 3: The Sensors

I used 3 light sensors and 1 tilt sensor. You can substitute LEDs or photo-transistors for the CDS cells, and/or mercury switches for the ball bearing tilt sensor but your code will have to reflect the change(s).

I soldered wires from a scrap ribbon cable to the leads of each light sensor and a 2 pin header on the opposite end to connect easily to the breadboard. Use electrical tape / heat shrink / liquid insulator on the bare parts of the wire so that they don't short out.

Once that's done, hot glue the sensors at equal intervals around the circumference of the mirror. I placed the sensors so that the flat collecting side of the sensor was parallel with the plane of the mirror and angled out from the center just slightly.

The tilt sensor that I found was a plastic box with 4 contacts running through it and a ball bearing inside. This sensor prevents the tracker from pointing at the ground and also gives the y-axis an end-stop.

Solder wires onto the 4 pins of the tilt sensor then glue it on the back side of the mirror with the leads running horizontally. With the mirror pointing straight upward, the BB should be resting on the 2 middle leads.

The image I have uploaded is similar but not exactly the same as the tilt sensor I have. The one I used has only 4 leads.

Step 4: Wiring It Up

Take a look at the pics for the wiring diagram and schematic. (sorry about the confusing scheamtic, still learning Fritzing)

***EDIT (04/03/13)*** Changed the images to reflect the proper wiring and cleaned it up a bit.

Step 5: The Arduino Code

#define TILTL 2
#define TILTH 3
#define BOTTOM 2
#define TOPLEFT 0
#define TOPRIGHT 1
#include <Servo.h>
#include "math.h"

Servo hservo;
Servo vservo;
int tlsense;
int trsense;
int bsense;
int tavg;
int diff;
int spd;
int divisor;
int sensitivity;
int tiltl;
int tilth;

void setup () {
  vservo.attach(9); // attaches the servo on pin 9 to the servo object
  hservo.attach(10); // attaches the servo on pin 10 to the servo object
  divisor = 10; // this controls the speed of the servo. lower number = higher speed
  sensitivity = 5; // this controls the sensitivity of the tracker. lower number = higher sensitivity. if your tracker is constantly jittering back and forth increase the number
  Serial.begin(19200); // open serial com
  Serial.print("SolarTracker ready!");
  pinMode(BOTTOM, INPUT); // set the inputs
pinMode(TILTL, INPUT);
pinMode(TILTH, INPUT);

void loop () {

tiltl = digitalRead(TILTL); // read the tilt sensor
tilth = digitalRead(TILTH);
tlsense = analogRead(TOPLEFT); // read the light sensors
trsense = analogRead(TOPRIGHT);
bsense = analogRead(BOTTOM);
//bsense = bsense * 1.05; // I had to adjust the value of this sensor to make it more accurate. you might have to do the same but start by leaving it alone
tavg = (tlsense + trsense)/2; // get an average value for the top 2 sensors
diff = abs(tavg - bsense); // this judges how far the tracker must turn
spd = diff/divisor; // and adjusts the speed of the reaction accordingly
spd = max(spd, 1); // sets the minimum speed to 1
Serial.print("\nTOP: "); Serial.print(tavg, DEC); // print the sensor values to the serial com
Serial.print("\tBOTTOM:"); Serial.print(bsense, DEC);
Serial.print("\tLEFT:"); Serial.print(tlsense, DEC);
Serial.print("\tRIGHT:"); Serial.print(trsense, DEC);

if((tavg < bsense) && (diff > sensitivity) && (tiltl == LOW) && (tilth == LOW)){ // if the average value of the top sensors is smaller (more light) than the bottom sensor and the tilt sensor is in the correct range
vservo.write(90 - spd); // send servo command to turn upward plus add speed
Serial.print("\tState: "); Serial.print("UP!");
}else if((tavg < bsense) && (diff > sensitivity) && (tiltl == HIGH) && (tilth == LOW)){ // if the average value of the top sensors is smaller (more light) than the bottom sensor and the tilt sensor is in the correct range
vservo.write(90 - spd); // send servo command to turn upward plus add speed
Serial.print("\tState: "); Serial.print("UP!");
}else if((tavg > bsense) && (diff > sensitivity) && (tiltl == HIGH) && (tilth == LOW)){ // if the value of the bottom sensor is smaller (more light) than the average value of the top sensors and the tilt sensor is in the correct range
vservo.write(90 + spd); // send servo command to turn downward plus add speed
Serial.print("\tState: "); Serial.print("DOWN!");
}else if((tavg > bsense) && (diff > sensitivity) && (tiltl == LOW) && (tilth == HIGH)){ // if the value of the bottom sensor is smaller (more light) than the average value of the top sensors and the tilt sensor is in the correct range
vservo.write(90 + spd); // send servo command to turn downward plus add speed
Serial.print("\tState: "); Serial.print("DOWN!");
}else{ // for every other instance
vservo.write(90); // stop the y-axis motor
Serial.print("\tState: "); Serial.print("STOP!");

tlsense = analogRead(TOPLEFT); // read the top 2 sensors again because they have probably changed
trsense = analogRead(TOPRIGHT);
//trsense = trsense * 1.03; // again I had to adjust the value of one sensor to make the tracker more accurate
diff = abs(tlsense - trsense); // reset the diff variable for the new values
spd = diff/divisor; // and generate a speed accordingly
spd = max(spd, 1); // set the minimum speed to 1

if((tlsense < trsense) && (diff > sensitivity)){ // if the top left sensor value is smaller (more light) than the top right sensor
hservo.write(90 + spd); // send servo command to turn left
Serial.print("\tState: "); Serial.print("LEFT!");
}else if((tlsense > trsense) && (diff > sensitivity)){ // if the top left sensor value is larger (less light) than the top right sensor
hservo.write(90 - spd); // send servo command to turn right
Serial.print("\tState: "); Serial.print("RIGHT!");
}else{ // for every other instance
hservo.write(90); // stop the x-axis motor
Serial.print("\tState: "); Serial.print("STOP!");

delay(10); // delay 10ms

Arduino polls the sensors and reacts accordingly making sure never to tilt too far down or up. The difference in light determines how fast the tracker should react.

Step 6: The Vid

Ya i look tired, I work a lot. Wanafightaboutit?


majid.nabil made it!(author)2016-04-19

good work

SwarnadeepP made it!(author)2016-03-30

Hi I am making this tracker and using same of your code but the horizontal axis is not moving .. Can you please help me ?

baspav made it!(author)2015-04-04

Hello. Trying to understand the sketch and translate the comments, I'm sorry, correct me if I'm wrong, but maybe here is the error:
}else if((tavg > bsense) && (diff > sensitivity) && (tiltl == HIGH) && (tilth == LOW)){ // if the value of the bottom sensor is smaller (more light) than the average value of the top sensors and the tilt sensor is in the correct range?
I think there should be
}else if((tavg > bsense) && (diff > sensitivity) && (tiltl == LOW) && (tilth == LOW)){ // if the value of the bottom sensor is smaller (more light) than the average value of the top sensors and the tilt sensor is in the correct range


Dear Adam.

Two years ago I acquired a 10-Watt solar
panel, controller, battery. Two years this instrument
helps me in tourist trips. Naturally, the question arose about the tracker. I looked at many options on the Internet, and has collected this one:
by purchasing servo motors (where
they at 60 degrees), but the functionality was not good.

Since I
need two axis tracker, I stopped on Your wiring diagram, completely changing the mechanical part. I
purchased the Visduino UNO R3 and the breadboard
on Aliexpress. Previously on the market bought the photoresistors. I took the 10K resistors from a radio boards. Assembled circuit for half an hour.
Because with the Arduino I ran for
the first time and I don't know how to write sketches for the Arduino at all, I
just copied Your sketch, filled in the Arduino (detailed instructions how to
upload sketches to the
Arduino the Internet is full), hooked up to my 60-degree servos.It worked immediately.


I have two
drivers UNL2003A and two stepper motors. So I would like to use
them in my scheme.

Since I don't know how to write
sketches, can I ask You to help me in changing Your sketch, so instead of servo
motors to connect stepper motors and show the scheme
of connection on the Arduino? I would be
very grateful.

Translated by Yandex.

aplavins made it!(author)2015-03-22


If you want to use stepper motors to control the tracker, I would recommend a STEP / DIRECTION style driver like this:

There is some working code here:


Thank you. But since I don't know enough English, I'm not able to program the Arduino, I want to know: can I connect two stepper motor whis drivers UNL2003A to the Arduino UNO R3; how to do it and to get changes in the sketch of Adam relative to the stepper motors. I do not in any way claim to the authorship of the sketch, when I'l publish the description, I will indicate the real authors of the sketch, but I need the actual version of the program. (although ideally have more requirements). If interested, I can throw on the mailbox.

Translated by Yandex.

PrayasB made it!(author)2015-02-17

y tilt sensors are used??/

PrayasB made it!(author)2015-02-17

dear admin

i want to know what is the role of tilt sensors?????

PrayasB made it!(author)2015-02-17

dear admin

i want to know what is the role of tilt sensors?????

jaykentchriss made it!(author)2015-01-17

Hi!... What about you will be adding a voltage and current sensors for the solar panel in your project?... it would be great if you will share to us your knowledge about coding. :)

jaykentchriss made it!(author)2014-11-21

good day!.. In your video, i see that there is no tilt sensor at the back of the mirror of your tracker... Where did you put your tilt sensor?... im just wondering... thanks... and also what if a mercury tilt sensor will be use?... how many mercury sensor will be used?... and can you send me the code please?.. i will really appreciate it.. Thanks :)

robinmitchell made it!(author)2013-03-03

i really wanted to thank you so much for sharing and posting this tutorial. it was my first experiment and programming experience and it was pretty easy after figuring out how to adapt a different type of tilt sensor. but because of your thorough commenting in your program sketch it was easy to modify your code. thank u thank u thank u!!!

jaykentchriss made it!(author)2014-11-21

hi robinmitchell... did you used a mercury tilt sensor in your project?.. how many mercury tilt sensor did you used? i had difficulty in modifying the code when i used a mercury tilt sensor.. can u send the code? pls?... i will really appreciate it... thanks :)

McAldo made it!(author)2013-10-30

That's a wonderful way to approach the problem, thanks for sharing it.

dpmdmph made it!(author)2013-08-26

Did you have to calibrate ur continuous servos first, before putting them in ur project?

DaBlakus made it!(author)2013-07-15

How does it perform in low light levels, like say dusk or dawn ??
I have made up a 2 axis system which works well under "normal' sunshine but I'm having trouble when all sensors are returning similar levels ie. diffused light when cloudy.
I have the sensors enclosed in short tubes 2 face 'forward' with the 3rd facing backwards to detect if the array is not facing the sun.

aplavins made it!(author)2013-07-16

The code measures the ambient light and measures the difference between each of the sensors. Problems do arise when all the sensors are receiving the same amount of light. The best solution would be a hybrid of light tracking and positional tracking.

robinmitchell made it!(author)2013-03-10

Hey Adam, nice update. interesting with this new set up, it looks like u can side step the alternate 5v power supply to the board. very cool!!
I was successful at replicating your experiment!! thanks for sharing!!
I was wondering about how to scale this project up to be able to carry a load of 40-50 lbs on top easily. ie a stirling engine and a fresnel lens :

i found this servo set up that i think might be able to do the job but i thought i would ask u for your thoughts. if i can accomplish the same thing bu replacing it with this servo gearbox set up.
thanks in advance for your time,
thanks in advance for you

Science+Wizard made it!(author)2013-03-05

Also in the diagram what is the two grey rounded pieces?

aplavins made it!(author)2013-03-05

the tilt sensors

Science+Wizard made it!(author)2013-03-04

Now both of my motors are working continually but the photocells aren't working. Also, the motors don't stop according to where the light is. How can i fix this? the 3rd wire, the pulse of the servo motor should have how many volts?

aplavins made it!(author)2013-03-05

read this:

google is your friend

Science+Wizard made it!(author)2013-03-04

i still don't know how to resolve my problem. What do I need to do so that the photo resistor or photocell tells the servo motors to move and stop according to the light detected.

Science+Wizard made it!(author)2013-03-03

Hi! I have made your solar tracker but with a different wooden mount but the wiring according to your diagram isn't working. The servo motors are either continuously turning or not at all. Can you email me or reply me here with further instruction on how to wire the arduino, breadboard, to the tracker to make it properly work. I would appreciate if you can get back to me by tomorrow. I need it for a special science fair project. Thank you!

aplavins made it!(author)2013-03-04

servo.write(90) should tell the servo to stand still. If it moves, you need to adjust the potentiometer inside the servo. there should be an access hole opposite the wires.

Science+Wizard made it!(author)2013-03-04

How would you do that and how do I connect the tilt sensor, I haven't done it so maybe that is a problem too? Also, can you send me a diagram with everything that i need to do and which diagram should i use from the two in your instructions?

aplavins made it!(author)2013-03-04

I updated the diagrams on the instructable. They are both the same, just different views.

Science+Wizard made it!(author)2013-03-04

If I need to adjust the potentiometer in the servo meter, how would i do that?

Science+Wizard made it!(author)2013-03-04

Also, is the arduino code correct. Do i just copy and paste it. then upload it to the arduino board?

robinmitchell made it!(author)2013-02-18

lovely project. i'm a newbie trying to recreate this project and i'm am curious why the ground connection on the pic is connected to the positive side of the servo motor on the breadboard image?

aplavins made it!(author)2013-02-18

Thanks for spotting that, it should be ground to ground.

maewert made it!(author)2011-02-07


Cool project!  Great for an all-purpose tracker.  For a specific purpose (solar) tracker you may wish to consider using a polar mount instead of an alt-az mount.  This will mean that only one motor would move to track the sun throughout the day.  Since the position of the sun is predictable, you could compute its position and not rely on sensors. (This might mean that you might need to change to steppers instead of servos and power off the motors when they don't need to move maybe using a worm gear drive which holds its position after the motors are unpowered.) 

Your solution could generate some electricity even from moonlight since it would track the brightest object and not necessarily only Mr. Sun however!  You might need an algorithm to determine if it is worth the electricity spent in the motors to eek out the most current (I'd hate to see a bank of solar cells track the headlights from a passing car at night!)  Actually I WOULD like to see that!  It would make me back up a few times!

Very nice instructable.  Best Wishes.

aplavins made it!(author)2011-02-07

Thanks for the comment, I agree that a "polar mount" might be better for tracking the sun. I'm going to make one of those next. I want to be able to move it to different locations though. I'll probably need a gps sensor, compass, 3-axis accelerometer and an eeprom to store the sun's movement data.

An improvement I could make to this project would be to sense the ambient light and tell it not to track at night.

gtoal made it!(author)2012-06-12

It would be best to return to the position needed for morning sun when you detect that night has fallen, while you still have some battery power available in case it is used up by some appliance overnight.

Agree with the polar mount - you just need to change the elevation manually once a month for best angle.

GPS for configuration is overkill - a paper table of latitudes should be enough, and longitude doesn't affect it.

Also - this would make it a different project - I think the servo can be driven analog without needing an arduino.

Good to see people working on this prohlem, there are no cost-effective trackers that I know of for the size of panel you'd buy for an RV or a boat - just toy ones for tiny panels and industrial ones for kilowatt panels...

244+Jake made it!(author)2011-10-20

Well a solar tracker, yes, but if you wanted to use what I see, a small mirror, and focus its light on water tank, all day long. That would most kool. A group of small mirrors could be grouped to all reflect the Suns heat to tank, all day long.

Reed switches could be used with magnets to define stop points.


Graver made it!(author)2011-07-29

Hi, I have put together your sun tracker. I could not find a ball bearing tilt switch so I substuted mercury switches not having luck with them as limit switches. I munted one up and one down at the axes line may be they should be out from closed to center. They don't stop the Y axes from rolling. I don't undersand how you arrived at the values for bsense = 1.05 and trsense = 1.03. What values should I see in ambient light and in direct sun light? What do you think about using 180 degree servos instead of continuous servos?
Fun project.

aplavins made it!(author)2011-08-04

I tested the 3 sensors from a single light source after they were mounted. the bottom sensor and top right sensor were giving me values that were lower than the top left sensor. This is probably due to some small variation in its manufacturing. The values 1.03 and 1.05 are ratios that the read analog value are multiplied by. I arrived at these values by powering up the tracker indoors where it would track the light on the ceiling. I used trial and error on the values until the mirror pointed the reflection of the light straight back at it.

Using 180 degree servos would be totally possible but would require major modification to the code. 360 degree servos use no feedback system and the pulse value you send to it represents a speed. With 180 degree servos, the pulse value represents a position.

Hope this helps!

Graver made it!(author)2011-08-07

Hi Apiavins: Still have a problem with the 1.03 and 1.05 numbers. Tried changing the 1.03 and 1.05 numbers to calabrate the sensors but they didn't seem to change. Did notice that bsense = bsense * 1.05 and trsense = trsende * 1.03 were commented out. I guess I don't understand were the calabration takes place. I have noticed that the sensor platforn does hunt a lot once it faces the light source. Think you said that sensitivity will take care of that. I will send you a photo of my tracker

aplavins made it!(author)2011-08-11

by default, the program doesn't change the values of bsense and trsense. you will have to uncomment the lines that multiply those values. you can also add a line to modify tlsense if you think that's more appropriate.

Increasing the value of sensitivity will reduce the amount of hunting.

nice pic, hope this helps!

About This Instructable




More by aplavins:Arduino 2-axis servo solar tracker
Add instructable to: