Wednesday, May 8, 2013

Sound Sensor




This project makes use of Seeedstudio's Grove Sound Sensor. Which essentially gives your Arduino ears ! The small sound sensor (microphone) attaches to an Analog pin on the Arduino and can be used to detect the level of noise in the surrounding environment. You could potentially use this sensor to turn a light on in your house after recognising a specific clap/whistle sequence. In this tutorial we are going to connect a few LEDs to the Arduino, and get it to listen for a click/clap, and respond accordingly. Have a look at the video below for this project in Action.







Parts Required





Assembly

  • Place the Grove Base shield onto the Arduino UNO or compatible microcontroller.
  • Solder some headers onto a ProtoBoard and then stack this onto the Grove Base Shield
  • Stick a Mini-Bread board onto the ProtoBoard.
  • Get 3 LEDs and 330 ohm resistors, connect to Pins 3, 5 and 6, and then to Ground
  • Attach the Sound Sensor to the Grove Base Shield A0 clip (next to shield's reset button) 




Sketch








Arduino LogoArduino Code:

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* =======================================================
ArduinoBasics: Sound Sensor: Written by ScottC 8th May 2013
==========================================================*/

int soundSensorPin=A0;
int soundReading=0;
int soundThreshold=500;
int intensity[3]={0,0,0};
int LEDPins[3] = {3,5,6};
int numberOfPins=3;
int currentPin=0;
int fadeCounter=0;
int fadeDelay=50;
boolean switcher = true;

void setup(){
pinMode(soundSensorPin, INPUT);
for(int i=0; i<numberOfPins;i++){
pinMode(LEDPins[i],OUTPUT);
}
}

void loop(){
soundReading=analogRead(soundSensorPin);
if(soundReading>soundThreshold){
if(switcher){
aboveThreshold(currentPin);
switcher=true;
}
} else {
if(switcher){
belowThreshold();
switcher=true;
}
}
}

void aboveThreshold(int cPin){
switcher=false;
if(intensity[cPin]<10){
intensity[cPin]=255;
delay(50);
currentPin=currentPin+1;
}

if(currentPin==numberOfPins){
currentPin=0;
}
}

void belowThreshold(){
switcher=false;
fadeCounter++;
if(fadeCounter==fadeDelay){
fadeCounter=0;
for(int i=0; i<numberOfPins;i++){
analogWrite(LEDPins[i],intensity[i]);
}
for(int i=0; i<numberOfPins;i++){
intensity[i]--;
if(intensity[i]<0){
intensity[i]=0;
}
}
}
}