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

Troubles with sonar sensors

Parallax Ping HC-SR04 Sonar Sensor

  • Please log in to reply
5 replies to this topic

#1 rubenhak

rubenhak

    Member

  • Members
  • PipPip
  • 10 posts

Posted 14 May 2013 - 10:56 PM

Hi Everybody,

 

I'm having troubles with making the sonar sensors work. I used both Parallax Ping and HC-SR04, but neither of those worked well.

 

The problem with HC-SR04 is that I'm always getting -1 as result. The code is from this library: http://forums.netdui...ic-rangefinder/

Tried connecting to different digital pins but it didnt work either. Powering from 5V pin of the netduino. Is there anything I'm missing?

 

The Parallax Ping works "sometimes".  If it works i get correct distance result. The trouble with this one is that sometimes it gets stuck in following loop:

while (lineState)lineState = _port.Read();

Using this code:

http://forums.netdui...rasonic-sensor/

 

Are those sensors sensitive to the power source? Running from USB. 

 

Any help is highly appreciated.

 

Thansk,

Ruben

 

P.S. I use Netduino Plus 2 with latest firmware 4.2.2.

 

 

 

 



#2 baxter

baxter

    Advanced Member

  • Members
  • PipPipPip
  • 415 posts

Posted 15 May 2013 - 01:02 AM

I modified the code from here,

http://forums.netdui...asures-jumping/

 

and the files are here for Netduino Plus 2 (in VB),

https://www.dropbox....0a7Geue?v=0mwng

 

The cheap HC-SR04 modules are spotty at best. I wasted a lot of time with a bad one. The above code works just fine. I merely added some smoothing routines. The module is powered from the Netduino 5V pin.

 



#3 rubenhak

rubenhak

    Member

  • Members
  • PipPip
  • 10 posts

Posted 15 May 2013 - 01:14 AM

I modified the code from here,

http://forums.netdui...asures-jumping/

 

and the files are here for Netduino Plus 2 (in VB),

https://www.dropbox....0a7Geue?v=0mwng

 

The cheap HC-SR04 modules are spotty at best. I wasted a lot of time with a bad one. The above code works just fine. I merely added some smoothing routines. The module is powered from the Netduino 5V pin.

 

Thank you, I will try to use and see how it works.

Are there any limitations on the pins which I can use?



#4 rubenhak

rubenhak

    Member

  • Members
  • PipPip
  • 10 posts

Posted 15 May 2013 - 07:36 PM

Still didn't work. If I use PIN4 it gets stuck in while (lineState == false). If I connect it to PIN5 it gets stuck in while(lineState).

 

Also when I connect it to PIN13 every time the port Writes(true) or false, it turnes on or off netduinos blue LED. 

 

What am i doing wrong?

 

Thanks,

Ruben

        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;        }

adsf



#5 Shadi

Shadi

    Member

  • Members
  • PipPip
  • 12 posts

Posted 15 May 2013 - 11:06 PM

The only way you're going to get somewhat accurate measurements is to use an Interrupt and calculate based off the time parameter.

public UltraSoundSensor(Cpu.Pin trigger, Cpu.Pin echo){	this.pinTrigger = new OutputPort(trigger, false);	this.pinEcho = new InterruptPort(echo, false, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeBoth);	pinEcho.OnInterrupt += new NativeEventHandler(pinEcho_OnInterrupt);				isBusy = false;	LastEchoDelayTicks = 0;	LastEchoDelayMilliSeconds = 0;}
protected void pinEcho_OnInterrupt(uint data1, uint data2, DateTime time){	if (data2 == 0)	{		ticksEndEcho = time.Ticks;		LastEchoDelayTicks = ticksEndEcho - ticksStartEcho;		LastEchoDelayMilliSeconds = LastEchoDelayTicks / TimeSpan.TicksPerMillisecond;		LastEchoDelayMeters = LastEchoDelayMilliSeconds * 0.3432;		isBusy = false;		if (Pong != null)			Pong(LastEchoDelayMeters);	}	else	{		ticksStartEcho = time.Ticks;	}}

This won't be as accurate as baxter's temperature/smoothed version.



#6 rubenhak

rubenhak

    Member

  • Members
  • PipPip
  • 10 posts

Posted 16 May 2013 - 12:26 AM

Shadi, The trouble is that I'm not getting any interrupt on a echo port.







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.