Arduino solar tracker with stepper

Hello guys. I have some trouble with these arduino codes. And I was finished my solar tracker but without any codes please help me :slight_smile:

2 x 28 BYJ-48 Steper Motor and ULN2003A Step Motor Driver Board

4 x LDR Light Sensor Card

These are my pin numbers

4 x LDR

up left A0
up right A1
bottom left A2
bottom right A3

2 x stepper motor

for vertical move

1N1 pin 11
1N2 pin 10
1N3 pin 9
1N4 pin 8

for horizantal move

1N1 pin 7
1N2 pin 6
1N3 pin 5
1N4 pin 4

and these are my project photos

Can anybody help me, please :slight_smile:

What code do you have?

Google is your friend. I typed in arduino solar tracker code and got over 40,000 hits. Surely one of those will be helpful.

Thats right google the best but too many with servo motor but i find 3 or 4 good code for stepper then i looked the codes 1 motor and i see 2 pins again. İ find for 4 pins then look for pins then upload my work then only lights up but still dont move anywhere. These are my codes

// using unipolar stepper motors 28BYJ-48 with ULN2003 driver breakout board
#include <Stepper.h> // include stepper library


const int stepsPerRevolution = 4076; // the number of steps per revolution for your motor

//initialize the stepper library on pins 4 through 1
Stepper myStepperH(stepsPerRevolution, 4, 5, 6, 7); //azimuth adjustment
Stepper myStepperV(stepsPerRevolution, 8, 9, 10, 11); //elevation adjustment

// LDR (Light Dependent Resistor) pin connections
// name = analog pin on respective arduino board;
int ldrtopright = 1; //LDR top right
int ldrtopleft = 0; //LDR top left
int ldrbottomleft = 2; //LDR bottom left
int ldrbottomright = 3; //LDR bottom right

int dtime = 25; //delay time in milliseconds
int tolerance = 100; //range of tolerance between LDR readings
int count = 0; //start millisecond count

void setup()
{
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  Serial.begin(9600);  // initialize the serial port:
  myStepperH.setSpeed(15);
  myStepperV.setSpeed(15);
}
void loop()
{
//int count = millis(); //start millisecond count
int tr = analogRead(ldrtopright);
int tl = analogRead(ldrtopleft);
int bl = analogRead(ldrbottomleft);
int br = analogRead(ldrbottomright);

//print resistance values to serial monitor for debugging
Serial.println(tr);
Serial.println(tl);
Serial.println(bl);
Serial.println(br);
Serial.println();

count++;

int avt= (tr + tl) / 2; // average value top
int avd = (bl + br) / 2; // average value down
int avl = (tl + bl) / 2; // average value left
int avr = (tr + br) / 2; // average value right
int avgTot = (tr+tl+bl+br)/4;

int dvert = avt - avd; // check the difference of top and bottom LDRs
int dhoriz = avl - avr;// check the difference of left and right LDRs

if (-1*tolerance > dvert || dvert > tolerance) // check if the difference is in the tolerance else change vertical angle
{
if (avt > avd) //if average resistance on top is greater than on bottom
{
myStepperV.step(stepsPerRevolution);
}
else //avg resistance on bottom is greater than on top
{
myStepperV.step(-stepsPerRevolution);
}
}
else if (-1*tolerance < dvert || dvert < tolerance) //if difference is smaller than tolerance, stop vertical stepper
{
myStepperV.setSpeed(0);
}

if (-1*tolerance > dhoriz || dhoriz > tolerance) // check if the difference is in the tolerance else change horizontal angle
{
if (avl > avr)
{
myStepperH.step(-stepsPerRevolution);
}
else
{
myStepperH.step(stepsPerRevolution);
}
}
else if (-1*tolerance < dhoriz || dhoriz < tolerance) //if difference in horizontal
{
myStepperH.setSpeed(0);
}
delay(dtime);
}

First, reformat your code using Ctrl-T in the IDE...it will be easier to read. Second, replace these statements:

int avt= (tr + tl) / 2; // average value top
int avd = (bl + br) / 2; // average value down
int avl = (tl + bl) / 2; // average value left
int avr = (tr + br) / 2; // average value right
int avgTot = (tr+tl+bl+br)/4;

int dvert = avt - avd; // check the difference of top and bottom LDRs
int dhoriz = avl - avr;// check the difference of left and right LDRs

with hard-coded test values and see what the rest of your code does. Are the results what you expected for the values presented?

I suspect a simpler approach would be something like this pseudo code

loop() {
    // read LDRs
    if (leftLDR > rightLDR) {
        // move one step left // move towards brighter LDR
    }
    else if (rightLDR > leftLDR) {
        // move one step right
    }
}

...R

I am not good at arduino code. I use always flowcode. I hear something about arduino is very simple. Then I use arduino but not that easy. My english is bad sorry.

if a0 and a3 is 1
motor 2 is move left until a2 a3 is 1
then
if a2 a3 is 1
move motor 1 down until a0 a1 a2 a3 is 1

like this but this flow chart is very simple.

Hey econjack i tried ctrl-t but says no changes necessary for auto format. And i dont know what am I expected for these values :smiley: My friends send to these codes :smiley:

I don't know how to say it any simpler than in Reply #5.

Perhaps if you explain what you don't understand ?

(By the way, in Reply #5 I only dealt with one axis - you would need the same again for the other axis)

...R

You might want to switch to a picAxe which has a flowcharting program.

Robin2:
I don't know how to say it any simpler than in Reply #5.

Perhaps if you explain what you don't understand ?

(By the way, in Reply #5 I only dealt with one axis - you would need the same again for the other axis)

...R

Yea Robin first i dont understand you but now yea thats simple but how am ı control these stepper motors. KeithRB may I use flowchard codes on arduino ?

No, only C/C++

Here is a link to the modern PicAxe flowchart program:
http://www.picaxe.com/Teaching/Blockly-for-PICAXE/

SerdarKurkcu:
but how am ı control these stepper motors.

There mut be dozens, perhaps hundreds, of Threads on the Forum with code for 28BYJ stepper motors, However I have only got bigger biploar motors myself so my code won't help.

...R