Example Code

Measuring Distance with myDAQ and Sharp Infrared Proximity Sensor

Code and Documents

Attachment

Overview


Measure distance with Sharp’s Infrared Proximity Sensor sold by SparkFun (SEN-08959) and National Instruments myDAQ in LabVIEW. Acquire data using the DAQ Assistant which is provided in LabVIEW with the NI-DAQmx device drivers, and convert the data into distance using interpolation and basic programming in LabVIEW.

Objective:

Measure distance using the Sharp Infrared Proximity Sensor (Sharp GP2Y0A02YK0F) with National Instrument’s myDAQ and LabVIEW.

 

 http:/static.sparkfun.com/images/products/08958-03-L_i_ma.jpg

Figure 1: Sharp Infrared Proximity Sensor (Sharp GP2Y0A02YK0F)

Background:

Infrared Proximity Sensors are sensors that use an infrared output and read in the reflected infrared light by use of a photodiode. The resulting magnitude of the reflected light can tell the range of the object with respect to the photodiode. For instance, when an object is in front of the sensor a pulse of infrared light is sent out then bounces off the object. The object will reflect light back to the sensor. Since the object cannot achieve 100% reflectivity due to environmental factors, the infrared light will return back to the sensor's photodiode at only a fraction of what was pulsed. This difference is read in by the photodiode to determine the distance. Normally infrared sensors have limitations due to noise from other light sources and the reflectivity of the target object; however, Sharp has implemented triangulation to make it less susceptible to interference.

Note: See the application note by Sharp linked to this example for more information on the signal conditioning.

 

What You Need:

  • NI myDAQ
  • LabVIEW
  • Sharp Long Range Infrared Proximity Sensor (SEN-08958)
  • Wire
  • Infrared Sensor Jumper Wire (SEN-08733)
  • Breadboard (optional)

 

Wiring Instructions:

The wiring of the sensor is straight forward because the sensor already has the wire adapter to plug straight into the screw terminals of the myDAQ. In order to power the sensor, it needs to be connected to a 4.5 to 5.5 VDC source via the red wire (Vcc). The myDAQ has a 5V output connection that can supply up to 500mA of current.  The data sheet of the sensor says that the Sharp Infrared Proximity Sensor has a typical 33mA current draw from the power source, which is well below the limit of the myDAQ. Once the red power wire is connected the black wire (GND) needs to be connected to the ground of the myDAQ (this makes sure that the sensor and the myDAQ both have the same reference point). In this case we connected the black wire to AGND. The ground also needs to be connected to AI0-. This is because the myDAQ is limited to differential measurements only which takes AI0+ - AI0-. However, we want AI0+ - GND, so AI0- needs to be set to GND (achieved by connecting GND to AI0-). The yellow wire is the sensor output (Vo) this sensor output should be connected to AI0+. This means the voltage we are reading is Vo - GND. 

Note: Before connecting hardware, always make sure that the power specifications match up with the sensor’s requirement.

 

Connection Overview:

  1. Connect the red wire to the 5V output of the myDAQ.
  2. Connect the black wire to the AGND.
  3. Jump AI0- to AGND.
  4. Connect the yellow wire to AI0+.

The final connection can be seen below:

Figure 2: Final Connection

 

LabVIEW User Interface:

The user interface (see Figure 3) shows the distance in a digital display as well as a progress bar to graphically show the distance. The top left of the front panel we have frequency, sample rate and number of samples as inputs to set the filter and data acquisition tasks. We also have an Average of Voltage samples indicator that will show what the average value for each set of data retrieved from the myDAQ. There are 2 graphs and 1 chart in this example. The chart shows the average voltage and its change over time. The graphs are the raw data coming off the myDAQ and a filtered version of that data. There is also a stop button to stop the VI.

Figure 3: Front Panel of VI

Note: You should avoid using the abort at the top of the VI because this causes the VI to abruptly stop instead of stopping and cleaning up any used references gracefully.

 

Coding Strategy:

There are 3 parts of this example:

  1. Gather the raw data in the form of voltage from the sensor.
  2. Filter and average the data so that we can reject some noise.
  3. Interpolate the data to convert it into distance.

In order to receive data at a consistent rate, use the continuous acquisition mode to get a set amount of data utilizing the myDAQ onboard timing engine. The timing engine will ensure that the time between samples is consistent for the duration of the program.

