Sharp_GP2Y0A 红外测距传感器 Arduino读取

Sharp_GP2Y0A

GP2Y0A02YK0F, GP2Y0A710K0F Arduino 程序

GP2Y0A02YK0F

Sharp_GP2Y0A 红外测距传感器 Arduino读取_第1张图片
传感器参数:

  • 20~150cm Analog output type.
  • 4.5 to 5.5V, 33mA
  • Block diagram
    Sharp_GP2Y0A 红外测距传感器 Arduino读取_第2张图片
  • Timing Chart
    Sharp_GP2Y0A 红外测距传感器 Arduino读取_第3张图片
  • Output
    Sharp_GP2Y0A 红外测距传感器 Arduino读取_第4张图片

参考V-cm图, 用查表法, 连接Vout到Arduino的A0引脚, 测量频率20Hz.

//SHARP GP2Y0A02
//Yellow Vout
//Black GND
//Red 5V

float distance[] = {20, 30, 40, 50, 60, 70, 80, 90, 100,  110,  120,  130,  140,  150};
float voltage[14] = {2.5, 2, 1.55, 1.25, 1.1, 0.85, 0.8, 0.73, 0.7, 0.65, 0.6, 0.5, 0.45, 0.4};

typedef struct {
  float maxDistance;  //cm
  float minDistance;  //cm
  float offset; //cm
  float distance; //cm, 
  int frequency;  //Hz
  int pin;
}SHARP;

SHARP Sharp = {150, 20, 0, 0, 20, A0};

void getDistance(SHARP* Sharp) {
  float v = analogRead(Sharp->pin);
  v = v / 1024.0 * 5;
  int index = 0;
  for(index = 0; index < 14; index++) {
    if(v >= voltage[index]) {
      break;
    }
  }
  if(index == 0) {
    Sharp->distance = 20;
  } else if(index == 14) {
    Sharp->distance = 150;
  } else {
    Sharp->distance = map(v, voltage[index], voltage[index-1], distance[index], distance[index-1]);
  }
}

void setup() {
  Serial.begin(115200);
}

void loop() {
  static unsigned long lastTime = millis();
  if(millis() - lastTime > 1000/Sharp.frequency) {
    lastTime = millis();
    getDistance(&Sharp);
    Serial.println(Sharp.distance);
//    int v = analogRead(Sharp.pin);
//    Serial.println(v);
  }  
}

GP2Y0A710K0F

Sharp_GP2Y0A 红外测距传感器 Arduino读取_第5张图片

传感器参数:

  • 100~550cm Analog output type
  • 4.5~5.5V, 30mA
  • Block diagram
    Sharp_GP2Y0A 红外测距传感器 Arduino读取_第6张图片
  • timing chart
    Sharp_GP2Y0A 红外测距传感器 Arduino读取_第7张图片
  • Output
    Sharp_GP2Y0A 红外测距传感器 Arduino读取_第8张图片

一开始用的公式法, 参考的是V-1/cm图, 结果测出来数据有点糟糕, 跳来跳去. 后来用查表法. 连接Vout到Arduino的A0引脚, 测量频率40Hz.

Github

代码参考我的Github: Sharp_GP2Y0A

你可能感兴趣的:(EE,红外测距,夏普,Arduino,查表法,公式法)