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

New to Netduino, need help for IR sensors


  • Please log in to reply
12 replies to this topic

#1 aniMK16

aniMK16

    New Member

  • Members
  • Pip
  • 8 posts

Posted 01 April 2015 - 10:55 AM

Hi guys, I am a newbie in Netduino and I know very few things about it like-

1. Turning on and off the on-board LED.

2. Turning on and off external LED.

3. Running a motor for a limited time.

What I want to know is how to control IR distance sensor connected to it.

I want to make a program which will take input from the IR distance sensor, then if the distance is for example - 3 cm, then the program will start a sound from the speaker connected to the analogue of the micro-controller.

Is it possible. If yes, then some-body pleas help.

Thanks and sorry if this topic has been posted preciously by some-body else.



#2 KiwiDev

KiwiDev

    Advanced Member

  • Members
  • PipPipPip
  • 100 posts
  • LocationNew Zealand

Posted 07 April 2015 - 09:57 AM

Hi aniMK16

 

If you have a look at

 

http://netmftoolbox.codeplex.com/

 

and

 

http://netduinohelpers.codeplex.com/

 

These have drivers for a Sharp IR Ranger (which produce an analog signal) these should get you started.

 

Do you want a beeper or do you want to play a custom sound?

 

@KiwiBryn

blog.devmobile.co.nz



#3 aniMK16

aniMK16

    New Member

  • Members
  • Pip
  • 8 posts

Posted 08 April 2015 - 07:05 AM

@KiwiDev Thank you!

And I want custom sound. Is it possible to get the custom sound

example - "obstacle at 3 cm."

Please reply!



#4 KiwiDev

KiwiDev

    Advanced Member

  • Members
  • PipPipPip
  • 100 posts
  • LocationNew Zealand

Posted 08 April 2015 - 08:55 AM

Hi aniMK16

 

For a custom sound you could use a VS1053 based MP3 player shield. These are available from a number of vendors such as Sparkfun, Elecfreaks, Adafruit, Seeedstudio, GE Tech etc.

 

SoftElectoTech have written a VS1053 driver which is available from http://softelectrotech.com/?p=701

 

With this driver code you should be able to play an MP3 audio file from the SD card based on the distance info returned by the IR ranger 

 

Have a look at my blog post for more info on MP3 shields

 

@KiwiBryn

blog.devmobile.co.nz



#5 aniMK16

aniMK16

    New Member

  • Members
  • Pip
  • 8 posts

Posted 08 April 2015 - 11:35 AM

@KiwiDev Thank-you once more.

I got the thing about custom sound. But I did not get the IR distance measure on the links you told me. I didn't get the things I wanted.

Any-ways, I just saw Ultrasonic range distance measurement code here http://blog.devmobil...tegory/sensors/ . I could not understand the code. 

Could you please explain me?



#6 KiwiDev

KiwiDev

    Advanced Member

  • Members
  • PipPipPip
  • 100 posts
  • LocationNew Zealand

Posted 09 April 2015 - 09:27 AM

Hi aniMK16

 

The Sharp GP2Y0A02YK sensor outputs a voltage which you measure with an AnalogInput. This voltage can be converted into how far away the object is. (The NetMFToolbox driver code does the voltage to distance conversion)

 

From the netMF toolbox sample code

 

public static void Main()
{
   SharpGP2Y0A02YK Distance = new SharpGP2Y0A02YK(new Netduino.ADC(Pins.GPIO_PIN_A0));
 
   while (true)
   {
      int cm = Distance.Distance;
      int inch = (int)(cm / 2.54);
      Debug.Print("Approximate distance: " + cm.ToString() + "cm / " + inch.ToString() + "\"");
      Thread.Sleep(1000);
   }
}
 
The ultrasonic ranger I use in my blog post is a fairly common one. You initiate a measurement by strobing the trigger pin  (with a digital output). Then measure the width of the pulse on the echo pin (with an InteruptPort triggering on both edges) which represents how far away the object is. My sample code in the event handler does the pulse width to distance conversion.
 
static void echoInterruptPort_OnInterrupt(uint data1, uint data2, DateTime time)
{
   long pulseWidthTicks;
 
   if (data2 == 1) // leading edge, start of pulse
   {
      pulseStartTicks = time.Ticks;
   }
   else
   {
      pulseWidthTicks = time.Ticks - pulseStartTicks;
 
      long distance = pulseWidthTicks / 58;
 
      Debug.Print("distance = " + distance.ToString() + "mm");
   }
}
 
Based on the distance you could play different sound files using an MP3 player shield & code referred to in my previous post
 
@KiwiBryn
blog.devmobile.co.nz


#7 aniMK16

aniMK16

    New Member

  • Members
  • Pip
  • 8 posts

Posted 11 April 2015 - 02:56 PM

@KiwiDev, thanks.

So can the thing which I want to do be easier to do that is to measure distance and play the buzzer with ultrasonic sensor?

I am using a simple ultrasonic sensor, the circuit-construction is very simple! I connect the 2 terminals pof thr sensor to GPI0 A0 AND A1.

Thank-you

Please reply



#8 KiwiDev

KiwiDev

    Advanced Member

  • Members
  • PipPipPip
  • 100 posts
  • LocationNew Zealand

Posted 12 April 2015 - 03:39 AM

Hi,

 

