首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Arduino超声波距离传感器

Arduino超声波距离传感器
EN

Stack Overflow用户
提问于 2013-07-03 20:25:12
回答 3查看 3K关注 0票数 1

我正在尝试从我的传感器接收信息,但是我的输出总是只有0,是不是我的代码中有什么问题?所有与硬件相关的事情都做得很好。

代码语言:javascript
复制
loop()
{
    long duration, inches, cm;

    pinMode(pingPin, OUTPUT);
    digitalWrite(pingPin, LOW);
    delayMicroseconds(2);
    digitalWrite(pingPin, HIGH);
    delayMicroseconds(5);
    digitalWrite(pingPin, LOW);

    pinMode(pingPin, INPUT);
    duration = pulseIn(pingPin, HIGH);

    inches = microsecondsToInches(duration);
    cm = microsecondsToCentimeters(duration);

    Serial.print(inches);
    Serial.print("in; ");
    Serial.print(cm);
    Serial.print("cm");
    Serial.println();
}

long microsecondsToInches(long microseconds)
{
    return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
    return microseconds / 29 / 2;
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-07-03 22:33:52

经过多次尝试,并感谢John b的帮助,这被认为是如何正确使用这种传感器的正确答案,它完全按照需要工作,输出得到完美的测量。

代码语言:javascript
复制
#include <SoftwareSerial.h>

// TX_PIN is not used by the sensor, since that the it only transmits!
#define PING_RX_PIN 6
#define PING_TX_PIN 7

SoftwareSerial mySerial(PING_RX_PIN, PING_TX_PIN);

long inches = 0, mili = 0;
byte mybuffer[4] = {0};
byte bitpos = 0;

void setup() {
  Serial.begin(9600);

  mySerial.begin(9600);
}


void loop() {
  bitpos = 0;
  while (mySerial.available()) {
    // the first byte is ALWAYS 0xFF and I'm not using the checksum (last byte)
    // if your print the mySerial.read() data as HEX until it is not available, you will get several measures for the distance (FF-XX-XX-XX-FF-YY-YY-YY-FF-...). I think that is some kind of internal buffer, so I'm only considering the first 4 bytes in the sequence (which I hope that are the most recent! :D )
    if (bitpos < 4) {
      mybuffer[bitpos++] = mySerial.read();
    } else break;
  }
  mySerial.flush(); // discard older values in the next read

  mili = mybuffer[1]<<8 | mybuffer[2]; // 0x-- : 0xb3b2 : 0xb1b0 : 0x--
  inches = 0.0393700787 * mili;
  Serial.print("PING: ");
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(mili);
  Serial.print("mili");
  Serial.println();

  delay(100);
}
票数 1
EN

Stack Overflow用户

发布于 2013-07-03 21:18:35

您的传感器不使用PWM作为发送距离的方式。相反,它使用串行连接。问题是Arduino上没有额外的串行硬件。

您可以使用arduino的串行端口读取传感器数据,但不能将任何内容记录到屏幕上

串行接口的运行速度为9600波特,这对于软件仿真来说是很快的。

我建议你买一个使用标准PWM通信模式的传感器。这样做会省去几个令人头疼的.But,我应该告诉你有一种方法。它使用的是软件串行库。该库将帮助您像使用serail管脚一样使用数字管脚。

http://arduino.cc/en/Reference/SoftwareSerial

http://www.suntekstore.com/goods-14002212-3-pin_ultrasonic_sensor_distance_measuring_module.html

http://iw.suntekstore.com/attach.php?id=14002212&img=14002212.doc

票数 2
EN

Stack Overflow用户

发布于 2013-07-03 20:55:21

您使用的是Parallax PING的代码,它使用的协议与您已有的协议不同。Here is a link to the datasheet of your sensor。它以每50ms 9600bps的速率通过标准串口输出。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17447918

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档