Arduino based Solar Tracker – Stepper Motor & Light Resistor Tutorial

///Arduino based Solar Tracker – Stepper Motor & Light Resistor Tutorial

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

CODE: FULL CODE ON GITHUB
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
#include
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() {
Serial.begin(9600);
stepper.setRPM(5);
stepper.setSPR(4075.7728395);
}
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(rightSensorRead);
Serial.print(" left = ");
Serial.print(leftSensorRead);
Serial.print(" Pct r/l = ");
Serial.print(rightLightPct);
Serial.print("/");
Serial.println(leftLightPct);

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!");
rotateLeft();
stepper.run();
}else{
Serial.println("Turning CW!");
rotateRight();
stepper.run();
}
}

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(){
stepper.setDirection(CCW);
stepper.rotateDegrees(degreeChange);
while(stepper.isDone() == false){
stepper.run();
}
}
void rotateRight(){
stepper.setDirection(CW);
stepper.rotateDegrees(degreeChange);
while(stepper.isDone() == false){
stepper.run();
}
}

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

Conclusion

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

By | 2016-09-23T22:44:04-07:00 July 5th, 2016|Arduino, Tutorials|10 Comments

About the Author:

Welcome to EEEnthusiast.com My name is Vlad, I'm an Electrical Engineer and I love to build and teach. I graduated in 2013 and have tried my best to share my electrical knowledge and experience with viewers and readers from all over the world. You can find me on my personal site or LinkedIn!

10 Comments

  1. Gil September 7, 2016 at 3:13 pm

    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?

  2. grace February 6, 2017 at 6:47 pm

    hi nice project!. how to stop the motor if i want it to stop at a specific position? will i use a limit switch or just modify the code? thanks

  3. Steve February 13, 2017 at 5:47 pm

    Enjoyed building the sun tracker. I am interested in adding another stepper motor to rotate the solar cell as well. I have already built the device and while researching code, I found yours. Thank you. Worked out well. Any ideas for say adding another axis to the project?

  4. Newbe February 14, 2017 at 6:15 pm

    Please someone email be circuit diagram and hardware sketch to me

  5. FAHAD June 15, 2018 at 10:28 am

    I tried to upload the program directly to my Arduino Uno but I couldn’t because of some errors that I don’t know how to rectify it … please help me with the right program and schematic.

  6. EEEnthusiast June 18, 2018 at 4:24 pm

    Could you please post the errors? Here or anywhere else with a link?

  7. FAHAD June 20, 2018 at 11:04 am

    Hi … I performed this with the same program but unfortunately, the motor is only vibrating rather than rotating so I feel really disappointed. I changed the motor driver and motor as well as Arduino board but still motor not functioning… Please help me with this …. I am looking forward to hearing some suspicions.

  8. EEEnthusiast June 20, 2018 at 7:51 pm

    Can you send a serial output to see the target position of the motor? What does that give you?

  9. Cosmos June 28, 2018 at 9:17 am

    Pls can you send the circuit diagram and cost outline for the project to me. I will very much appreciate a response. Thanks.

  10. EEEnthusiast June 28, 2018 at 10:27 am

    Hi Cosmos, unfortunately, I no longer have the schematic for this project. That being said, it shouldn’t be extremely difficult to figure out from the video… Sorry about that.

Leave A Comment

CommentLuv badge

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Show Buttons
Hide Buttons