I had been thinking about an Ultrasonic Tape measure as one of the projects for code club. One of the “challenges” we start each evening with was measuring how long the Netduino on board button was pressed using an InterruptPort triggering on both the leading and trailing edges. This challenge implements the core of the code required to use an Ultrasonic Ranger.
Ultrasonic Ranger Test Rig bill of Materials (April 2014)
- Netduino Plus 2 USD59.95/NZD108.25 or Netduino 2 USD32.95 or NZD59.63
- Ultrasonic Ranger USD2.80 or NZD7.00
- Mini breadboard USD1.50 or NZD5.00
A plug and play option would be
To make a measurement the trigger pin is strobed high, then the duration of the pulse on the echo pin represents the distance from the object. The NetMF DataTime structure represents tick as one hundred nanoseconds or one ten-millionth of a second. (There are 10,000 ticks in a millisecond) so the duration in ticks/58 is the distance in millimetres.
public UltraSonicRanger(Cpu.Pin triggerPin, Cpu.Pin echoPin)
{
#region Diagnostic Assertions
Debug.Assert(echoPin != Cpu.Pin.GPIO_NONE);
Debug.Assert(triggerPin != Cpu.Pin.GPIO_NONE);
Debug.Assert(echoPin != triggerPin);
#endregion
triggerOutput = new OutputPort(triggerPin, false);
echoInterrupt = new InterruptPort(echoPin, true, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeBoth);
echoInterrupt.OnInterrupt += new NativeEventHandler(echoInterruptPort_OnInterrupt);
echoInterrupt.DisableInterrupt();
}
void echoInterruptPort_OnInterrupt(uint data1, uint data2, DateTime time)
{
if (data2 == 0) // falling edge, end of pulse
{
pulseWidthTicks = time.Ticks - pulseStartTicks;
measurementComplete.Set();
}
else
{
pulseStartTicks = time.Ticks;
}
}
Ultrasonic Ranger module source
I was pleasantly surprised by the sub 0.5 cm accuracy of the sensor

When I put the GND in Netduino, the board turn off
Hi,
What model sensor were you using, HC-SR04 ?
Could you post a photo of your Netduino & wiring so I can see if I can spot what is wrong?
Bryn