Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Microphone Detection in Arduino / Galileo (IoT) Using VC++

DZone's Guide to

Microphone Detection in Arduino / Galileo (IoT) Using VC++

· IoT Zone
Free Resource

After setting up Intel Galileo in our last post, let's get going with the first sensor – Microphone. I had to refresh some of the basics that I had learnt during my bachelor studies - yes I did my undergraduate engineering studies in Automation and I've played with different microprocessors, controllers and sensors. So this post is going to be about voice detection using Microphone detector and pulsating LED when voice crosses few decibels.

Basics first, the wiring

You need a Galileo board and an Arduino compatible shield that can help you wire your sensors in a clean way. So with the shield, your board will look like

Now you need 2 different Grove sensors for this. Ideally, you can use sensors of any brand with any IoT device. All you need to remember is that all sensors will have minimum 2 pins

  • Voltage – Often abbreviated as V or VCC
  • Ground – Often abbreviated as GND
  • Data Pins – Often abbreviated as Dx (where x is a number)
  • Not connected Pins – Often abbreviated as NC

A point to remember is that you always have to connect V/VCC with another V/VCC and GND with another GND on any board. If you connect otherwise, your circuit will not be complete (and current will not flow).

When you are using an "Analog" sensor that will provide you some data, you will have a pin that says OUT. This OUT pin will have a voltage signal that will represent the signal captured by your sensor. This may not make perfect sense at first go. So let us go a bit deeper. There are 2 types of sensors – Analog ones that provide signals back in Voltage form and Digital ones that provide signals in bit/byte form. A weighing scale uses a sensor that can be analog or digital.

Any signal measured in analog format will require some calibration i.e. a conversion mechanism to digital or the other way.

Microphone Sensor and LED kit

A microphone sensor has 4 pins – VCC, GND, NC and OUT. You will get the voltage as sensor signal in the OUT pin

A LED sensor kit has 4 pins as well – VCC, GND, NC, SIG. You can set 5V on the SIG pin to light up the LED and can set 0V to SIG pin to light it off

So essentially what we are planning to do is to get the OUT signal of microphone into the SIG pin of LED kit. Ideally, you do not need a powerful processor like Galileo for such a trivial work. You can do this with few electronics fundamentals. But considering that you want to build something more sophisticated and this is the first step, we can go through the rest of the tutorial.

Setting up the sensor and the kit

I've setup Microphone sensor on A0 (as INPUT) and LED sensor kit on D3 (as OUTPUT) of the shield. You can use any other ports of your choice. Next is opening up VS 2013 and creating a new project of type Visual C++ > Windows for IoT

And in the main.cpp, you can paste the below code

#include "stdafx.h"
#include "arduino.h"

#define MICROPHONE A0
#define LED D3
#define THRESHOLD_VALUE 450

void pins_init()
{
	pinMode(LED, OUTPUT);
	pinMode(MICROPHONE, INPUT);
}
void turnOnLED()
{
	digitalWrite(LED, HIGH);
}
void turnOffLED()
{
	digitalWrite(LED, LOW);
}

int _tmain(int argc, _TCHAR* argv[])
{
	return RunArduinoSketch();
}

void setup()
{
	Serial.begin(9600);
	pins_init();
}

void loop()
{
	int sensorValue = analogRead(MICROPHONE);
	Serial.print("sensorValue");
	Serial.println(sensorValue);

	if (sensorValue > THRESHOLD_VALUE)
	{
		Log("OK, got something worth listening\n");
		turnOnLED();
		delay(2000);
	}

	turnOffLED();
}

Understanding the Code

#define THRESHOLD_VALUE 450

The above statement is a digital value for the sound threshold. A microphone captures analog signal (0-5V) which is provided to your Galileo in form of a digital signal (0-1024). This means 0v = 0 in digital and 5v = 1024 in digital. To eliminate the environmental sounds, I prefer a threshold to be at least 33% i.e. 2v. So a digital value of 450, converts to 2.19v (= 450* 5 / 1024). At my place, I found that environmental sounds where contributing to a value of 291 (i.e. 1.42v)

The next important bits are the port definitions,

pinMode(LED, OUTPUT);
pinMode(MICROPHONE, INPUT);

Here, we have directed that we will take input from A0 and output the data to D3. Now let's understand the core of our program – the loop function

We are reading the analog value of microphone sensor using below code which converts the analog value into digital number

int sensorValue = analogRead(MICROPHONE);

When this value goes beyond the defined threshold, you want to send a 5v to LED (by sending a HIGH bit) using code

digitalWrite(LED, HIGH);

When you play some loud music you will see that the LED light will lighten-up for 2 seconds (delay=2000ms) and will turn off.

When you run/execute this project from Visual Studio using Remote Debugger, VS will deploy this code to your Galileo device. You will be prompted for your Galileo user name and password.

You can say something aloud or play some video on YouTube to test this functionality.

This code is also available on GitHub at:https://github.com/punitganshani/ganshani/tree/master/Samples/IntelGalileo/GroveMic


Topics:

Published at DZone with permission of Punit Ganshani, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}