Ultrasonic Ranger Distance Measurement

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 connected to Netduino Plus 2

Ultrasonic Ranger Test Rig bill of Materials (April 2014)

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

2 thoughts on “Ultrasonic Ranger Distance Measurement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s