Introduction: How to Use the Sharp IR Sensor (GP2Y0A41SK0F) - Arduino Tutorial
In this tutorial we will use the Sharp IR sensor (GP2Y0A41SK0F) to measure distance from an object.
IR Sensors work by using a specific light sensor to detect a select light wavelength in the Infra-Red (IR) spectrum. By using an LED which produces light at the same wavelength as what the sensor is looking for, you can look at the intensity of the received light. When an object is close to the sensor, the light from the LED bounces off the object and into the light sensor. This results in a large jump in the intensity, which we already know can be detected using a threshold.
Since the sensor works by looking for reflected light, it is possible to have a sensor that can return the value of the reflected light. This type of sensor can then be used to measure how "bright" the object is. This is useful for tasks like line tracking.
In this tutorial we will try to measure the distance from an object (4~30cm).
Let's get started!
Step 1: What You Will Need
For this tutorial you will need:
- Arduino uno
- Sharp IR Sensor (model GP2Y0A41SK0F)*
- 3 breadboard cables
* The code will only work for model GP2Y0A41SK0F, if you have a different one just contact us to make a tutorial for your sensor.
Step 2: The Circuit
The connections are pretty easy, see the above image with the breadboard circuit schematic.
Step 3: The Code Using Codebender
Here's the code, embedded using Codebender!
Try downloading the Codebender plugin and clicking on the "Run on Arduino" button to program your Arduino board. And that's it, you've programmed your Arduino with this sketch!
You can keep playing with that by clicking the "Edit" button and start making your own modifications to the code.
Step 4: Serial Monitor - Testing
Press connect button below to start serial communication.
Step 5: Well Done!
You have successfully completed one more "How to" tutorial and you learned how to use the Sharp IR sensor with Arduino.
I hope you liked this, let me know in the comments!
7 Comments
Question 2 years ago
Is the distance reading greatly affected by the material?
I'm trying to find a way to make the dust shoe (brush) on my CNC router automatically adjust to the height of the material underneath. However, I may sometimes cut MDF, plywood, plastics etc..
3 years ago
What is this? "float volts = analogRead(sensor)*0.0048828125???
and How to covert this value to the Sharp2Y0A20 ????
I want to test 20 to 150 cm Sensors .....
6 years ago
Hey i need help. I have to use two sensors like this but i don't know how to write this. I have Arduino Uno R3 board with Adafruit Motorshield V1. Please help me.
Thanks.
6 years ago
Dear Codeblender.cc
this is my model, GP2Y0A21YK0F, can you help me do up a code? thanks:)
Sincerely Gabriel
Reply 6 years ago
I think most of these Sharp sensors operate in a similar manner. The difference will be the numbers used to turn the analog reading into distance.
This could be figured out by comparing the output specs between the two, or just some experimentation.
6 years ago
Dear Codeblender.cc,
i also do not understand this line, please help me?
13*pow(volts, -1); // worked out from datasheet graph
thanks:)
Sincerely Gabriel
Reply 6 years ago
It looks like he is finding the reciprocal for the variable volts
https://www.arduino.cc/en/Reference/Pow