Skip to content

Sensor de distancia SHARP

javierre edited this page May 15, 2019 · 11 revisions

Sensor de distancia SHARP 2y0a21

SHARP

Este sensor óptico tiene un rango de 10-80cm (aunque modelos similares tienen otros rangos).

SHARP Pinout

Es importante saber que SHARP devuelve la distancia en valores analógicos, por tanto es importante que el pin de datos esté conectado a un pin analógico. En el caso de NodeMCU sólo existe un pin analógico: A0.

SHARP Fritzing

/*
 * getDistance
 *
 * Example of using SharpIR library to calculate the distance beetween the sensor and an obstacle
 *
 * Created by Giuseppe Masino, 15 June 2016
 *
 * -----------------------------------------------------------------------------------
 *
 * Things that you need:
 * - Arduino
 * - A Sharp IR Sensor
 *
 *
 * The circuit:
 * - Arduino 5V -> Sensor's pin 1 (Vcc)
 * - Arduino GND -> Sensor's pin 2 (GND)
 * - Arduino pin A0 -> Sensor's pin 3 (Output)
 *
 *
 * See the Sharp sensor datasheet for the pin reference, the pin configuration is the same for all models.
 * There is the datasheet for the model GP2Y0A41SK0F:
 *
 * http://www.robotstore.it/open2b/var/product-files/78.pdf
 *
 */

//import the library in the sketch
#include <SharpIR.h>

//MODELS

//GP2YA41SK0F
//GP2Y0A21YK0F
//GP2Y0A02YK0F


//Create a new instance of the library
//Call the sensor "sensor"
//The model of the sensor is "GP2Y0A21YK0F"
//The sensor output pin is attached to the pin A0
SharpIR sensor( SharpIR::GP2Y0A21YK0F, A0 );

void setup()
{
  Serial.begin( 9600 ); //Enable the serial comunication
}

void loop()
{
  int distance = sensor.getDistance(); //Calculate the distance in centimeters and store the value in a variable

  Serial.println( distance ); //Print the value to the serial monitor
}

Ver código

Este sensor requiere de la instalación de la siguiente librería:

  • SharpIR

    Instalar librería


Más información

Clone this wiki locally