

Note that we have called the sensor ‘mySensor’ in this example.

Print the measured distance to the serial monitor: Get a distance measurement and store it as distance_cm: Begin serial communication at a baudrate of 9600: SharpIR mySensor = SharpIR(IRPin, model) Create a new instance of the SharpIR class: Create variable to store the distance: *SHARP GP2Y0A21YK0F IR distance sensor with Arduino and SharpIR library example code. When the beam of light gets reflected by an object, the reflected beam will reach the light detector and an ‘optical spot’ will form on the PSD. The sensor consists of an IR LED and a light detector or PSD (Position Sensing Device). The distance is calculated using triangulation of the beam of light. is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to products on .Īn IR distance sensor uses a beam of infrared light to reflect off an object to measure its distance.

The SHARP GP2Y0A21YK0F is an easy to use IR distance sensor with a range of 10 – 80 cm.
