Microphone Sensor

Microphone Sensor

Hey guys, for this tutorial i’m going to show you how to set up a microphone sensor.  The microphone sensor is just like any other sensor in the fact that it takes some sort of external stimuli and turns it into something that the computer can read.  For the microphone sensor this external stimuli is sound.  Basically, rather than a button this sensor will allow you to control something using a clap, a door slamming, or even a dog bark.

Setup:

There are 2 ways to set up this microphone sensor, one is by using the analog sensor and the other is the digital.  For the sake of this tutorial we are going to use the analog signal. Besides the analog pin, the microphone sensor only needs to be connected to power and ground.  

For a visual I also set up a LED to pin 13 so that we can see if we are effectively using the microphone sensor.  Connecting this LED is optional but I always prefer to have some sort of visual outside of the serial monitor.

Once you get the setup together you can move onto the code section and we can get this working!

Code:

Take a look over the code briefly and see how many of the functions you recognize.

As you can see this is a very basic code that reads an analog signal then and checks to see if it reaches a threshold.  If it does then it adds 1 to ‘lightState’.  We then check ‘lightState’ to see if it is divisible by 2.  This means that if the threshold was reached 5 times and we divide 5 by 2, we will have a remainder of 1: this will turn the light on.  On the other hand, if you clap one more time and the lightState is 6, the remainder will be 0 and the light will turn off.  

Once you have your system set up and coded, check out the challenge and see if you can figure it out!

Challenge:

For this tutorial i used a modulus function that checks if a number is divisible by 2, but you could also do something similar with a boolean function.  Can you figure this out?

Components:

1X Arduino Compatible Board

1X Female – Male Jumper Cables

1X LED

1X sound module

 

This sound module requires a connection to 5v, ground, and either the analog out or the digital out.  For sake of this tutorial we will only be going over the analog signal.  Once you have it wired up as show below take a step into the code following the diagram

 

arduino sketch

 

Code:

We will be utilizing very basic functions such as digitalWrite and analogRead

 

#define input A0 //defining the analog pin 
#define light 13 //defining digital pin for LED

int lightState=0;//used for on and off

void setup() {
  pinMode(input,INPUT); //defines the analog pin as an input
  pinMode(light,OUTPUT);//defines digital pin as an output
  Serial.begin(9600);
}

void loop() {
if (analogRead(input)>275){ //checks to see if light reached a threshold
    lightState++; //increments light integer
    digitalWrite(light,lightState%2);//if devisable by 2 light off if not light on
  }
  Serial.println(lightState%2);//print to screen for trouble shooting
  
}

corra02

Leave a Reply

Your email address will not be published. Required fields are marked *