In: Computer Science
The purpose of this project is to use two light sensors (photoresistors) to measure light intensity in two opposite positions (0 & 180°) and indicate the position with a servomotor. The materials that will be used are: two light sensors, one Arduino micro-controller, two LEDs, and one servo motor. Each LED will indicate when a photoresistor is illuminated (positioned on opposite sides of the servomotor), while the servomotor moves to the illuminated side. In addition, when the intensity of the two photoresistors is almost the same, the servo motor must be placed in a central position (90°), and both LEDs must be turned off. Please use serial print to track the code. Thanks
Connections are made as per the requirements given. The circuit in tinkerCad is shown below.
Code for the above given circuit is attached below.
#include <Servo.h>
int ldr_180=0; //photoresistor at 180 pin number
int ldr_0 =1; //photoresistor at 0 pin number
int led_180=6; // LED indicating 180 servo angle pin
int led_0 =7; // LED indicating 0 servo angle pin
int value_180=0; // ldr at 180 value
int value_0 =0; // ldr at 0 value
Servo servo_9; // servo motor varible
void setup()
{
servo_9.attach(9); //declaring the pin for servo motor
pinMode(led_180,OUTPUT); //declaring the led pin as OUTPUT
pinMode(led_0,OUTPUT); //declaring the led pin as OUTPUT
pinMode(ldr_180,INPUT); //declaring the ldr pin as INPUT
pinMode(ldr_0,INPUT); // declaring the ldr pin as INPUT
Serial.begin(9600);
}
void loop()
{
value_180 = analogRead(ldr_180); // reading photoresistor values
value_0 = analogRead(ldr_0); // reading photoresistor values
Serial.print("LDR at 180 degrees intensity ");
Serial.println(value_180);
Serial.print("LDR at 0 degrees intensity ");
Serial.println(value_0);
if (value_180>value_0){ // if left LDR as more light
digitalWrite(led_180,HIGH); // LED at 180 is ON
digitalWrite(led_0,LOW); // LED at 0 is OFF
servo_9.write(180); // servo motor is turned to 180
Serial.println("Turned to 180");
}
if (value_0>value_180){ // if right LDR as more light
digitalWrite(led_0,HIGH); // LED at 0 is ON
digitalWrite(led_180,LOW); // LED at 180 is OFF
servo_9.write(0); // servo motor turned to 0
Serial.println("Turned to 0");
}
if (value_180==value_0){ // if both are equal
digitalWrite(led_180,LOW); // LED is OFF
digitalWrite(led_0,LOW); // LED is OFF
servo_9.write(90); // Servo at 90
tone(10,255,400); // buzzer is ON
delay(1000);
noTone(10);
Serial.println("Turned to 90");
}
delay(800); // delay helps to observe the serial monitor clearly
// you can remove it.
}
Make sure you connect the circuit as shown in the circuit.
Some of the results are shown below.
Hope you got the answer. If you still have any doubts regarding this, please try to comment, I will solve them for you.