Netduino + Sonar + WP8 + Bluetooth – Controlling sound with your mind


OK, maybe not with your mind, but at the very least, your hand.

The objective here was to add a sonar (ultrasonic) sensor to the Netduino, work out how close my hand was, and then send that info over Bluetooth to Windows Phone 8 and display it. Bonus points for doing something useful with the data (yes, controlling the volume of the Imperial March is totally useful!).

What you need for this is:

  • Netduino or variant
  • Sonic/sonar sensor
  • Bluetooth Module
  • Windows Phone 8

I’m not going to explain the Bluetooth stuff, so checkout these previous posts:

Controlling a Netduino over Bluetooth with WP8
More Netduino + WP8 + Bluetooth fun- 3D reconstruction

A few months ago I bought this Sonar sensor: HC-SR04 ( I got it from a random little online Chinese shop for $4. I literally just bought it to see if they would actually deliver (free worldwide) because they have quite a big range of Arduino/Netduino sensors and it’s hard to get things locally. Surprisingly it actually came.

So last night I opened it, tweeted, and plugged it into the Netduino:

VCC   -  5v
Trig   -  D6
Echo  – D7

This site has a great explanation about how these work:

They have two transducers, basically a speaker and a microphone.
Ultrasound is a high frequency sound (typically 40 KHz is used). A short burst of sound waves (often only 8 cycles) is sent out the “Transmit” transducer (left, above). Then the “Receive” transducer listens for an echo. Thus, the principle of ultrasonic distance measurement is the same as with Radio-based radar.

After understanding it I started coding a little helper to do the math for me, and realized that someone would have done this before – and proceeded to find this perfect little class by J Wilson on the Netduino forums:
Download that class and add it to your Netduino project.

One thing I didn’t like about his example usage was it blocked up the main thread. So I used it like below which will keep the main thread open for you use while updating in the background:

What’s happening there is that first I start a new background thread which will constantly loop, updating a class variable (in case you need it from the main thread) and then I connect the Bluetooth adapter just like in the previous post.
You can look inside the HC_SR04 class for a better understanding of how it works – but basically, you send out a ping, wait for an interrupt, then see how long it took. You can then convert that time into distance. Occasionally a value of just over 600 came through, so I just ignore everything above 400.

Something to note is that everywhere in the world is going to give slightly different values due to atmospheric differences. So once you have the sensor working, stick a ruler alongside and measure how accurate the values coming back are. In my example above you can see that I’ve added a method called TicksToCm into J Wilsons class. He only had Inches. Here is that method:

If the sensor is reporting incorrect values, keep adjusting the cmConversion/inchConversion variable until they are correct. That variable is basically a multiplier and actually not specifically tied to an actual CM or Inch. I eventually got mine to be accurate to around half a CM. Although I didn’t really test going further than one Metre.

Next up is the WP8 part.

Create a new WP8 project just like before and add the Bluetooth stuff like in the first post.
Then I have a very simple UI. It’s got a ProgressBar to visualize the volume, a MediaElement to play the song, and some TextBlocks to show the current value.

This would have probably been a good time to show a very basic use of MVVM, but I will leave that for another day.

Besides the Bluetooth code, there is really only one method that does all the work here, and it’s really simple. This method is called when a message is received over BT. The message in this case will be the CM distance value from the sonar sensor:

First try cast it to a double. If that works update the filter. The filter used here is the same one as in all the previous posts and just smoothes out the values. Next change the TextBlock to display the right value, and convert the value to a double from 0 to 1.0 to represent volume. Here I am dividing the CM by 30 to essentially mean that 0CM the sound will be off, and 30CM the volume will be full. I found that it was impossible to hear the sound at lower volumes so I add 0.3 to bump it up a bit. Finally update the ProgressBar.

And there you have it – a few lines of code later and you have a working mind hand control device using your Netduino and sexy Windows Phone!

If you’ve got question you can ask me here or on the Twitter machine: @roguecode

WP8 and Netduino solution: Download

p.s. For copyright reasons I’ve removed the Imperial March MP3 from the solution.

  • Guest

    wow Matt this is super amazing man.

  • Sbangani

    Wow Matt this is super Amazing.