Measure distance using Sharp IR proximity sensor with Arduino + OLED [DigitSpace.com]
Sponsored by DigitSpace
Products used: Sharp IR proximity sensor and 0.96″ OLED display 128×64 Blue/Yellow
Hello and welcome, this is the first sponsored project from DigitSpace, and it’s about distance measuring using the Sharp IR proximity sensor GP2Y0A02YK0F. The measurement is in both metric and imperial systems, and we’ll use an OLED screen, there are a lot of possibilities to do with it: display the measurements with different units in the same time, switch between them…
The sensor emits beams of infrared light, and they reflect back to the sensor. the output can sometimes be digital like FC 51 IR module, which means at a certain distance it will give you a HIGH or LOW level, or like the one we’re using here which has an Analog Output: It gives you a voltage value depending on how far is the object to detect, but don’t forget that it depends on the object reflectiveness and the lighting conditions.
The range can also differ depending on the version, mine has 20 cm -> 120 cm, so it’s better to use yours and test it to know its range.
The Module has 3 wires: Vcc, Gnd and The Data or Output (Analog).
Test 1
Wiring
Library
There are different libraries to use but this one worked for me very well, you can download it from the link right here.
To install it, just: Arduino IDE -> Sketch -> Include library -> Add .ZIP file and select the library. Or extract and copy the folder containing the sources to the Arduino libraries folder.
Code
The first time you can choose to test with the library example it’s very simple or test with this code. Download the code here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
/* This code works with SHARP IR Analog procimity sensor * It measures the distance and displays it on the serial monitor every 2s * Refer to http://www.surtrtech.com for more information */ #include <SharpIR.h> //IR sensor library #define ir A0 //IR sensor analog output pin #define model 20150 //or1080 depends on the model it actually means the range in cm 20-150 or 10-80 // 1080 for GP2Y0A21Y // 20150 for GP2Y0A02Y String Data; //To store the sentence to display SharpIR IR_prox(ir,model); //define the sensor void setup() { Serial.begin(9600); } void loop() { int dis_cm=IR_prox.distance(); //Read the distance in cm and store it Data = "Distance = "+String(dis_cm)+(" cm"); //group what to display Serial.println(Data); //display the string e.g "Distance = 40 cm" delay(2000); //done every 2 seconds, you can change it but make it at least 50 ms } |
Result
Upload the code and open the serial monitor:
Sensor problem
The issue with the module is that at distances inferior to 20 cm it starts to give strange values (40 cm 35 cm…) which is not suitable for close range applications.
OR AM I THE ONLY ONE WHO HAS THIS ISSUE? Well test and tell me.
Test 2
The test 2, just replace the serial monitor with an OLED display, and add a push button to switch between some units (cm-mm-in).
Wiring 2
For the push button I like to use this wiring, and in the code I activate the internal pull-up, the button is always at HIGH state and when I press it goes LOW, you can use the usual method but you need to wire it with 5v and add a pull down resistor then set the pinMode as Input only and in the code activate the state as HIGH level instead of LOW.
And of course you can replace the OLED with the 128×32 version or LCD or whatever you like.
Libraries
In addition of the sensor library used before: download it from the link right here, you should add the OLED Adafruit libraries:
- Adafruit OLED SSD1306 library: Download here from Github or Donwload here.
- Adafruit GFX Library Download here from Github or Download here.
Code
Download the code here or check below (It contains the comments needed).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
/* This code works with SHARP IR proximity sensor and 132x64 OLED display and a push button * It displays the measured distance in "mm" and with every button press it switch to "cm" or "inch" and goes back * Refer to http://www.surtrtech.com for more details */ #include <SharpIR.h> //IR sensor library and OLED Adafruit library #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 64 // OLED display height, in pixels #define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin) #define ir A0 #define model 20150 //or1080 depends on the model it actually means the range in cm 20-150 or 10-80 // 1080 for GP2Y0A21Y // 20150 for GP2Y0A02Y #define Button1 2 //Push button digital pin, please note that it should be wired with this pin and GND bool Button1_state; //Here we store the button state if it's LOW or HIGH short State=1; //State is 1 at default and could change to 2 or 3 SharpIR IR_prox(ir,model); //IR sensor name "IR_prox" Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); //Declaring the display name (display) void setup() { pinMode(Button1, INPUT_PULLUP); //Here I activate the internal pull up for the button (Always on HIGH state, when pressed goes LOW) display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //Start the OLED display display.clearDisplay(); display.setTextSize(2); display.setTextColor(WHITE); display.display(); delay(2000); } void loop() { int dis_cm=IR_prox.distance(); //Read the distance in cm Button1_state=digitalRead(Button1); //We're constantly reading the button wired on D2 if it's pressed or not if(Button1_state==LOW){ //if the button is pressed if(State<3) //We check the State value it should be 1 or 2 State+=1; else //if it's 3 we go back to one State=1; delay(200); //a delay is needed so the push won't be counted twice or more times } display.clearDisplay(); display.setCursor(20,20); if(State==1){ //Depending on the button state we will display mm, cm or in display.print(dis_cm*10); display.print(" mm"); //in mm we just multiply by 10 } if(State==2){ display.print(dis_cm); display.print(" cm"); //cm is default } if(State==3){ display.setTextSize(1); display.setCursor(40,0); display.print("USA Baby"); //Was just messing up you can remove it of course :D :D display.setTextSize(2); display.setCursor(20,20); float in_m=dis_cm/2.54; //Conversion to inches also a float display.print(in_m); display.print(" in"); } //You can add as many state as you want to add other units just do the correct changes display.display(); delay(50); display.clearDisplay(); } |
Result
That’s it for this module, the only downside was that the sensor doesn’t give a warning or a stable value when it’s measuring under 20cm, but rather gives false readings…. Don’t forget to support the website and channel.
Big thanks to DigitSpace for their sponsorship.
Categories
Yassine View All
Automation and Electrical Engineer, Electronics amateur trying to share my little projects.
One thought on “Measure distance using Sharp IR proximity sensor with Arduino + OLED [DigitSpace.com]” Leave a comment ›