Sabtu, 10 Maret 2018

Arduino Sensor Sharp GP2D120 Distance Sensor Tutorial

Arduino Sensor Sharp GP2D120 Distance Sensor Tutorial

Image source: http://www.ozturkibrahim.com/wp-content/uploads/2014/04/IR_RangeFinder_bb-1024x493.png

Arduino Sensor Sharp GP2D120 Distance Sensor Tutorial

Introduction

The Sharp GP2D120 is a distance sensor from Sharp's range of optoelectronic devices. Optoelectronic devices are transducers that work by converting light to electricity or vice versa.

The GP2D120 has two lenses in front; it works by emitting a focused beam of light through one. If there is an object in the sensor's working range the beam reflects off that object and lands onto the collector (second) lens. This makes a triangle between the emitter, object and collector. The sensor then checks the angle of the reflected beam and uses it to identify how far the object is.
Sensor_diagram

There are two types of inputs digital and analogue. Digital inputs are discrete signals in the form of HIGHs and LOWs. The combination of these HIGHs and LOWs indicates what the data is. Analogue signals are continuous signals in the form of voltage. The GP2D120 distance sensor gives an analogue output. It can detect objects within the range of 40mm to 300mm. When the sensor is turned on it takes 44 ms to start up and has a response time of 39 ms meaning that it updates its values every 39 ms. The average power consumption is 33 mA.

Like everything there are pros and cons for this sensor as well. The Sharp GP2D120 is cheap, light, compact, easy to program and accurate enough for most tasks. On the other hand it can only get a reading of one point of the object, any readings below 40mm are unreliable and there is no possible method of differentiating if the object is right in front of the sensor (0mm) or there is no object in front of the sensor.
The Tutorial
Requirements
Hardware

Arduino Uno (but any other would work too as long as you can get it to communicate with the PC).
Sharp GP2D120 distance sensor
Arduino IDE

arduino_items
Software
Setting up the Hardware

This tutorial assumes you have an Arduino connected to your computer for programming. Your sensor would have come a cable that consists of three wires; red, black and white. One end of the cable should have a JST female connector and the other end should be three bare wires. Go ahead and connect the JST female connector into your sensor; there is only one right way the cable will go in.

Following are what the three different wires are:

Red (VCC) -- Connects to the positive terminal of the sensor
Black (GND) -- Connects to the ground terminal of the sensor
White (VO) -- Connects to the analogue output from the sensor

Ensure that your Arduino is powered off. Now insert the red wire into the +5V power pin and the white wire into the ground power pin of the Arduino. Insert the white wire into the A0 analogue pin. Plugging in the hardware is now complete. Let's move on to the programming bit.
arduino_setup
Programming the Arduino

First we define which pin the distance sensor is connected to:

#define ds_pin 0 // Pin to which the distance sensor is connected

TIP: Instead of writing the pin numbers in between the code #define them on top so if you want to check or decide to change the pin to which your device is connected you can quickly do it here.

int ds_value; // Integer to hold the sensor's reading

The integer ds_value is the variable that we will use to store the sensors value.

To program template for an Arduino requires the following two functions:

void setup ()
{

// This function runs immediately after the Arduino is turned on and only runs once
}

void loop ()
{
// This function keeps looping and is the main function to write your code in
}

Go ahead and initialize the serial port with the following code in the setup loop:

Serial.begin (9600); // Initiates serial communication at 9600 baud

Note: The arduino can communicate over the serial port with a lot of baud rates (bits per second). You can look these up in Arduino's reference guide. Do keep this number in mind because when reading the serial port on your PC you will have to set it to that baud rate.

Let's now enter the first bit of code for our loop function to read the distance sensor values

ds_value = analogRead (ds_pin); // Reads the distance sensor

This distance sensor outputs an analogue voltage in the range of 0 -- 3V (approx.) whereas the analogRead (); command takes the reference voltage to be 5V and gives the output in the range of (0 -- 1024) i.e. a value of 5V read using analogRead (); gives the result of 1024. We therefore need to scale our ds_value. Since the maximum value we can get with our sensor is 3 the function will return a maximum value of

,3-5.?1024?615
Therefore:

ds_scaledValue = ((float)ds_value / 615) * 1024; // Calculates the scaled value

Finally, we output this value to the PC

Serial.println ((int)ds_scaledValue); // Screams out the scaled value as integer

Your final code should look like this:

#define ds_pin 0 // Pin to which the distance sensor is connected
int ds_value; // Integer to hold the sensor's reading

float ds_scaledValue; // Distance sensor's scaled value

void setup ()
{
Serial.begin (9600); // Initiates serial communication at 9600 baud
}

void loop ()
{
ds_value = analogRead (ds_pin); // Reads the distance sensor
ds_scaledValue = ((float)ds_value / 615) * 1024;
// Calculates the scaled value

Serial.println ((int)ds_scaledValue); // Screams out the scaled value as integer
delay (200); // Delay of 200ms
}
Running the Program

Finally! We're all done. All we need to do now is run the system so go ahead and turn on your Arduino. Ensure that it is connected to the PC. Upload the program to the PC and press the serial monitor icon run_program in the top left.

You should now see your Arduino now sending values to you that are the scaled distance sensor values.
Calibrating the Sensor

The datasheet for the sensor will provide you with a graph that shows how the value of the sensor relates the distance between it and the object. These graphs are for an ideal sensor. Every sensor is different (several factors affect the sensors reliability e.g. how old it is, how clean the lens is, how reflective the object is) which is why you will have to calibrate your sensor yourself even if you have multiple new just unboxed sensors.

For example, if you are using the sensor to check if there is something between 50mm and 200mm of the sensor.

#define ds_pin 0 // Pin to which the distance sensor is connected
#define ds_scaledResolution 1024

#define ds_valueAt50 900 // The value the sensor returns at 50mm distance
#define ds_valueAt200 200 // The value the sensor returns at 200mm distance

int ds_value; // Integer to hold distance sensor's reading
float ds_scaledValue; // Distance sensor's scaled value

void setup ()
{
Serial.begin (9600); // Initiates serial communication at 9600 baud
}

void loop ()
{
ds_value = analogRead (ds_pin); // Reads the distance sensor
ds_scaledValue = ((float)ds_value / 615) * ds_scaledResolution; // Calculates the scaled value
if ((ds_scaledValue = ds_valueAt200)) // Checks if any object in range
{
Serial.println ("Object detected!"); // Screams if object found
}
}

Secrets of How to Show How Much You Love Him How Do I Show My Boyfriend I Really Love Him

Image source: http://www.hug2love.com/wp-content/uploads/2015/10/How-Much-I-Love-You-Poems-for-him-and-her-images.jpg Secrets of How to Show...