Skip to content

5. Connecting an Audio Sensor

Armand Emamdjomeh edited this page Sep 24, 2015 · 7 revisions

Now we're getting somewhere! We've used some simple code to make an LED blink, but how do we make an audio sensor? First up, unplug your resistor, LED and wires. Then, grab the mic and a colored wire.

  1. Plug the mic into the breadboard so each pin is on a different numbered row.
  2. Connect a wire from the Arduino 3.3V pin to the Vcc/Vdd pin on the mic.
  3. Connect a wire from the Analog 0 pin on your Arduino to the "out" pin on your mic.
  4. Connect the "GND" pin on the mic to a ground pin on the Arduino.
  5. Connect the 9V wall adapter to your Arduino. The wifi board requires more power than your USB port can supply.
    /*
      ReadAnalogVoltage
      Reads an analog input on pin 0, converts it to voltage, and prints the result to the serial monitor.
      Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.
     
      This example code is in the public domain.
     */

    // the setup routine runs once when you press reset:
    void setup() {
      // initialize serial communication at 9600 bits per second:
      Serial.begin(9600);
    }

    // the loop routine runs over and over again forever:`
    void loop() {
      // read the input on analog pin 0:
      int sensorValue = analogRead(A0);

      // Convert the analog reading (which goes from 0 - 1023)
      // to a voltage (0 - 3.3V):
      float voltage = sensorValue * (3.3 / 1023.0);

      // print out the value you read:
      Serial.println(voltage);
    }

That's it! We're powering the mic, and it's sending a signal to the analog input for the Arduino. We're going to print out the voltage to the mic using this code: check it out

Copy the code into your IDE and upload it to your Arduino. Click tools > serial monitor (or ctrl/cmd + shift + m) to check out the output.

But what's this code doing?

Setup runs once, at the start of the program. Usually, variables are initialized and values set. In this function, we simply start the Serial object, which is helpful for debugging.

In the loop function, the A0 pin reads an input from the audio sensor, which is on a scale from 0-1023 and converts it to a voltage, between zero and three volts. It then prints this to the serial monitor.

Pretty simple, right?

Next up: Name our robots »