Ted
Ted

Reputation: 639

Arduino ultrasonic distance sensor

I am trying to receive information from my sensor, however my output is just 0 all the time, is there something wrong in my code? Everything hardware related is done well.

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;
}

Upvotes: 1

Views: 3179

Answers (3)

Ted
Ted

Reputation: 639

After many tries and thanks to the help of John b, this was figured out as the proper answer on how to use this kind of sensor properly, it works exactly as needed, the output being perfectly measured

#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);
}

Upvotes: 1

John b
John b

Reputation: 1398

The sensor you have does not use PWM as a way of sending distance. Rather it uses a serail connection. The issue is that you do NOT have a extra serial hardware on the Arduino.

You could use the arduino's serail port to read the sensors data, but you would not be able to log anything to the screen

The speed at which the serial connection runs is 9600 baud, which is to fast to emulate in software.

I advice you to buy a sensor that uses the standard PWM mode of communication. Doing this will save a several headaches .But I should tell you there is a way. It is using the software serial library. The library will help you use digital pins like they are serail pins.

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

Upvotes: 2

user2461391
user2461391

Reputation: 1433

You are using code for the Parallax PING, which uses a different protocol from the one you have. Here is a link to the datasheet of your sensor. It outputs through standard serial at 9600bps every 50ms.

Upvotes: 1

Related Questions