0% found this document useful (0 votes)
7 views2 pages

Arduino Servo Control with LDR Sensors

The document contains an Arduino sketch for controlling two servo motors (horizontal and vertical) based on light sensor readings from four LDRs. It calculates the average light intensity from the sensors to determine the necessary adjustments to the servo positions, ensuring the device remains balanced. The code includes limits for the servo movements and a tolerance value for adjustments.

Uploaded by

pereradilhara2
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Arduino Servo Control with LDR Sensors

The document contains an Arduino sketch for controlling two servo motors (horizontal and vertical) based on light sensor readings from four LDRs. It calculates the average light intensity from the sensors to determine the necessary adjustments to the servo positions, ensuring the device remains balanced. The code includes limits for the servo movements and a tolerance value for adjustments.

Uploaded by

pereradilhara2
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#include <Servo.

h>

Servo horizontal; // Horizontal Servo Motor


int servohori = 180;
int servohoriLimitHigh = 175;
int servohoriLimitLow = 5;

Servo vertical; // Vertical Servo Motor


int servovert = 45;
int servovertLimitHigh = 100;
int servovertLimitLow = 1;

// LDR pin connections


int ldrlt = A0; // Bottom Left LDR
int ldrrt = A3; // Bottom Right LDR
int ldrld = A1; // Top Left LDR
int ldrrd = A2; // Top Right LDR

void setup() {
[Link](2);
[Link](13);
[Link](180);
[Link](45);
delay(2500);
}

void loop() {
int lt = analogRead(ldrlt); // Top left
int rt = analogRead(ldrrt); // Top right
int ld = analogRead(ldrld); // Bottom left
int rd = analogRead(ldrrd); // Bottom right

int dtime = 10;


int tol = 90; // Tolerance value for adjustment

int avt = (lt + rt) / 2; // Average value of top sensors


int avd = (ld + rd) / 2; // Average value of bottom sensors
int avl = (lt + ld) / 2; // Average value of left sensors
int avr = (rt + rd) / 2; // Average value of right sensors

int dvert = avt - avd; // Difference between top and bottom


int dhoriz = avl - avr; // Difference between left and right

if (abs(dvert) > tol) {


if (avt > avd) {
servovert = ++servovert;
if (servovert > servovertLimitHigh) servovert = servovertLimitHigh;
} else {
servovert = --servovert;
if (servovert < servovertLimitLow) servovert = servovertLimitLow;
}
[Link](servovert);
}

if (abs(dhoriz) > tol) {


if (avl > avr) {
servohori = --servohori;
if (servohori < servohoriLimitLow) servohori = servohoriLimitLow;
} else {
servohori = ++servohori;
if (servohori > servohoriLimitHigh) servohori = servohoriLimitHigh;
}
[Link](servohori);
}

delay(dtime);
}

Common questions

Powered by AI

The purpose of setting limits for servo angles is to prevent the servos from exceeding their physical or intended operational range, which could potentially cause mechanical strain or system malfunction. The horizontal servo has limits set between 5 and 175 degrees, while the vertical servo is constrained between 1 and 100 degrees. These limits ensure the system operates within safe and intended boundaries .

The LDR sensor readings influence the movement of the servos by determining the difference in light intensity between different directions. The system calculates the average value of the top (avt) and bottom (avd) sensors, as well as the left (avl) and right (avr) sensors. Differences (dvert and dhoriz) between these averages control the movement of the vertical and horizontal servos, respectively. If the difference exceeds a specified tolerance (tol), the servos adjust their positions to align with the direction of greater light intensity within the constraints of their defined limits .

The use of average values from paired sensors (top, bottom, left, right) is effective because it smooths out individual sensor anomalies and considerations for ambient light variations, providing a more balanced input for decision-making. This method allows the system to make calculated adjustments rather than reacting to spikes from any single sensor. However, improvements could include integrating weighted averages that prioritize sensors known to have more significant impact under certain conditions, or introducing a calibration routine that accounts for differing sensor sensitivities over time. Such enhancements could offer improved precision by better accounting for environmental or hardware variances .

The tolerance value is crucial as it acts as a threshold to prevent unnecessary servo adjustments for minor differences in light intensity across the LDR sensors. This value ensures that only significant discrepancies result in servo movement, thus reducing unnecessary wear and power consumption. The tolerance is set to 90, meaning that any difference in light intensity below this value will not trigger servo adjustment, enhancing system stability and efficiency .

Upon startup, the servos are initialized to specific angles: the horizontal servo is set to 180 degrees and the vertical servo to 45 degrees. These initial positions serve as a reference point for subsequent movements. This calibration is crucial as it ensures that the servos start from a known, central position, which facilitates accurate and consistent tracking of light from a balanced state. Starting from predefined positions also helps avoid cumulative errors in servo adjustments that could occur if the initial state were arbitrary or unknown .

The system balances between vertical and horizontal movements by calculating average light readings from the LDR pairs positioned at the top, bottom, left, and right. It uses these averages to determine the difference in light exposure vertically (dvert) and horizontally (dhoriz). If these differences exceed the set tolerance, the corresponding servo is adjusted. This mechanism allows the system to prioritize adjustments based on the most substantial difference in light exposure, effectively balancing movements to achieve optimal light tracking from both the vertical and horizontal axes, ensuring that corrections are only applied where most needed .

The programming structures use conditional checks to ensure servo movements remain within predefined limits. Increment and decrement operations are bounded with if-statements that compare servo angles to their respective high and low limits. If a servo movement calculation results in an angle beyond these limits, it is reset to the nearest boundary (e.g., servohori is reset to servohoriLimitHigh or servohoriLimitLow, and similarly for vertical movement). These constraints prevent the physical hardware from moving beyond its operational range, protecting the system from possible mechanical failure or errors .

The 'delay' function is important because it introduces a controlled pause between iterations of the loop, ensuring that sensor readings and subsequent servo adjustments do not occur too rapidly or frequently. This helps stabilize the system by allowing enough time for the servos to reach their new positions before the next readjustment cycle. Additionally, it helps prevent excessive wear on the servos and reduces power consumption by ensuring that adjustments are not being constantly calculated and executed almost instantaneously .

A malfunction in any of the LDRs can significantly disrupt the system's ability to correctly orient towards the light. For instance, if an LDR gives false readings or no readings, this could falsely indicate an imbalance in light distribution, leading to incorrect rotations of the servos. If the top left or right LDR fails, it might cause constant adjustments in the vertical axis, whereas malfunctions in bottom left or right LDRs would affect horizontal adjustments. The system's overall efficiency and its main goal—to track light effectively—would be compromised, possibly resulting in increased power consumption or failure to maintain the desired orientation .

Lowering the tolerance level from 90 to 50 would make the system more sensitive to differences in light intensity between the LDR sensors, causing the servos to adjust more frequently. Although this could improve the accuracy of light tracking by reacting to finer light discrepancies, it might also lead to increased wear and tear on the servos, higher power consumption, and potential over-adjustment or oscillations as the system reacts to smaller variations that were previously considered negligible .

You might also like