Even though we have the data, it will be slightly noisy, and this is due to external noise that might be found on the wires or even from extra light being detected by the sensor. Even though the sensor does have signal conditioning to reduce noise, it is not fully immune to noise. In order to filter out the noise, we will use a low pass filter to remove extraneous spikes. 

The sensor actually sends back data in "time windows" which can be seen on the graph as “steps” of voltage.  In order to keep the timing and coding simple, we will average the sample set instead of trying to match the timing of the myDAQ's sample set to the sample set of the sensor.

Note: In the data sheet that is attached to this example there is more detailed information on how the samples are sent out through the Vo pin.

After filtering we take the data and average the sample set into a single voltage, however this value is in volts and not centimeters. In order convert this data to centimeters we have to find a way to map this value to a non-linear curve as seen in figure 4 below and in the data sheet.

Figure 4: Distance vs. Voltage graph

The non linearity is a problem since we cannot use a line approximation, but we can use the spline interpolation functions found in LabVIEW to curve fit the above figure to the data. In order to use interpolation, there has to be a set of known values. By using estimated points from the graph in figure 4, we can estimate a set of data that the spline interpolation can use as a guide to determine the centimeter or voltage value. In this example we will use the following table.

 

Voltage Centimeters
2.75 15
2.55 20
2 30
1.55 40
1.25 50
1.15 60
.9 70
.8 80
.75 90
.65 100
.6 110
.55 120
.5 130
.455 140
.45 150

Table 1: Approximate Centimeter to Volt Pairs

Once we can interpolate the data we can output the data to the digital display in centimeters and update the progress bar to graphically show the distance changes.

The final block diagram can be seen below:

Figure 5: Final Block Diagram

 

Note: The attached LabVIEW code snippet can be dragged-and-dropped to a LabVIEW block diagram, use attached PNG file.  After locating the PNG file, just drag the file icon onto a blank block diagram, as if you were dragging the file onto your desktop.

 

How It Works:

First there are two constant array of values that were approximated from the graph shown in Figure 4. The two constants are then put into a VI called Spline Interpolant that outputs a 1D array called Interpolant, that is required by the Spline Interpolation sub VI. 

In the while loop, data is taken from the DAQ Assistant and send through the Filter Express VI to produce a cleaner signal. As a check, we output both the raw data and filtered data to their respective graphs. Once this is done, we average of the data points using the Mean VI and output the value to the indicator (Average of Voltage Samples). Once we have average of the sample set, which is also the average voltage, the value is sent through the Spline Interpolation VI to be matched with the corresponding centimeter value.

Then the value is output to the indicator to show the distance in centimeters.

Note: Graphs are used for the raw and filtered data because they are to show a single set of data from the DAQ Assistant. A chart, which has a buffer to store past points, is used for the average voltage in order to display the change over multiple data sets from the DAQ Assistant.

 

Setting Analog DAQ Assistant Task:

  1. Select the DAQ Assistant
  2. Select Acquire Signals -> Analog Input  -> Voltage
  3. Choose the myDAQ under supported channels and select AI0.
  4. Click next
  5. Set the max voltage to 3 and the min voltage to 0 on both channels.
  6. Set samples to read to 1000 and rate to 10000. (This will be ignored anyways since it is being overridden in code)

Figure 6: Analog Input Settings

 

Tips and Tricks

  • You can modify the VI to log the data to file using a ‘Write To Spreadsheet File.vi’ express VI if you wish to save the data.  Be sure to place it in the loop and be sure to append new data to the spreadsheet file.
  • To get more accurate results, find the timing between samples using the data sheet and read only when there is valid data.
  • Be careful of how the sensor is angled toward the target object as it will also throw off the reading.
  • Use ‘From DDT’ to turn the blue Dynamic Data Type wire into an orange double precision number.  This allows the data from the DAQ Assistant to be used with standard LabVIEW VIs.
  • You can right click on each control and indicator and make the icons smaller by selecting “Show as Icon”

Related Links

» Sparkfun's Sharp GP2Y0A02YK0F Product Page

» Sharp GP2Y0A02YK0F Data Sheet

» Infrared Proximity Sensor Long Range - Sharp GP2Y0A02YK0F

» NI myDAQ Getting Started and Support

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Contributors