Arduino based Solar Tracker – Stepper Motor & Light Resistor Tutorial

In this project, we will be building a simple solar tracker capable of detecting the most optimal position of the sun on one axis and adjusting itself accordingly. The goal is to create a control loop which will be able to accurately position a solar panel toward the sun.

Required materials and hardware

In this project, I wanted to use a stepper motor (28BYJ-48 Stepper Motor) for the purpose of showing the use of a stepper motor library and some of its functionality.

Optional Hardware:

Arduino Stepper Solar Tracker

Connecting your Arduino Solar Tracker

The connections for this project are not very tricky. The stepper motor is connected through the controller (typically included with it) to the Arduino. The arduino pins used in my case are 9, 10, 11 and 12. The controller has a 5 female socket which accommodates the stepper motor connector. The photo resistors are connected in series with the 220ohm resistors across the 5v rail and the GND. The signals which we are interested in are connected between the photo resistors and the 220 ohm resistors. The signals are connected back to the A0 and A1 pins on the Arduino Board.

Arduino Solar Tracker Circuit

Arduino Solar Tracker Circuit

Programming the solar tracker and the stepper motor

The two major components of the program are the servo control and the photo resistor positioning loop.
const int rightSensorPin = A0;
const int leftSensorPin = A1;
int rightSensorRead = 0;
int leftSensorRead = 0;
int rightLightPct = 0;
int leftLightPct = 0;
int degreeChange = 1;
//Arduino's CustomStepper library
//Author: Igor Campos
CustomStepper stepper(9, 10, 11, 12);

The first step is to initialize a few variables we will be using in the program. We will be using two analog pins: A0 and A1. The raw values of an analog read, will be stored in the following two integers. The “Pct” integers will be used to store the percentage of light the sensors are receiving. The degreeChange variable is used for the positioning control loop which we will cover later. Lastly, a CustomStepper is initialized for the purpose of controlling the stepper motor which will drive the solar panel.

void setup() {
void loop() {
rightSensorRead = analogRead(rightSensorPin);
leftSensorRead = analogRead(leftSensorPin);
rightLightPct = map(rightSensorRead, 0, 1023, 0, 100);
leftLightPct = map(leftSensorRead, 0, 1023, 0, 100);
degreeChange = map(abs(rightLightPct-leftLightPct),0,100,2,10);
Serial.print("right = ");
Serial.print(" left = ");
Serial.print(" Pct r/l = ");

In this section, we start off by initializing the Serial communication followed by the stepper motor parameters. The first one is the RPM which will be used for the stepper. It can be changed to a different value depending on the application, but I found that 5 works quite well for my setup. The setSPR function is used to set the number of steps per revolution for the motor. This value is derived from the gearing ratio of the motor.

Inside of the loop function, we start off by reading the analog pins which we use for the photo resistors. These values are then converted to a percentages through the map function. The next line computes the degreeChange variable which is used to control the number of degrees the solar panel will be adjusted for. The greater the difference between the two sensors, the greater of a move we will be making with our control loop.

The Serial lines you see below are mostly used for troubleshooting purposes. I’ve used them to report the values read on the sensors in order to make sure that light is properly captured and converted into percentages.

if(rightLightPct < 30 && leftLightPct < 30){ Serial.println("No sun detected!"); }else if(rightLightPct > leftLightPct){
Serial.println("Turning CCW!");
Serial.println("Turning CW!");

This section of the code is the main control loop. The first condition we are looking for is the “No sun present” which occurs when both sensors are reading below 30%. In this case, no adjustment will be made to the position of the stepper motor by the arduino. The second condition determines if the right sensor has more light shining on it than the left one. If this is true, the stepper would be turning in the counter clockwise direction. Otherwise, the stepper would be turned in the opposite (clockwise) direction.

void rotateLeft(){
while(stepper.isDone() == false){;
void rotateRight(){
while(stepper.isDone() == false){;

The final segment contains the functions outlined above. Both of these functions are used to rotate the motor clockwise or counter clockwise. The motor would be moving until the rotation has been completed entierly.

Solar Tracker Project

Solar Tracker Project

Valuable Resources & Links


At the end of this project, you would have a fully functioning prototype (or full installation) of a solar tracker. You should now be able to work with a stepper motor as well as photo resistors.

Make sure to update me with any projects you’ve built!

Thank you for reading & watching!
– EEEnthusiast

Comments 1

  1. Hi. Good information and a good step to get my feet wet with arduino and sun trackers.
    Where Can I find the schematic for this project?

Leave a Reply

Your email address will not be published.

CommentLuv badge