Netduino home hardware projects downloads community

Jump to content


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

Parallax - Ultrasonic Distance Sensor does not work


  • Please log in to reply
3 replies to this topic

#1 EricO

EricO

    Member

  • Members
  • PipPip
  • 17 posts
  • LocationLas Vegas

Posted 16 October 2013 - 10:59 PM

I have an NP2 connected to a Parallax BOE Shield and a Parallax Ping Ultrasonic Distance Sensor. The ping sensor is connected to the BOE shield using the 5V pin and the Gnd pin right next to it, I've tried multiple I/O pins but no success with the ping sensor.  To ensure the ping sensor was working, I connected it to another Parallax board I have and it worked fine. Below, is the code I'm using for the NP2, I found it on this forum.  I've read where some have had success with it and others have not. Not knowing what to try next, I swapped out the NP2 with an Arduino uno and left the ping sensor exactly how I had it connected on the BOE shield.  It worked with no problems.

 

I'd still like to give the NP2 a chance but I don't know what to try next. Suggestions are welcome.

 

Thank you.

 

_________________________

 

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

namespace ParallaxSensors {   /******************************************************************* * Reference the following DLLs *  - Microsoft.SPOT.Hardware *  - Microsoft.SPOT.Native *  - SecretLabs.NETMF.Hardware *  - SecretLabs.NETMF.Hardware.Netduino ******************************************************************/

  /// <summary>   /// Holds the distance information for the event   /// </summary>   public class PingEventArgs   {   public PingEventArgs(int distance)   {   Distance = distance;   }

  public int Distance { get; set; }   }

  /// <summary>   /// Implements a timer based distance measurement of distance (in cm) using the   /// parallax Ping sensor   ///   /// Example usage:   /// public static void Main()   /// {   /// Ping ping = new Ping(Pins.GPIO_PIN_D2, 1000, true);   /// ping.RangeEvent += new Ping.RangeTakenDelegate(ping_RangeEvent);   ///   /// // Sleep forever!   /// Thread.Sleep(Timeout.Infinite);   /// }   ///   /// static void ping_RangeEvent(object sender, PingEventArgs e)   /// {   /// Debug.Print("Range: " +  e.Distance + " cm");   /// }   /// </summary>   public class Ping   {   public delegate void RangeTakenDelegate(object sender, PingEventArgs e);   public event RangeTakenDelegate RangeEvent;

  private TristatePort _port;   private ExtendedTimer _timer;   private int _period;   private bool _enabled;

  /// <summary>   /// Constructor: initializes the Ping class   /// </summary>   /// <param name="pin">which pin the sensor is connected to</param>   /// <param name="period">time between pulses in millisec, minimum of 1000</param>   /// <param name="enabled">if true, start pinging immediately, otherwise wait for enable</param>   public Ping(Cpu.Pin pin, int period, bool enabled)   {   _port = new TristatePort(pin, false, false, ResistorModes.Disabled);

  // Initially set as disabled.   _timer = new ExtendedTimer(TakeMeasurement, null, Timeout.Infinite, period);

  // Store the current period   Period = period;

  // Set the enabled state   Enabled = enabled;   }

  /// <summary>   /// Enable or disable the timer that triggers the read.   /// </summary>   public bool Enabled   {   get { return _enabled; }   set   {   _enabled = value;

  _timer.Change((_enabled) ? 0 : Timeout.Infinite, Period);   }   }

  /// <summary>   /// Set the period of pings, min is 1000ms   /// </summary>   public int Period   {   get { return _period; }   set   {   _period = value;   if (_period < 1000)   _period = 1000;

  // Set enabled to the current value to force update   Enabled = _enabled;   }   }

  /// <summary>   /// Get distance in cm   /// </summary>   /// <returns>distance in cm, based on wikipedia for dry air at 20C</returns>   private int GetDistance()   {

  // First we need to pulse the port from high to low.   _port.Active = true; // Put port in write mode   _port.Write(true); // Pulse pin   _port.Write(false);   _port.Active = false;// Put port in read mode;  

  bool lineState = false;

  // Wait for the line to go high, for start of pulse.   while (lineState == false)   lineState = _port.Read();

  long startOfPulseAt = System.DateTime.Now.Ticks;   // Save start ticks.

  // Wait for line to go low.   while (lineState)   lineState = _port.Read();

  long endOfPulse = System.DateTime.Now.Ticks;   // Save end ticks.

  int ticks = (int)(endOfPulse - startOfPulseAt);

  return ticks / 580;   }

  /// <summary>   /// Initiates the pulse, and triggers the event   /// </summary>   /// <param name="stateInfo"></param>   private void TakeMeasurement(Object stateInfo)   {   int distance = GetDistance();

  if (RangeEvent != null)   {   RangeEvent(this, new PingEventArgs(distance));   }   }   } }



#2 perpetualKid

perpetualKid

    Member

  • Members
  • PipPip
  • 20 posts

Posted 17 October 2013 - 11:39 AM

is this the Parallax Ping))) sensor with 3pins only? I'm using below code and get about 5mm resolution.

using System;using System.Threading;using Microsoft.SPOT;using Microsoft.SPOT.Hardware;namespace Netduino.Toolbox.Hardware{    public class UltraSonic : IDisposable    {        private TristatePort port;        private Stopwatch watch;        private readonly double soundSpeed;        public UltraSonic(Cpu.Pin channel, short temperature = 18)        {            this.port = new TristatePort(channel, false, false, Port.ResistorMode.Disabled);            this.watch = Stopwatch.StartNew(false);            soundSpeed = 10000 / (331.4 + 0.6 * temperature);        }        public int ReadDistance()        {//            watch.Reset();            port.Active = true;            port.Write(true);            port.Active = false;            while (!port.Read()) ;             watch.Start();            while (port.Read());            watch.Stop();            if (watch.ElapsedMicroseconds >= Const.UltraSonicReadTimeout)                return -1;            return (int)((watch.ElapsedTicks >> 1) / soundSpeed);        }        ~UltraSonic()        {            Dispose();        }        public void Dispose()        {            if (port != null)            {                port.Dispose();                port = null;            }        }    }}

Comparing with your code I think the issue is you set the port to high using Write(true) and set to low again Write(false) before you start reading. Therefore the port is always low when you read, however you want it to become low as a function over time.



#3 EricO

EricO

    Member

  • Members
  • PipPip
  • 17 posts
  • LocationLas Vegas

Posted 17 October 2013 - 05:16 PM

Thank you for the reply.  Yes, it is the Parallax Ping))). I suspect you're write about the port settings, looking at my Arduino code, which does work, I notice they handle the port quite a bit differently than the code I was using.  At any rate, I'll give your code a try and let you know.

 

Thank you again.



#4 EricO

EricO

    Member

  • Members
  • PipPip
  • 17 posts
  • LocationLas Vegas

Posted 18 October 2013 - 08:06 PM

I tried your code, but no success.  Now it looks like my NP2 is bricked anyway so I guess won't  be going any further with it.

 

Thank you again for your assistance.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

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.