const int led = 11; // LED connected to pin 11
const int sound = A0; // Sound sensor connected to A0
int soundVal = 0; // Variable to store sound sensor reading
int bright; // Variable to store calculated brightness
void setup() {
pinMode(led, OUTPUT); // Set LED pin as output
pinMode(sound, INPUT); // Set sound sensor pin as input
Serial.begin(9600); // Initialize Serial Monitor
}
void loop() {
// Read analog value from sound sensor
soundVal = analogRead(sound);
// Print sound value to Serial Monitor
Serial.print(\"soundVal = \");
Serial.println(soundVal);
// Map sound value range (adjust 516 as baseline background noise)
bright = map(soundVal, 516, 1023, 0, 255);
bright = max(0, bright); // Prevent negative values
// Adjust LED brightness based on sound level
analogWrite(led, bright);
// Delay for 0.2 seconds
delay(200);
}
Instruction Details
Wiring: Connect sound sensor VCC to 5V, GND to GND, analog OUT to Analog Pin A0. Connect LED anode to Digital Pin 11 via 220Ω, cathode to GND.
Library: Not required (built-in).
Upload Code: Tools → Board → Arduino Uno. Tools → Port → select your COM port. Click Upload button.
View Output: LED brightness varies with sound intensity. Open Serial Monitor at 9600 baud for sound values. Adjust potentiometer for sensitivity.
How to Use
Connect the required components as per the Pin Configuration above
Open Arduino IDE and create a new sketch
Copy and paste the code above
Select your Arduino board and COM port from Tools menu
Click the Upload button to upload the code to your Arduino
Open Serial Monitor (if applicable) to see the output