This circuit is designed to interact with environmental light using a photocell (LDR) to control a servomotor, an LED, and a piezo buzzer. The Arduino UNO serves as the central microcontroller unit, reading the LDR value and controlling the other components based on the light intensity detected. When the light level is below a certain threshold, the servomotor moves to a fixed position, the LED turns on, and the buzzer toggles between on and off states to create a beeping sound. The circuit includes a resistor to limit current to the buzzer and LED.
#include <Servo.h>
Servo myServo; // Create a servo object
int ldrPin = A0; // LDR connected to analog pin A0
int ldrValue = 0; // Variable to store LDR value
int servoPin = 9; // Servo connected to pin 9
int ledPin = 13; // LED connected to digital pin 13
int buzzerPin = 8; // Buzzer connected to digital pin 8
int threshold = 10; // Set the light threshold
int fixedPosition = 100; // Fixed position for the servo when it's dark
// Variables for buzzer beeping
unsigned long previousMillis = 0;
const long beepDuration = 500; // Duration for the buzzer to be ON (in milliseconds)
const long silenceDuration = 500; // Duration for the buzzer to be OFF (in milliseconds)
bool buzzerState = false; // Buzzer state to track whether it should be ON or OFF
unsigned long lastChangeMillis = 0; // Time of last change of buzzer state
void setup() {
myServo.attach(servoPin); // Attach the servo to pin 9
pinMode(ledPin, OUTPUT); // Set the LED pin as an output
pinMode(buzzerPin, OUTPUT); // Set the buzzer pin as an output
Serial.begin(9600); // Start serial communication for debugging
}
void loop() {
ldrValue = analogRead(ldrPin); // Read the LDR value (0-1023)
// Check if the LDR value is above the threshold
if (ldrValue > threshold) {
// If light is detected (not dark):
myServo.write(0); // Move the servo to 0 degrees
digitalWrite(ledPin, LOW); // Turn the LED off
noTone(buzzerPin); // Turn the buzzer off
buzzerState = false; // Reset buzzer state
lastChangeMillis = 0; // Reset last change time
Serial.print("LDR Value: ");
Serial.print(ldrValue);
Serial.println(" | Light Detected. Servo at 0 degrees. LED OFF. Buzzer OFF.");
} else {
// If the LDR value is below the threshold (dark):
myServo.write(fixedPosition); // Move the servo to the fixed position (180 degrees)
digitalWrite(ledPin, HIGH); // Turn the LED on
unsigned long currentMillis = millis();
// Check if it's time to change the buzzer state
if (buzzerState) {
// If buzzer is currently ON, check if it's time to turn it OFF
if (currentMillis - lastChangeMillis >= beepDuration) {
noTone(buzzerPin); // Turn the buzzer off
buzzerState = false; // Update state
lastChangeMillis = currentMillis; // Record the time of change
}
} else {
// If buzzer is currently OFF, check if it's time to turn it ON
if (currentMillis - lastChangeMillis >= silenceDuration) {
tone(buzzerPin, 1000); // Turn the buzzer on with a 1000 Hz tone
buzzerState = true; // Update state
lastChangeMillis = currentMillis; // Record the time of change
}
}
Serial.print("LDR Value: ");
Serial.print(ldrValue);
Serial.println(" | Below Threshold. Servo at 180 degrees. LED ON. Buzzer ON/OFF (Toggling).");
}
// No delay here to allow smooth operation
}
This code is designed to run on the Arduino UNO microcontroller. It initializes the connected components and controls them based on the LDR's readings. The servo's position, the LED's state, and the buzzer's beeping are all dependent on the light level detected by the LDR.