HUM: SHARP GP2Y0A21YKOF ANALOG DISTANCE SENSOR
You are a beginner with Croduino. Or electronics? A specific module caught your eye, but you do not know how to use it? Do not worry, HUM is here for you! How to Use Module (HUM) is a blog tutorials series by e-radionica where you will find all you need in order to begin working with your favorite module. Tutorials include: technical characteristics, work principle, instructions on how to connect the module with Croduino and the basic code. Everything else is left to your imagination.
If you have ever made a project in which you had to measure the distance to some obstacle, then this is the sensor that allows just that. SHARP GP2Y0A21YK0F distance sensor measures the distance to the obstacle, and at the output, depending on the distance, provides the appropriate voltage. The sensor is easy to use, we only need one analog pin to use it. To learn how to use the sensor, let us first see how it works, and after that, it will be clearer to us how to use it.
• Voltage: 5V
• Current consumption: 30 mA
• Distance range measured: 10 cm – 80 cm
• Dimensions: 45x13x13.5 mm
HOW DOES THE SENSOR WORK?
The sensor consists of three basic parts used for distance measurement, the infrared LED diode (IRED), the light sensor (PSD) and the processor that processes the signal. The diode emits a precise wavelength of light (about 870 nm), and the light sensor that responds to that exact wavelength measures the amount of light intensity. If the intensity is stronger, then the obstacle is closer and vice versa. The processor processes the signal from the light sensor and provides us with the appropriate voltage at the output that we measure with Croduino and calculate the distance.
HOW TO CONNECT IT?
The module is easy to connect because it has an analog output that we need to connect to Croduino’s analog input.
We still have to bring power to the sensor and we are ready to work. The sensor has wires that are labeled with colors, the red wire is connected to +5 V, the black wire to GND, and the yellow one is our signal and we connect it to Croduino’s analog pin.
Because of the fact that the sensor has an analog output, we need to connect an electrolytic capacitor of 100 μF 16 V (or greater voltage) to the sensor power supply (the capacitor – is connected to gnd, and + to +5V of the sensor) to reduce the interference and to make the readings more accurate.
To work with the sensor we need a library that measures the distance several times and calculates the middle value because a single measurement does not give reliable and accurate results, and therefore we take more measurements. The code is simple because we just call the function that gives us the distance, and then, with this data, we can do whatever we want to. If you are stuck with installing a library, check our tutorial that explains the whole process.
#include <SharpIR.h> //including the library to work with the distance sensor
//sensors that can be used with this library
SharpIR senzor( SharpIR::GP2Y0A21YK0F, A0 );
//SharpIR library constructor (name of the sensor, analog pin to which the module is connected)
//Serial communication initialization (speed 9600 baud)
//to the dis variable, we save the distance which we get by calling the function distance
// displaying the text in the brackets on the Serial monitor
//displaying the distance value on the Serial monitor
//2 seconds pause between the readings
Products used in this tutorial
No products found