I would use a couple of the digital IO pins rather than analog ones.

 

In the code sample on my blog I use D5 (configured as an OutputPort) to the trigger pin and D4(configured as an InterruptPort) to the echo ping on the ultrasonic ranger. If you used those pins you could adapt my code by removing all references to the LED bar and then it would just display the distance.

 

Then, you could use the measured distance to set the period of a Timer which turns a piezoelectric beeper attached to a DigitalOutput on & off.

 

@KiwBryn

blog.devmobile.co.nz



#9 aniMK16

aniMK16

    New Member

  • Members
  • Pip
  • 8 posts

Posted 12 April 2015 - 08:50 AM

So I will use 4 digital pins(2 for sensor 2 for speaker)?

So D0 and D1 will be input from sensors right? And D3 & D4 will be output port

Am I right? If my circuit is wrong, please correct me.

Thanks for all ur help!



#10 KiwiDev

KiwiDev

    Advanced Member

  • Members
  • PipPipPip
  • 100 posts
  • LocationNew Zealand

Posted 12 April 2015 - 10:49 AM

Hi,

 

For a single ultrasonic ranger like an HC-SR04 you need two digital pins.

 

One would be an OutputPort used to trigger the ranging process, the other would be an InterruptPort used to measure the width of the echo to determine the range,

 

If you just want to make a beeping noise (like park distance control in a car) rather than using a speaker it would be easier to use a buzzer like this which produces a tone, this approach would use one digital output.

 

The picture below shows how to wire up an HC-SRO4 ranger for the provided sample code 

public class Program
{
   private static OutputPort triggerOutput;
   private static InterruptPort echoInterrupt;
   private static long pulseStartTicks;
      
   public static void Main()
   {
      triggerOutput = new OutputPort(Pins.GPIO_PIN_D3, false);

      echoInterrupt = new InterruptPort(Pins.GPIO_PIN_D2, true, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeBoth);
      echoInterrupt.OnInterrupt += new NativeEventHandler(echoInterruptPort_OnInterrupt);

      Timer distanceUpdate = new Timer( distanceUpdateCallbackProc, null, 0, 500 ) ;

      Thread.Sleep(Timeout.Infinite);
   }

      
      
   public static void distanceUpdateCallbackProc(object state)
   {
      triggerOutput.Write(false);
      Thread.Sleep(2);
      triggerOutput.Write(true);
      Thread.Sleep(10);
      triggerOutput.Write(false);
      Thread.Sleep(2);
   }

      

   static void echoInterruptPort_OnInterrupt(uint data1, uint data2, DateTime time)
   {
      long pulseWidthTicks;      

      if (data2 == 1) // leading edge, start of pulse
      {
         pulseStartTicks = time.Ticks;
      }
      else
      {
         pulseWidthTicks = time.Ticks - pulseStartTicks;

         long distance = pulseWidthTicks / 58;

         Debug.Print("distance = " + distance.ToString() + "mm");
      }
   }
}

@KiwBryn

blog.devmobile.co.nz

 

 

 

wp_20150412_004.jpg



#11 aniMK16

aniMK16

    New Member

  • Members
  • Pip
  • 8 posts

Posted 12 April 2015 - 03:16 PM

I got half the code, but why it says Debug.Print(....);

I dont want to print anything, just the buzzer!



#12 KiwiDev

KiwiDev

    Advanced Member

  • Members
  • PipPipPip
  • 100 posts
  • LocationNew Zealand

Posted 13 April 2015 - 07:50 AM

Hi aniMK16

 

I would use a timer to generate the beeps and would change the frequency of the beeps based on the distance. 

 

The demo code below flashes the onboard LED at different frequencies to show how to use a timer and change the frequency. (You will need to change the pin to suit your wiring.)

 
You will need to add a Timer to the Ultrasonic Ranger code sample and where the code does the Debug.Print(....); you will need to add some logic to control the frequency of beeps emitted by a buzzer by calling  the Change(...) method of your timer. 
 

@KiwDev

blog.devmobile.co.nz

 

 

 

 

public class Program

   {
      static OutputPort buzzer = new OutputPort(Pins.ONBOARD_LED, false); 
 
      public static void Main()
      {
         Timer buzzerTimer = new Timer(buzzerTimerCallback, null, 0, 500);
 
         Debug.Print("Far away");
 
         Thread.Sleep(5000);
 
         Debug.Print("Closer");
 
         buzzerTimer.Change(0, 250);
 
         Thread.Sleep(5000);
 
         Debug.Print("Really Close");
 
         buzzerTimer.Change(0, 100);
 
         Thread.Sleep(5000);
 
         Debug.Print("Stopped");
 
         // Stop the timer and turn the buzzer off
         buzzerTimer.Change(Timeout.Infinite, Timeout.Infinite);
         buzzer.Write(false)
 
         Thread.Sleep(Timeout.Infinite);
      }
 
 
 
      public static void buzzerTimerCallback(object state)
      {
         buzzer.Write(!buzzer.Read());
      }
   }


#13 aniMK16

aniMK16

    New Member

  • Members
  • Pip
  • 8 posts

Posted 18 April 2015 - 06:29 AM

Hey KiwiDev, I am thankful that you gave me the code.

But I am unable to understand it. Will you please explain the code to me step-by-step.

Thank-you.






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.