Introduction: Arduino Interactive Sound Sensor

Picture of Arduino Interactive Sound Sensor

We'll be learning how to build an Arduino circuit that takes the input from any sort of analog sensor (vibration sensor, pressure sensor, potentiometer) and use it to output sound from a piezo speaker. This particular example will teach you how to connect a vibration sensor and a piezo to the Arduino, and write the code to output a different frequency of sound based on the value of the sensor, as well as set a threshold for when the sound should start. Without further delay, let's get started!

Step 1: Required Components

You will need...

  • Basic knowledge of how to program an Arduino and how to connect components to it
  • An Arduino (we'll be using an Uno, but any model will work)
  • A piezo speaker
  • A vibration sensor (you can use any sort of analog sensor)
  • A breadboard (optional)
  • Various jumper wires, for hooking things up

Step 2: Writing the Code: Variables and Setup()

Because you're probably already using the computer you use for programming your Arduino, let's start with that. Open up the Arduino IDE and create a new sketch. Make sure you have your setup and loop functions written, so that your code looks something like this.

void setup() {

}

void loop() {

}

First, we need to define a couple of variables to keep track of where our components are connected on the board. Add these to lines to the very top of your code.

int SENSOR_PIN = 0; // Change to your ANALOG PIN for your sensor
int PIEZO_PIN = 11; // Change to your DIGITAL PIN for piezo speaker

We'll be referencing these variables later in our code so we don't have to type the pin number multiple times, and so we can easily change a pin without breaking our code. Next, we will add code to our setup function so that the Arduino knows how we are using our pins. Add this line of code to the setup() function (inside the curly brackets).

pinMode(PIEZO_PIN, OUTPUT);

Here, we are telling the Arduino that we intend to use the pin the piezo is connected to as an output. Pins are in input mode by default, so we don't need to specify this for our sensor pin.

Step 3: Writing the Code: Loop()

Next, add the following line of code to the loop() function (inside the curly brackets).

tone(PIEZO_PIN, map(analogRead(SENSOR_PIN), 400, 1023, 300, 1500), 100);

If you don't code very often, the first line of code should look pretty frightening to you. Not to worry though, it's actually pretty simple. Let's look at it from the outside in.

tone(PIEZO_PIN, FREQUENCY, 100);

Tone is the function that output sounds to your piezo. It needs three arguments or parameters - the pin number, (PIEZO_PIN), the frequency (FREQUENCY), and the length of the sound in milliseconds (100). We're telling this function to output a sound to the device connected to pin PIEZO_PIN, with a frequency made by some code we'll discuss later, for 100 milliseconds. Let's go deeper now.

map(VALUE, 400, 1023, 300, 1500)

We're choosing the frequency for the tone function using Map. Map takes a value and adjusts it another value. It takes five arguments or parameters - the value to map (VALUE), the lower limit of what that value should be (400), the upper limit of what that value should be (1023), and lowest value to map to (300), and the highest value to map to (1500). We'll be plugging our sensor value into VALUE, and the possible values we can get are 0 through 1023. We'll choose the range of 400 to 1023 to set a bit of a threshold - any value below 400 will be ignored. We want the lowest frequency we output to be about 300, and the highest to be about 1500. All together, what our map function is saying is "take the value from our sensor, which should be between 400 and 1023, then adjust that to be between 300 and 1500." You can adjust any of these values to your liking to better work with your project. Let's go one level deeper and finish off this line of code.

analogRead(SENSOR_PIN);

All we're doing here is reading the analog value from our sensor pin. The pin takes one argument or parameter, and that is the number of the pin to read (SENSOR_PIN). It's as simple as that! Now, lets add this final line of code to the end of our loop() function (inside the curly brackets).

delay(200);

All we do here is stop and wait for 200 milliseconds before doing the whole thing over again. This serves three functions: to allow our tone to play, to add a bit of separation to our tones, and to read the sensor at a reasonable rate, giving it plenty of time to change its value.

Step 4: Writing the Code: Finished Code

Your finished code should look something like this. Make sure you've written everything correctly and set your pin numbers to the pins you want to use.

int SENSOR_PIN = 0;
int PIEZO_PIN = 11;

void setup(){
	pinMode(PIEZO_PIN, OUTPUT);
}

void loop(){
	tone(PIEZO_PIN, map(analogRead(SENSOR_PIN), 400, 1023, 300, 1500, 100);
	delay(200);
}

Now, we are ready to upload this code to the Arduino. Plug your Arduino into your computer, select your port and board, then click the Upload button. Disconnect your Arduino before moving on to the next step.

Step 5: Connecting the Hardware

Now it's time to connect the hardware. There's not a lot to do here, as we only have two components we need to connect, and neither component is polarized. Make the following connections between your components and the Arduino.

Sensor

  • One leg to +5V
  • One leg to A0 (or whatever analog pin you set in your code)

Piezo Speaker

  • One leg to GND
  • One leg to D11 (or whatever digital pin you set in your code)

And that's it! Power up your Arduino and test it out - the greater the value on the sensor, the higher pitched the sound from the speaker should be. You may need to tweak some of the values in the code to work best with your particular sensor.

One final note - when using sensors like the vibration sensor we used here, the material your sensor is on will have a profound effect on how it works. The vibration sensor will work very differently on a piece of foam compared to a hard, solid surface. We found that taping our sensor to the top of a makeshift drum makes it super sensitive, and it yielded some pretty cool results. Be creative, and see what kind of things you can come up with!

Comments

Build_it_Bob (author)2016-03-28

Nicely written; thanks for sharing.

Xarmin (author)Build_it_Bob2016-03-28

Thanks!

wold630 (author)2016-03-24

Thanks for sharing your knowledge!

About This Instructable

1,306views

7favorites

License:

More by Xarmin:Arduino Interactive Sound Sensor
Add instructable to: