Introduction: RGB LED Sound Level Detector

This is the seventh module to the series of Arduino 101 tutorials I've been writing.

In this project we will use a sound detector module from SparkFun and depending on the sound levels it is detecting, change the colour of an RGB LED, so you can tell how loud or quiet you have been. You can also use it to make the classic light clapper.

Step 1: Tools and Materials

Step 2: Circuit

Firstly, we will connect the power from the Arduino board

  • Connect the 3.3V pin of the Arduino board to the red power rail on the bread board with a red jumper wire
  • Connect the GND pin of the Arduino board to the black power rail on the bread board with a black jumper wire.

Secondly, we will wire the RGB LED.

  • Place the RGB LED anywhere on the bread board with all it's legs in different pin holes.
  • The longest leg is the ground so connect this to the black power rail on the breadboard.
  • For the remaining three pins connect the 100Ω resistors to each one of them.
  • Connect the ends of these resistor starting from the one closest to the ground to pins 5, 6, and 9 on the Arduino board, respectively.

Lastly, we will connect the wires to the sound detector module.

  • Place the Sound detector module in a place away from the RGB LED on the breadboard.
  • Connect the VCC of the sound detector module to the red power rail on the breadboard.
  • Connect the GND of the sound detector module to the black ground rail on the breadboard.
  • Connect the pin called "envelope" from the sound detector module to the A0 pin of the Arduino

Step 3: Coding

//pin variables

const int redPin = 5; const int greenPin = 6; const int bluePin = 9; const int soundPin = 0;

//variables for storing raw sound and scaled value int sound; int scale;

void setup() { //start the serial port a@ 9600bps Serial.begin(9600); //set RGB pins to OUTPUT pinMode(redPin, OUTPUT); pinMode(greenPin, OUTPUT); pinMode(bluePin, OUTPUT); }

void loop() { //read and store the audio from Envelope pin sound = analogRead(soundPin); //map sound which in a quiet room a clap is 300 //from 0 to 3 to be used with switch case scale = map(sound, 0, 300, 0, 3);

//print values over the serial port for debugging Serial.print(sound); Serial.print(" "); Serial.println(scale);

//switch case on scaled value switch (scale) { //if 0 RGB = Blue case 0: digitalWrite(redPin, LOW); digitalWrite(greenPin, LOW); digitalWrite(bluePin, HIGH); break; //if 1 RGB = Green case 1: digitalWrite(redPin, LOW); digitalWrite(greenPin, HIGH); digitalWrite(bluePin, LOW); break; //if 2 RGB = Yellow case 2: digitalWrite(redPin, HIGH); digitalWrite(greenPin, HIGH); digitalWrite(bluePin, LOW); break; //if 3 RGB = Red case 3: digitalWrite(redPin, HIGH); digitalWrite(greenPin, LOW); digitalWrite(bluePin, LOW); break; //default off default: digitalWrite(redPin, LOW); digitalWrite(greenPin, LOW); digitalWrite(bluePin, LOW); break; } }

Step 4: Demo

The colour of the LED changes depending on how loud the environment is, with red being the loudest and no light being very quiet.