Netduino home hardware projects downloads community

Jump to content

The Netduino forums have been replaced by new forums at This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.

Zeb Tanner

Member Since 12 Sep 2010
Offline Last Active Jun 18 2014 08:26 AM

Posts I've Made

In Topic: Netduino and Ping))) Ultrasonic Sensor?

14 September 2010 - 01:32 AM

So did it work well?

I have couple of these sensors are on the way, I wonder how accurate/responsive these are, hope I didn't waste my $30.00 just because I went for the cheap ones ($15/piece, upto 3 meters distance).

It will definitely suit my desire to play!

Accuracy is almost certainly limited by the timing loop in my code ... not the sensor itself. Based on observed results, it seems to be in the ballpark of 1000 ticks per iteration which works out to about +- 2cm. By eyeball +- 2cm, feels about right. (I didn't, however, use a ruler.) Results are pretty consistent back-to-back -- no weird numbers jumping out. That'll work for a lot of stuff.

I assume some kind of a firmware level routine to time an input pulse would provide much better accuracy.

I tried, but could not get CW2's idea of using the InterruptPort to more accurately time the pulse. It seems like it would be a better way to do it. HOWEVER, for this sensor, on one pin, the port has to transition from writing a pulse, to receiving the result in time to catch the leading edge of the return pulse.

I tried creating an OutputPort to write the request pulse, calling Dispose() on the OutputPort and creating an InterruptPort to receive the respose. It seemed like I was only catching the trailing edge. If I get a bit of time, I'll play a bit more. Failing that, I suppose it's an excuse to learn how to bang on the firmware. :-)

In Topic: Netduino and Ping))) Ultrasonic Sensor?

13 September 2010 - 05:08 AM

I picked up a Ping ultrasonic sensor (and a few other toys) at Radio Shack tonight.

After a bit of time futzing around with things, I seem to have it working with the code below. The sensor is connected to Ground, +5V, and Digital I/O D7.

The program displays (in the debugger output window) the measured distance, in cm, once every 5 seconds. (If you can't find your debugger output window ... go to the Debug Menu in Visual Studio, and pick Debug->Windows->Output.)

I'm now pointing the sensor at all kinds of random stuff, just to see how well it works.

Much fun.

using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;

namespace NetduinoApplication2
    public class Ping
        private TristatePort _port;

        // Pass in the pin to which the rangefinder is attached.
        public Ping(Cpu.Pin pin)
            _port = new TristatePort(pin, false, false, ResistorModes.Disabled);

        // Returns distance in mm
        public int mm_Distance()

            // First we need to pulse the port ... high, low.  It seems to work fine, without adding any delay.
            _port.Active = true;                // Put port in write mode
            _port.Write(true);                  // Pulse pin

            _port.Active = false;                        // Put port in read mode;    
            bool v1 = false;
            while (v1 == false) { v1 = _port.Read(); }   // Wait for line to go high.
            long tick1 = System.DateTime.Now.Ticks;      // Save start ticks.

            bool v2 = true;
            while (v2 == true) { v2 = _port.Read(); }    // Wait for line to go low.
            long tick2 = System.DateTime.Now.Ticks;      // Save end ticks. 

            int ticks = (int)(tick2 - tick1);
            // Now, just have to convert ticks to mm
            //   Per wikipedia: speed of sound, dry air, 20 deg C, 343 m /s
            //   Per msdn: one .NET "Tick" = 0.1 micro seconds.
            // Which works out to ... 
            // ... 1 mm of travel each 29.15 ticks
            // ... divide by 2 for round-trip ... 58.3 ticks per mm

            return ticks * 10 / 583;

    public class Program
        public static void Main()
            Ping ping = new Ping(Pins.GPIO_PIN_D7);
            while (true)
                int cm = ping.mm_Distance() / 10;
                Debug.Print(cm.ToString() + "cm");

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.