๐Ÿ‡ฆ๐Ÿ‡บ Electronics Community ยท Australia
Login Join Free

Control Two Servo Motor SG90 using LDR interfacing with Arduino Uno

This Circuit Control Two Servo Motor SG90 using LDR interfacing with Arduino Uno. We need two LDR, TWO SG90 Servo Motor and an Arduino Uno and some connecting Jumpers. This project can be used in making solar pannel tracker where in the solar pannel can be driven with two servo motors for X and Y AXIS.

Circuit diagram for Control Two Servo Motor SG90 using LDR interfacing with Arduino Uno
Watch This Circuit in Action โ€” ElectronGuide YouTube

๐ŸŽฌ Like this video? Subscribe to our YouTube channel for more electronics projects!

</> Source Code
//electronikprojects.com
//like share and subscribe 


#include <Servo.h>
Servo myservo;
int pos = 0;
int lightPin = A0;

Servo myservo1;
int pos1 = 0;
int lightPin1 = A2;

void setup() {
  myservo.attach(8);
  myservo1.attach(9);
}


void loop()   {
  int lightLevel =analogRead(lightPin);
  int lightLevel1 =analogRead(lightPin1);
  lightLevel = map(lightLevel, 0, 1023, 0, 179);
  lightLevel1 = map(lightLevel1, 0, 1023, 0, 179);
  pos = constrain(lightLevel, 0, 179);
  pos1 = constrain(lightLevel1, 0, 179);

  myservo.write(pos);
  delay(100);

  myservo1.write(pos1);
  delay(100);


}

This Arduino sketch uses the Arduino IDE along with the Servo library Arduino to control two servo motors based on light sensor input. At the beginning, two servo objects (myservo and myservo1) are created, along with variables to store their positions (pos, pos1). Two analog pins (A0 and A2) are assigned to light sensors, which will provide varying voltage readings depending on the amount of light detected. In the setup() function, each servo is attached to a digital pin (8 and 9), allowing the Arduino board to send control signals to them.


Inside the loop() function, the program continuously reads the light intensity from both sensors using analogRead(), which returns a value between 0 and 1023. Since servo motors operate within a range of about 0 to 180 degrees, these readings are converted using the map() function to a range of 0 to 179. The constrain() function is then used as a safety measure to ensure the values stay within valid servo limits. This processed data determines the angle at which each servo should be positioned.


Finally, the code sends the calculated positions to the servos using the write() function, making them rotate according to the light levels detected. A short delay of 100 milliseconds is added after each movement to stabilize the motion and prevent excessive rapid updates. Overall, this program effectively creates a simple light-responsive system—often used in projects like solar tracking or light-following mechanisms—where each servo adjusts its angle based on the intensity of light sensed on its respective input.

Questions & Answers (0)

No questions yet โ€” be the first to ask!

Ask a Question
ⓘ Questions are reviewed by admin before being published. Please keep questions relevant to this project.

Get the Latest Projects & News

New projects, tutorials and community updates delivered to your inbox.