Skip to content

Measure the Output of the Sharp IR Distance Sensor Exercise

GTA Marking

This is an assessed laboratory exercise. Upon completing all practical tasks, verify that your work satisfies the Assessed Exercise Requirements before demonstrating your system to a Graduate Teaching Assistant (GTA) for marking.

Note

Before beginning software implementation, verify that the Sharp IR distance sensor is securely mounted to the robot chassis, as shown in Building the Robot: Fig. 11. Verify the wiring and pin allocation against the suggested layout, given in Building the Robot: IR Sensor Circuit Layout

The following video provides a quick demonstration of the expected outcomes for this exercise:

Video demonstrating the expected outcome from this exercise

Introduction and Background

The aim of this exercise is to experimentally characterise the Sharp IR sensor and draw a graph of the measured output against measurement distance. During this exercise, you will interface the Sharp IR distance sensor with the Arduino's ADC, record the measured output voltage as a function of the distance to a target, and evaluate operational limitations—specifically the minimum detection threshold and dynamic range.

!!!! note "Hardware Assembly Checklist" Before proceeding with this exercise, you must ensure that the circuit is constructed in accordance with the IR sensor circuit instructions from the Building the Robot Document.

Noise on the IR sensor

The IR sensor is not an ideal sensor. As can be seen from Fig 1, significant noise is induced onto the output of the sensor during operation.

Serial Plotter Showing Noise on the IR distance sensor.
Serial Plotter Showing Noise on the IR distance sensor.

A simple moving averaging filter technique can be used on the output data from the IR sensor to reduce the noise content of the signal. A simple, but effective, implementation of this such techniques is illustrated in the Averaging Code, provided below:

Averaging Code
//Read the IR sensor
irVal = analogRead(irPin);

// accumulate a further 7 readings
irVal = irVal + analogRead(irPin);
irVal = irVal + analogRead(irPin);
irVal = irVal + analogRead(irPin);
irVal = irVal + analogRead(irPin);
irVal = irVal + analogRead(irPin);
irVal = irVal + analogRead(irPin);
irVal = irVal + analogRead(irPin);

// right shift 3 places to divide by 8
irVal = irVal >> 3;
The Averaging Code, above, sums the result of 8 ADC readings from the IR sensor and take the average value of these readings. This is a crude, but effective method for noise reduction, in this instance. The results of this implementation can be seen from the data shown in Fig 2.
Serial Plotter Showing the Averaged IR Sensor Data.
Serial Plotter Showing the Averaged IR Sensor Data.

Comparing the noise magnitude superimposed on the raw IR sensor measurement in Fig 1 against the filtered response in Fig 2 demonstrates that this 8-sample moving-average technique significantly reduces the noise signals induced onto the IR sensor measurement.

Experimental Setup with Lolly Stick up

The IR sensor is sensitive to picking up reflections from any surface that it is operating. It has been found from experimentation, that raising the sensor above the surface (operating plane) produces improved and more consistant results.

As a result, when using the IR sensor, you should ensure that the lolly stick assembly is in the upright position, as shown in the righthand figure in the Building the Robot document

Component Data Sheets and Technical Documentation

For more details on concerning the pin connections for the Sharp GP2Y0A21YK0F distance measurement sensor, see the data sheet for the sensor on the Blackboard site:
  • ACS231 Blackboards Site>>Mechatronics Kit Information>> Component Data Sheets and Technical Documentation.

Expected Results

The IR sensor has a non-linear distance to output voltage characteristic, as illustrated in Fig 3. The sample curve in Fig 3 illustrates the expected profile of your empirical dataset — note, however, that your recorded plot must include a fully scaled and labeled vertical axis representing the output values from the Arduino ADC, not voltage.

Characteristic measurement result from the IR distance sensor.
Characteristic measurement result from the IR distance sensor.

Assessed Exercise

For this assessed exercise you will develop an Arduino sketch to sample the output of the IR sensor and display the ADC value of on the serial monitor. The distance measurements should be taken at STATIC positions, using a series of measurement points at:

1cm, 2cm, 3cm, 4cm, 5cm, 6cm, 7cm, 8cm, 9cm, 10cm, 15cm, 20cm, 25cm, 30cm, 35cm, 40cm, 45cm, 50cm, 55cm, 60cm, 65cm, 70cm, 75cm, and 80cm

You will collect static calibration data and generate a plot using a software analysis tool (such as Microsoft Excel, MATLAB, or Python/Matplotlib). The resulting plot must display Target Distance (\(\text{cm}\)) on the horizontal (\(x\)) axis and ADC Reading on the vertical (\(y\)) axis.

Procedure:

  1. Develop the Acquisition Sketch: Using the POT.ino sketch as a template, write an Arduino sketch to sample the output of the IR distance sensor, incorporating the 8-sample moving avarage filter, and stream the results to the serial monitor.
  2. Prepare Measurement Fixture: Position the robot chassis level surface. Align a ruler or tape measure along the sensor's optical axis. Ensure no adjacent obstacles infringe on the sensor's field of view.
  3. Record the Sensor Output at Different Distances: For all of the distance measurements, listed above, record and tabulate the ADC measurement values.
  4. Plot Sensor Transfer Function: From this dataset, plot the results for Target Distance (\(\text{cm}\)) on the horizontal (\(x\)) axis and ADC Reading on the vertical (\(y\)) axis.
  5. Demonstrate and Obtain Sign-Off: Present your resulting graph to a GTA for assessment and demonstrate the operation of your working system.

Exercise Assessment

What we expect to see from your demonstration?

When completed, your code should allow you to run an experiment to draw the following graph:

  • A graph of ADC measurement value against distance.
  • The graph should be correctly framed within the axes.
  • Appropriate axes labels, with units and a descriptive graph title. You should plot your results in a computer package, such as Microsoft Excel, MATLAB, or Python/Matplotlib, with the x-axis as the distance measured, and the y-axis as the ADC measurement value, as illustrated in Fig 3

We expect you to also demonstrate the operation of your code and the output on the Serial Monitor, as describes in the procedure.

Now Get Your Work Marked by a GTA

Once you have completed your code and are satisfied with its operation, you should show your work to a GTA for marking.