Sparkfun Sound Detector VS. HC-SR04 Ultrasonic
Which sensor module is more suited for Arduino projects?
Components and supplies
1
SparkFun Sound Detector (with Headers)
1
ELEGOO HC-SR04 Ultrasonic Distance Sensor Kits
Apps and platforms
1
Arduino Web Editor
1
Arduino IDE
Project description
Code
Ultrasonic Sensor Code
c_cpp
1int trigPin = 11; 2int echoPin = 12; 3 4long duration; 5int distance; 6 7void setup() { 8 9pinMode(trigPin, OUTPUT); 10pinMode(echoPin, INPUT); 11Serial.begin(9600); 12 13} 14void loop() { 15 16digitalWrite(trigPin, LOW); 17delayMicroseconds(2); 18 19digitalWrite(trigPin, HIGH); 20delayMicroseconds(10); 21digitalWrite(trigPin, LOW); 22 23duration = pulseIn(echoPin, HIGH); 24distance= duration*0.034/2; 25 26Serial.print("Distance: "); 27Serial.println(distance); 28}
Sparkfun Sound Detector
c_cpp
1/****************************************************************************** 2* sound_detector_demo.ino 3* Sound detector sample sketch 4* Byron Jacquot @ SparkFun Electronics 5* February 19, 2014 6* https://github.com/sparkfun/Sound_Detector 7* 8* This sketch demonstrates the use of the Sparkfun Sound Detector board. 9* 10* The Sound Detector is a small board that combines a microphone and some 11* processing circuitry. It provides not only an audio output, but also a 12* binary indication of the presence of sound and an analog representation 13* of it's amplitude. 14* 15* This sketch demonstrates two different modes of usage for the Sound 16* Detector. The gate output (a binary indication that is high when sound 17* is present, and low when conditions are quiet) is used to fire a pin-change 18* ISR, which lights an LED when the sound is present. The envelope output 19* (an analog voltage to rises to indicate the amplitude of the sound) is 20* sampled in the loop(), and it prints an indication of the level to the 21* serial terminal. 22* 23* For more details about the Sound Detector, please check the hookup guide. 24* 25* Connections: 26* The Sound Detector is connected to the Adrduino as follows: 27* (Sound Detector -> Arduino pin) 28* GND → GND 29* VCC → 5V 30* Gate → Pin 2 31* Envelope → A0 32* 33* Resources: 34* Additional library requirements: none 35* 36* Development environment specifics: 37* Using Arduino IDe 1.0.5 38* Tested on Redboard, 3.3v/8MHz and 5v/16MHz ProMini hardware. 39* 40* This code is beerware; if you see me (or any other SparkFun employee) at the 41* local, and you've found our code helpful, please buy us a round! 42* 43* Distributed as-is; no warranty is given. 44******************************************************************************/ 45// Define hardware connections 46#define PIN_GATE_IN 2 47#define IRQ_GATE_IN 0 48#define PIN_LED_OUT 13 49#define PIN_ANALOG_IN A0 50// soundISR() 51// This function is installed as an interrupt service routine for the pin 52// change interrupt. When digital input 2 changes state, this routine 53// is called. 54// It queries the state of that pin, and sets the onboard LED to reflect that 55// pin's state. 56void soundISR() 57{ 58 int pin_val; 59 pin_val = digitalRead(PIN_GATE_IN); 60 digitalWrite(PIN_LED_OUT, pin_val); 61} 62void setup() 63{ 64 Serial.begin(9600); 65 // Configure LED pin as output 66 pinMode(PIN_LED_OUT, OUTPUT); 67 // configure input to interrupt 68 pinMode(PIN_GATE_IN, INPUT); 69 attachInterrupt(IRQ_GATE_IN, soundISR, CHANGE); 70 // Display status 71 Serial.println("Initialized"); 72} 73void loop() 74{ 75 int value; 76 // Check the envelope input 77 value = analogRead(PIN_ANALOG_IN); 78 // Convert envelope value into a message 79 Serial.print("Status: "); 80 if(value <= 10) 81 { 82 Serial.println("Quiet."); 83 } 84 else if( (value > 10) && ( value <= 30) ) 85 { 86 Serial.println("Moderate."); 87 } 88 else if(value > 30) 89 { 90 Serial.println("Loud."); 91 } 92 // pause for 1 second 93 delay(1000); 94}
Sparkfun Sound Detector
c_cpp
1/****************************************************************************** 2* sound_detector_demo.ino 3* Sound detector sample sketch 4* Byron Jacquot @ SparkFun Electronics 5* February 19, 2014 6* https://github.com/sparkfun/Sound_Detector 7* 8* This sketch demonstrates the use of the Sparkfun Sound Detector board. 9* 10* The Sound Detector is a small board that combines a microphone and some 11* processing circuitry. It provides not only an audio output, but also a 12* binary indication of the presence of sound and an analog representation 13* of it's amplitude. 14* 15* This sketch demonstrates two different modes of usage for the Sound 16* Detector. The gate output (a binary indication that is high when sound 17* is present, and low when conditions are quiet) is used to fire a pin-change 18* ISR, which lights an LED when the sound is present. The envelope output 19* (an analog voltage to rises to indicate the amplitude of the sound) is 20* sampled in the loop(), and it prints an indication of the level to the 21* serial terminal. 22* 23* For more details about the Sound Detector, please check the hookup guide. 24* 25* Connections: 26* The Sound Detector is connected to the Adrduino as follows: 27* (Sound Detector -> Arduino pin) 28* GND → GND 29* VCC → 5V 30* Gate → Pin 2 31* Envelope → A0 32* 33* Resources: 34* Additional library requirements: none 35* 36* Development environment specifics: 37* Using Arduino IDe 1.0.5 38* Tested on Redboard, 3.3v/8MHz and 5v/16MHz ProMini hardware. 39* 40* This code is beerware; if you see me (or any other SparkFun employee) at the 41* local, and you've found our code helpful, please buy us a round! 42* 43* Distributed as-is; no warranty is given. 44******************************************************************************/ 45// Define hardware connections 46#define PIN_GATE_IN 2 47#define IRQ_GATE_IN 0 48#define PIN_LED_OUT 13 49#define PIN_ANALOG_IN A0 50// soundISR() 51// This function is installed as an interrupt service routine for the pin 52// change interrupt. When digital input 2 changes state, this routine 53// is called. 54// It queries the state of that pin, and sets the onboard LED to reflect that 55// pin's state. 56void soundISR() 57{ 58 int pin_val; 59 pin_val = digitalRead(PIN_GATE_IN); 60 digitalWrite(PIN_LED_OUT, pin_val); 61} 62void setup() 63{ 64 Serial.begin(9600); 65 // Configure LED pin as output 66 pinMode(PIN_LED_OUT, OUTPUT); 67 // configure input to interrupt 68 pinMode(PIN_GATE_IN, INPUT); 69 attachInterrupt(IRQ_GATE_IN, soundISR, CHANGE); 70 // Display status 71 Serial.println("Initialized"); 72} 73void loop() 74{ 75 int value; 76 // Check the envelope input 77 value = analogRead(PIN_ANALOG_IN); 78 // Convert envelope value into a message 79 Serial.print("Status: "); 80 if(value <= 10) 81 { 82 Serial.println("Quiet."); 83 } 84 else if( (value > 10) && ( value <= 30) ) 85 { 86 Serial.println("Moderate."); 87 } 88 else if(value > 30) 89 { 90 Serial.println("Loud."); 91 } 92 // pause for 1 second 93 delay(1000); 94}
Ultrasonic Sensor Code
c_cpp
1int trigPin = 11; 2int echoPin = 12; 3 4long duration; 5int distance; 6 7void 8 setup() { 9 10pinMode(trigPin, OUTPUT); 11pinMode(echoPin, INPUT); 12Serial.begin(9600); 13 14 15} 16void loop() { 17 18digitalWrite(trigPin, LOW); 19delayMicroseconds(2); 20 21digitalWrite(trigPin, 22 HIGH); 23delayMicroseconds(10); 24digitalWrite(trigPin, LOW); 25 26duration 27 = pulseIn(echoPin, HIGH); 28distance= duration*0.034/2; 29 30Serial.print("Distance: 31 "); 32Serial.println(distance); 33}
Downloadable files
Simple Sound Detector Schematic
Simple Sound Detector Schematic

Ultrasonic Sensor Schematic
VCC --> 5V Trig --> 11 Echo --> 12 GND --> GND
Ultrasonic Sensor Schematic

Ultrasonic Sensor Schematic
VCC --> 5V Trig --> 11 Echo --> 12 GND --> GND
Ultrasonic Sensor Schematic

Simple Sound Detector Schematic
Simple Sound Detector Schematic

Drawing Sound Detector Schematic
Drawing Sound Detector Schematic

Comments
Only logged in users can leave comments