Sound Sensor Example
Sound Sensor is a small board with a microphone which allows you to detect the sound in an environment.
In this example we will use the sound sensor to command the on/off switch of a led clapping your hands.
In this example we will use the sound sensor to command the on/off switch of a led clapping your hands.
Hardware Required
- Arduino Board
- Sound Sensor
- Wires
- Breadboard
- 1 resistor of 220Ω
- 1 Led
Circuit
NOTE:
The real sensor can be different from the fritzing image.
The Sound Sensor usaully has four pins. Connect the Ground pin to the GND of board, the Vcc to pin 5V of the board and the Analogic pin to pin A0 of board.
Also connect the anode of the LED (usually the longer pin) in series to a resistor of 220Ω and connect it to pin 13 of board.
Finally connect the cathode of led to GND of board.
Also connect the anode of the LED (usually the longer pin) in series to a resistor of 220Ω and connect it to pin 13 of board.
Finally connect the cathode of led to GND of board.
Schematic
Code
Initially the led is switched off. If you clap, the led switch goes on and it will remain until you clap again.
Now we'll explain the sketch in detail.
In the first part of sketch we define the variables used, like the threshold of sensor.
You can change the sensibility of the sensor by changing the value of threshold or turning the trimmer placed on it.
In the setup function we define the led pin as output and we set it to LOW.
In the loop block we read the detected value from the sensor and it is placed in the sound variable, after we compare this value with the threshold value.
If the sound detected is higher we'll change the LED status, by changing the variable led_value.
The led_value variable is finally used with the digitalWrite to change the LED status.
Now we'll explain the sketch in detail.
In the first part of sketch we define the variables used, like the threshold of sensor.
You can change the sensibility of the sensor by changing the value of threshold or turning the trimmer placed on it.
In the setup function we define the led pin as output and we set it to LOW.
In the loop block we read the detected value from the sensor and it is placed in the sound variable, after we compare this value with the threshold value.
If the sound detected is higher we'll change the LED status, by changing the variable led_value.
The led_value variable is finally used with the digitalWrite to change the LED status.
/*Sound Sensor Example*/ int led = 13; int threshold = 500; //Change this value if you want change the sensibility of sensor int sound; int led_value; void setup() { pinMode(led, OUTPUT); digitalWrite(led,LOW); led_value=0; } void loop() { sound = analogRead(A0); // Reads the value from the Analog PIN A0 if(sound>=threshold){ led_value=!led_value;} digitalWrite(led, led_value); delay(100); }
0 comments:
Post a Comment