Sharp Infrared Distance Sensor (2Y0A21YK)
Wiring Schematic and Setup
Software and Code
Inspiration was found by researching online where I found this useful tutorial which explained an Arduino coding setup:
What is the Effective Range of Operation:
The answer to this question was also found by researching online and pulling up the datasheet for this particular product. In the end, I found three different datasheets, but all of them appear to agree with one another and indicate that the effective range was 10 to 80cm. Although there were various documents, they all seem to verify one another.
What Happens if an Object is Closer Than the Shortest Distance:
Based on the plots provided in the datasheet, it appears that the Distance vs Voltage plot follows an exponentially decaying curve within the suggested range of 10 to 80cm, When plotting inverse distance vs voltage, this represents an friendly almost linear relationship.
However, going back to the distance vs voltage plot, it seems that the output voltage will suddenly change behavior and sharply decrease as the object moves closer than 10cm to the sensor. This will also cause the inverse distance vs voltage behavior to deviate from its linearity.
AnalogRead() Values at 20, 40, 60 and 80 cm From the Sensor
AnalogRead() values were taken by looking at the serial monitor while the Arduino program was run from my laptop computer. Distances were measured using a standard 1 foot ruler with cm markings and a white infrared reflective paper was placed perpendicularly at the desired distances.
The results can be found tabulated below along with a plot of distance vs analogRead() values. Most notably we find what resembles an exponentially decaying relationship with an R-squared value of 0.985 between distance and our experimental analogRead() values. This seem to fall perfectly in line with the behavior predicted above in the datasheets.