Arduino IR distance sensor

Help me by sharing this post

←PREVIOUS TUTORIAL       NEXT TUTORIAL→

See also original post. 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.

PART 1 - Schematic

The connections are pretty easy, see the image below with the breadboard circuit schematic. The sensor will give an analog signal according to the distance. We need to know the distance to voltage grtaph in order to know how to map the real value later in the code. Make the connections and let's see the code.

We need:
1 x Arduino NANO/UNO: LINK eBay
1 x GP2Y0A41SK0F module: LINK eBay
1 x i2c LCD: LINK eBay
1 x Jump wires: LINK eBay

PART 2 - CODE results on serial monitor

We read the analog value from the sensor. We look over the datasheet of the GP2Y0A41SK0F SHARP distance sensor and we can see the grap that will give us the regression line of the voltage vs distance. With that function, we can get the real distance in the code.

// Sharp IR GP2Y0A41SK0F Distance Test
// http://www.electronoobs.com/eng_arduino_tut72.php

#define sensor A0 // Sharp IR GP2Y0A41SK0F (4-30cm, analog)

void setup() {
Serial.begin(9600); // start the serial port
}

void loop() {

// 5v
float volts = analogRead(sensor)*0.0048828125;  // value from sensor * (5/1024)
int distance = 13*pow(volts, -1); // worked out from datasheet graph
delay(1000); // slow down serial port

if (distance <= 30){
Serial.println(distance);   // print the distance
}
}

Upload the code and make the connections. Then open the serial monitor at 9600 bauds and you will have the distance printed to the serial monitor. Or go below and downlaod the code with the i2c LCD.

PART 3 - CODE results on LCD

We read the analog value from the sensor. We look over the datasheet of the GP2Y0A41SK0F SHARP distance sensor and we can see the grap that will give us the regression line of the voltage vs distance. With that function, we can get the real distance in the code. Remember to download and install the i2c library for the screen and then compile and upload.

// Sharp IR GP2Y0A41SK0F Distance Test
http://www.electronoobs.com/eng_arduino_tut72.php

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// Set the LCD address to 0x27 or 0x3f for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 20, 4);

#define sensor A0 // Sharp IR GP2Y0A41SK0F (4-30cm, analog)

void setup() {
lcd.init();
lcd.backlight();
Serial.begin(9600); // start the serial port
}

void loop() {
// 5v

cm = pow(3027.4 / Read, 1.2134); // conversiÃ³n a centÃ­metros

delay(100); // tiempo de espera

if (cm <= 30){
Serial.print("Distance in cm: ");
Serial.println(cm); // lectura del sensor 0
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" DISTANCE in cm ");
lcd.setCursor(0,1);
lcd.print(cm);
}
else
{
Serial.println("OUT OF RANGE");   // print the distance
lcd.clear();
lcd.setCursor(0,0);
lcd.print("  OUT OF RANGE  ");
}

}

Upload the code and make the connections. Then open the serial monitor at 9600 bauds or just see the results on the LCD screen.

←PREVIOUS TUTORIAL       NEXT TUTORIAL→

Help me by sharing this post