This circuit is designed to control an LED bulb using an Arduino UNO based on input from a sound sensor. The system uses a 1-channel relay to switch the LED bulb on and off in response to sound detected by the sound sensor. The Arduino UNO is programmed to read the sound sensor output and toggle the relay state accordingly, which in turn controls the power to the LED bulb connected to a 220V AC power source.
// Definitions
const int relayPin = 8; // Relay Pin
const int soundSensorPin = 2; // Sound Sensor Pin
int relayState = LOW; // Initial relay state (light off)
int sensorValue = 0; // Variable to store the value from the sound sensor
unsigned long lastDebounceTime = 0; // Variable for debounce
unsigned long debounceDelay = 200; // Debounce delay 200 ms
void setup() {
pinMode(relayPin, OUTPUT); // Set Relay as output
pinMode(soundSensorPin, INPUT); // Set Sound Sensor as input
digitalWrite(relayPin, relayState); // Start with the relay off (light off)
}
void loop() {
// Read the value from the sound sensor
sensorValue = digitalRead(soundSensorPin);
// If sound is detected
if (sensorValue == HIGH && (millis() - lastDebounceTime) > debounceDelay) {
lastDebounceTime = millis(); // Reset debounce timer
// Toggle relay status
relayState = !relayState;
digitalWrite(relayPin, relayState);
}
}
The code is written for the Arduino UNO and is responsible for reading the digital output from the sound sensor. When sound is detected and a certain debounce period has passed, the state of the relay is toggled, which in turn switches the LED bulb on or off. The relay ensures that the high voltage required for the LED bulb is safely controlled by the low-voltage Arduino output.