Go Down

Topic: dual axis solar tracker via 4 photoresistors and dc motors code cycling issue (Read 864 times) previous topic - next topic


I really do not understand why you reject this circuit for the L298N ?
I have been using this schematic and the sketch for a while and I can not fault it.
I use normal engines with delay use but engines with a reduction ....... about 100 rpm is good enough.
This means that you do not have to use enA and enB for PWM and only in1 tm in4.
On the motors there is a threaded M8 with a bearing nut which controls the panel horizontally and vertically.
As a result, the displacement is only 1 mm per revolution or 100 mm per minute, if more than enough to adjust the panel quietly and without shocks.

The sketch must be adapted to the use without enA and enB.

Please turn off your capslock ...... it is now as if you are screaming.

BTW, why do you use count ????

Code: [Select]

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,20,4); // address 0x27 - 4 rows - 20 character

// motor azimuth L298N
  int in1 = 9;
  int in2 = 10;
// motor elevation L298N
  int in3 = 11;
  int in4 = 12;

void setup()
  Serial.begin(9600); // initialize the serial port
  lcd.init(); // initialize the lcd

  lcd.backlight(); //ON
  lcd.clear(); //lcd clear

void loop()

  int tr = analogRead(0); // LDR top right / brown
  int br = analogRead(1); // LDR bottom right / green
  int tl = analogRead(2); // LDR top left / yellow
  int bl = analogRead(3); // LDR bottom left / orange

// control delay time in milliseconds of LIGHT SENSOR readings
  int delaytime = analogRead(A7)*2;
// set range of tolerance between LIGHT SENSOR readings - Arduino LED indicate time
  int tolerance = analogRead(A6)/4;
 // set range for read 0 - 100%
  int sensetive = map(analogRead(A6), 1022, 0, 0, 100);

 //print LIGHT SENSOR values to serial monitor for debugging
  Serial.print("Top Left\t");
  Serial.print("Bottom Left\t");
  Serial.print("Top Right\t");   
  Serial.print("Bottom Left\t"); 


  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 dv = avt - avd; // average difference of top and bottom LIGHT SENSORS
  int dh = avl - avr;// average difference of left and right LIGHT SENSORS

  if (-1*tolerance > dv || dv > tolerance) // check if the difference in top/bottom LIGHT SENSORS is greater than tolerance
  if (avt > avd) // if average LIGHT SENSOR values on top side are greater than on bottom side then elevation motor rotates CLOCKWISE

// set motor Elevation enable
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
  lcd.print("ELEVATION :    UP"); // print lcd row 1
  else // if average LIGHT SENSOR values on bottom side are greater than on top side then elevation motor rotates COUNTERCLOCKWISE
// set motor Elevation enable
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  lcd.print("ELEVATION :  DOWN");// print lcd row 1
  else if (-1*tolerance < dv || dv < tolerance) // if difference is smaller than tolerance, STOP elevation motor

// stop Elevation motor
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
  Serial.println("ELEVATION MOTOR STOP");
  lcd.print("ELEVATION :  STOP"); // print lcd row 1

  if (-1*tolerance > dh || dh > tolerance) // check if the difference in left and right LIGHT SENSORS is within tolerance range
  if (avl > avr) // if average LIGHT SENSOR values on left side are greater than right side, azimuth motor rotates CLOCKWISE

// set motor Azimuth enable
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  Serial.println("AZIMUTH MOTOR MOVES EAST");
  lcd.print("AZIMUTH   :  EAST"); // print lcd row 2
  else // if average LIGHT SENSOR values on right side are greater than on left side, azimuth motor rotates COUNTERCLOCKWISE
// set motor Azimuth enable
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  Serial.println("AZIMUTH MOTOR MOVES WEST");
  lcd.print("AZIMUTH   :  WEST"); // print lcd row 2
  else if (-1*tolerance < dh || dh < tolerance) //if difference is smaller than tolerance, STOP azimuth motor
// stop motor Azimuth
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  Serial.println("AZIMUTH MOTOR STOP");
  lcd.print("AZIMUTH   :  STOP"); // print lcd row 2
// print lcd delaytime row 3
  lcd.print("DELAY     :  ");
  char temp[5]; // enough room for 4 numbers
  snprintf(temp, 5, "%4d", delaytime); // %4d = 4 digits, right aligned
  lcd.setCursor(18, 2);
  lcd.print("ms"); // time in milliseconds

// print lcd sensitive row 4
  lcd.print("SENSITIVE :   ");
  char temp1[4]; // enough room for 3 numbers
  snprintf(temp1, 4, "%3d", sensetive); // %3d = 3 digits, right aligned
  lcd.setCursor(18, 3);
  lcd.print("%"); //
  delay(delaytime); // wait for next loop


hi, i didn't know you could use the l298 without pwm enA and enB options.
thanks for the code that could be useful for everyone else and I may try it out too.

I read on other sites the L298 is inefficient.
It may have caused a voltage drop when I was powering my motors with a 6V solar panel.
The cause for them not working could have been something else.
Lots of people on the forum recommended using a battery and it did help and work.

I wanted to try to move the motors without the use of batteries.
I ended up getting a larger 6V solar panel that was harder to find but I eventually found it:



My motors move fine with a good amount of sun.

I would recommend using the L298 for cost effectiveness since it's cheaper than the BTS7960:
The bts 7960 is better for larger motors that require more voltage and current.



Here is the original code for reference:

I will try out the L298 again myself
Another cheap alternative is L9110S
I don't know if it's more efficient than the L298 but it's voltage range is not as high.


I was/am a fan of writing in CAPs but since it's considered screaming I'm not using it.
I am noticing though it's easier to code using a both upper case and lower case letters.

I can't recall what the count++ function was for.

Go Up