Sound Phidget

The Sound Phidget measures sound pressure level. The Sound Phidget returns loudness (sound pressure level) in decibels (symbol: dB)​

A typical vacuum will be around 80dB, whereas a typical Library will be around 40bB. The Sound Phidget can also be used to measure sound frequency.

See tutorial

Setup

Before you do any coding you will have to attach your Sound Phidget to your VINT Hub as shown:

Code (Java)

Create a file called Sonar and insert the following code. Run your code. Clap your hands to see a dB change.

Not your programming language? Set my language and IDE.

  
package sound;

//Add Phidgets Library
import com.phidget22.*;

public class Sound {

    public static void main(String[] args) throws Exception {

        //Create
        SoundSensor soundSensor = new SoundSensor();

        //Open
        soundSensor.open(1000);

        //Use your Phidgets
        while (true) {
            System.out.println("Sound pressure level:  " + soundSensor.getdB() + " dB");
            Thread.sleep(250);
        }
    }
}
  
  
//Add Phidgets Library
import com.phidget22.*;

public class Sound {

    public static void main(String[] args) throws Exception {

        //Create
        SoundSensor soundSensor = new SoundSensor();

        //Open
        soundSensor.open(1000);

        //Use your Phidgets
        while (true) {
            System.out.println("Sound pressure level:  " + soundSensor.getdB() + " dB");
            Thread.sleep(250);
        }
    }
}
  
  
//Add Phidgets Library
import com.phidget22.*;

//Define
SoundSensor soundSensor;

void setup(){
  try{
    
    //Create 
    soundSensor = new SoundSensor();

    //Open
    soundSensor.open(1000);
    
  }catch(Exception e){
    e.printStackTrace();
  }
}

void draw(){
  try{
    
    //Use your Phidgets
    println("Sound pressure level:  " + soundSensor.getdB() + " dB");
    delay(250);
    
  }catch(Exception e){
    e.printStackTrace();
  }
}
  

Code (Python)

Create a file called Sonar and insert the following code. Run your code. Clap your hands to see a dB change.

Not your programming language? Set my language and IDE.

  
#Add Phidgets Library
from Phidget22.Phidget import *
from Phidget22.Devices.SoundSensor import *
#Required for sleep statement
import time
 
#Create
soundSensor = SoundSensor()
 
#Open
soundSensor.openWaitForAttachment(1000)
 
#Use your Phidgets
while (True):
    print("Sound pressure level: " + str(soundSensor.getdB()) + " dB")
    time.sleep(0.25)
  

Code (C#)

Create a file called Sonar and insert the following code. Run your code. Clap your hands to see a dB change.

Not your programming language? Set my language and IDE.

  
//Add Phidgets Library
using Phidget22;

namespace Sound
{
    class Program
    {
        static void Main(string[] args)
        {

            //Create
            SoundSensor soundSensor = new SoundSensor();

            //Open
            soundSensor.Open(1000);

            //Use your Phidgets
            while (true)
            {
                System.Console.WriteLine("Sound pressure level: " + soundSensor.dB + " dB");
                System.Threading.Thread.Sleep(250);
            }
        }
    }
}
  

Code (Swift)

Create a file called Sonar and insert the following code. Run your code. Clap your hands to see a dB change.

Not your programming language? Set my language and IDE.

  
class ViewController: NSViewController {
 
	@IBOutlet weak var sensorLabel: NSTextField!
   
	//Create
	let soundSensor = SoundSensor()
    
	override func viewDidLoad() {
    	super.viewDidLoad()
    	do{
        	//Subscribe to event
        	let _ = soundSensor.SPLChange.addHandler(onSPLChange)
           
        	//Open
        	try soundSensor.open()
    	}catch{
        	print(error)
    	}
	}
   
	func onSPLChange(sender:SoundSensor, data: (dB: Double, dBA: Double, dBC: Double, octaves: [Double])){
    	DispatchQueue.main.async {
        		//Use information from your Phidget to change label
    	    self.sensorLabel.stringValue = String(data.dB) + " dB"
    	}
	}
}
  

Applications

Sound Sensors are commonly used in security systems and home automation. For example, if a loud noise is detected lights will turn on. Sound engineers use sound sensors to balance audio or measure sound levels in an auditorium for perfect live performances. Sound sensors may also be used in manufacturing or other loud environments to determine what personal protective equipment (for example, earplugs) should be worn, or if physical sound barriers need to be installed.

Practice

Using your Getting Started Kit and Sound Phidget, you can simulate a Clapper light system.

  1. Replace your Humidity Phidget with the Sound Phidget.
  2. Write a program that uses the Sound Phidget to detect 2 consecutive claps. (You will need to determine how many decibels a clap is).

Check out the advanced lesson Using the Sensor API before you use the API for the first time.

API
Troubleshoot
  1. Make sure everything is plugged in properly and your VINT Hub is connected to your computer
  2. Stop running other programs that are using your Phidgets before running a program.

If these do not solve your issue visit the Advanced Troubleshooting Page.

What are Phidgets?

Phidgets are programmable, modular USB devices, either sensors or controllers that you can connect together. Simply write code in your favorite language and solve real-world problems.

Learn more

Set your preferences

Windows

Mac OS

Raspberry Pi

Java

Python

C#

Swift

NetBeans

Processing

Eclipse

Thonny

PyCharm

PyScripter

Visual Studio

Xcode

Setting your preferred operating system, programming language and environment lets us display relevant code samples for the Getting Started Tutorial, Device Tutorials and Projects

Done