Controlling a step motor using microstep driver for solar tracking system

Hi guys I'm a beginner at arduino and I'm trying to control a solar panel using a stepper motor a nema 23 to be more specific, using a microstep driver (DQ54MA), and I just can't get the software right, can you please tell me what's wrong with it, and don't judge it because this is my first project using an arduino.

Thanks alot,,
this is my code
const int LDR1 = A0;
const int LDR2 = A1;

const int PUL = 3;
const int DIR = 2;
const int tol =20;
const int EN= 4;

void setup() {
pinMode(PUL, OUTPUT);
pinMode(DIR, OUTPUT);


void loop() {
int x1 = analogRead(LDR1);
int x2 = analogRead(LDR2);
int xd = x1 - x2;
if (abs(x1 - x2) <= tol) {
else {
if (x1 < x2) {

if (x2 < x1) {


void TurnMotorCW() {

digitalWrite(DIR, LOW);
digitalWrite (PUL, HIGH);
digitalWrite(PUL, LOW);
void TurnMotorCCW() {
digitalWrite(DIR, HIGH);
digitalWrite (PUL, HIGH);
digitalWrite(PUL, LOW);
void TurnMotorOFF() {


What does it do that's not what you wanted?

No need to check every second.

Also, no need to move if the readings aren't exactly the same. Add some hysteresis.

You should post code by using code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

It could be a lot of different reasons.
You have to provide much more information
exact type of the light-sensors. Some of them can't distinguish between medium bright light and full sunlight.

A handdrawn schematic of how you connected the light-sensors to the analog-inputs

A description what you would expect your program to do and what you are observing what your program really does.

The description you have given so far is like you go to the doctor
saying "I feel bad"

The doctor will ask a lot of questions to get more information before he is able to help you.

best regards Stefan