Sharp_GP2Y0A 红外测距传感器 Arduino读取

Sharp_GP2Y0A

GP2Y0A02YK0F, GP2Y0A710K0F Arduino 程序

GP2Y0A02YK0F

这里写图片描述
传感器参数:

  • 20~150cm Analog output type.
  • 4.5 to 5.5V, 33mA
  • Block diagram
    block
  • Timing Chart
    这里写图片描述
  • Output
    这里写图片描述

参考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

这里写图片描述

传感器参数:

  • 100~550cm Analog output type
  • 4.5~5.5V, 30mA
  • Block diagram
    这里写图片描述
  • timing chart
    这里写图片描述
  • Output
    这里写图片描述

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

Github

代码参考我的Github: Sharp_GP2Y0A

  • 5
    点赞
  • 73
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值