Distance measurement using Arduino and Sharp GP2Y0A41SK0F sensor
The sensor enables distance measurement. The closer the object is to the sensor, the higher the voltage at its output. The following example can be used with any Sharp analog distance sensor.
We will need the following items:
Connecting the sensor from Arduino:
To operate the sensor with Arduino, connect the circuit as follows:
Sharp module | Pin Arduino |
---|---|
3 | 5 V |
2 | GND |
1 | A1 |
Sharp sensor leads.
The diagram of the sensor connection with Arduino Uno.
Program to Arduino
The following code was used in the example:
int sensor = A1; //pin analogue A1 connected to a sensor signal void setup(){ Serial.begin(9600); //initialisation of a serial monitor Serial.println("Distance sensor test"); } void loop(){ int odl = analogueRead (sensor); //reading value from sensor Serial.println(odl); // display it on the monitor delay(200); // delay between readings }
The result of the program can be viewed on a serial monitor:
The program displays values from 0 to 1023. To convert them to a distance in centimetres, please follow the documentation of the respective module.