Sharp IR sensor displayed through virtual com port

SHARP GP2Y0A21YK0F Sensor Specs: Basic Description: GP2Y0A21YK0F is a distance measuring sensor unit, composed of an integrated combination of PSD (position sensitive detector) , IRED (infrared emitting diode) and signal processing circuit. The variety of the reflectivity of the object, the environmental temperature and the operating duration are not influenced easily to the distance detection because of adopting the triangulation method. This device outputs the voltage corresponding to the detection distance. So this sensor can also be used as a proximity sensor. '

Features: 1. Distance measuring range : 10 to 80 cm 2. Analog output type 3. Package size : 29.5×13×13.5 mm 4. Consumption current : Typ. 30 mA 5. Supply voltage : 4.5 to 5.5 V

Basic Advice and Maintenance: • The lens of this device needs to be kept clean. There are cases that dust, water or oil and so on deteriorate the characteristics of this device. Please consider in actual application. • Please don’t do washing. Washing may deteriorate the characteristics of optical system and so on. Please confirm resistance to chemicals under the actual usage since this product has not been designed against washing. • In case that an optical filter is set in front of the emitter and detector portion, the optical filter which has the most efficient transmittance at the emitting wavelength range of LED for this product (λ = 870 ± 70nm), shall be recommended to use. Both faces of the filter should be mirror polishing. Also, as there are cases that the characteristics may not be satisfied according to the distance between the protection cover and this product or the thickness of the protection cover, please use this product after confirming the operation sufficiently in actual application. • In case that there is an object near to emitter side of the sensor between sensor and a detecting object, please use this device after confirming sufficiently that the characteristics of this sensor do not change by the object. • When the detector is exposed to the direct light from the sun, tungsten lamp and so on, there are cases that it can not measure the distance exactly. Please consider the design that the detector is not exposed to the direct light from such light source. • Distance to a mirror reflector can not be sometimes measured exactly. In case of changing the mounting angle of this product, it may measure the distance exactly. • In case that reflective object has boundary line which material or color etc. are excessively different, in order to decrease deviation of measuring distance, it shall be recommended to set the sensor that the direction of boundary line and the line between emitter center and detector center are in parallel.

On Mar 10, 2014, at 3:11 PM, Patrick Harp <> wrote:

For IR sensor go here: For Serial PC go here:

This project is intended to input distance using a Sharp IR sensor and output data through PC virtual com port. We are taking analog signals from the Sharp Microelectronics - GP2Y0A21YK0F ($12.99) IR sensor connected to a mbed and displaying through the mbeds virtual com port. We are also adjusting these values so that it displays the actual distance measured.

The code starts by declaring the analog input from the IR sensore and the serial virtual com ports for the output. It prints off Hello World to tell you the program is running. Then an infinite while loop so that you will continue to run and a wait to keep the data from flowing too fast to read. The mbed reads the input from the IR sensor as a 1 for 4cm away and 0 for 30cm, this data is desplayed by "IR sensor reads...". You will notice that it has been decleared a float, this is for accuracy and for the fact that pc.printf does not work withouth casing a variable as a int,double,etc. Then we use a=1-a to get 0 = 4cm and 1 = 30cm so that we can use the equation 26*x+4 = distance. and print out "Distance is .. cm".

IR sensor to PC virtual COM port

#include "mbed.h"
AnalogIn IRSensor(p19);
Serial pc(USBTX, USBRX); // tx, rx

int main() {
    pc.printf("Hello World!\n");
    while(1) {
       //3.1V at 4cm to 0.3V at 30cm. 
       float a = IRSensor;// 1=4cm 0=30cm
       pc.printf("IR sensor reads %2.2f\n ", a);
       a=1-a;// now 0=4cm and 1=30cm 
       pc.printf("\rDistance is %2.2f cm \n ", (a*26+4)); // print and convert to distance by taking x=0->1 and 26*x+4


1 comment on Sharp IR sensor displayed through virtual com port:

24 Apr 2015


Please log in to post comments.