EsploraSoundSensor

Read the values from the microphone on the Esplora.

Esplora Microphone (Sound Sensor)

This sketch shows you how to read the microphone sensor. The microphone will range from 0 (total silence) to 1023 (really loud). It uses the sound level to set a brightness level for the green LED. When you're using the sensor's reading (for example, to set the brightness of the LED), you map the sensor's reading to a range between the minimum and the maximum.

Hardware Required

  • Arduino Esplora

Circuit

Only your Arduino Esplora is needed for this example. Connect the Esplora to your computer with a USB cable and open the Arduino's Serial Monitor.

Microphone to send data to your computer from the Esplora
Microphone to send data to your computer from the Esplora

Code

To send data to your computer, you need to open a serial connection. use Serial.begin() to open a serial port at 9600 baud on the Esplora.

The Esplora.readMicrophone() function gets the value from the microphone. It will give you a value between 0 and 1023.

To send the values to the Serial Monitor, you call Serial.print(). When the Esplora is connected, and the Serial Monitor is open, you should start to see values reported like this:

1sound level: 172 Green brightness: 42
2sound level: 74 Green brightness: 18
3sound level: 153 Green brightness: 38
4sound level: 67 Green brightness: 16
5sound level: 93 Green brightness: 23
6sound level: 110 Green brightness: 27
1/*
2
3 Esplora Sound Sensor
4
5 This sketch shows you how to read the microphone sensor. The microphone
6
7will range from 0 (total silence) to 1023 (really loud).
8
9 When you're using the sensor's reading (for example, to set the brightness
10
11 of the LED), you map the sensor's reading to a range between the minimum
12
13 and the maximum.
14
15 Created on 22 Dec 2012
16
17 by Tom Igoe
18
19 This example is in the public domain.
20
21 */
22
23#include <Esplora.h>
24
25void setup() {
26
27 // initialize the serial communication:
28
29 Serial.begin(9600);
30}
31
32void loop() {
33
34 // read the sensor into a variable:
35
36 int loudness = Esplora.readMicrophone();
37
38 // map the sound level to a brightness level for the LED:
39
40 int brightness = map(loudness, 0, 1023, 0, 255);
41
42 // write the brightness to the green LED:
43
44 Esplora.writeGreen(brightness);
45
46 // print the microphone levels and the LED levels (to see what's going on):
47
48 Serial.print("sound level: ");
49
50 Serial.print(loudness);
51
52 Serial.print(" Green brightness: ");
53
54 Serial.println(brightness);
55
56 // add a delay to keep the LED from flickering:
57
58 delay(10);
59}

Contribute to Arduino

Join the community and suggest improvements to this article via GitHub. Make sure to read out contribution policy before making your pull request.

Missing something?

Check out our store and get what you need to follow this tutorial.

Suggest Changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. You can read more on how to contribute in the contribution policy.