I'm using the DYP-ME007TX sensor with arduino uno. The output from this sensor comes in packets of 4-bytes:
Data[0]= 0xFF: Packet Header
Data[1]= 0x0-0xFF: Data Most Significant Byte (MSB)
Data[2]= 0x0-0xFF: Data Least Significant Byte (LSB)
Data[3]= 0x0-0xFF: Data Checksum: = (0XFF + MSB + LSB) & 0xFF
Data is in MILLIMETERS
16 bit value in mm = Data[1] << 8 | Data[2]
How can exctract this packet in arduino sketch and extract millimeter number?
Ricardo
3,3902 gold badges26 silver badges55 bronze badges
-
This is a serial data stream. Connect the device's OUT to your Arduino's RX, then read the documentation on the Serial library.Edgar Bonet– Edgar Bonet2015年06月16日 12:00:10 +00:00Commented Jun 16, 2015 at 12:00
-
I want to use two and more this kind of sensors, so I think I just can't use Arduino's TX. I used SoftwareSerial library: arduino.cc/en/Tutorial/TwoPortReceiveAmon Olimov– Amon Olimov2015年06月17日 01:49:57 +00:00Commented Jun 17, 2015 at 1:49
1 Answer 1
I found the answer from forum.arduino.cc There is a sketch
#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--
Serial.print("PING: ");
Serial.print(mili/10);
Serial.println("sm");
delay(500);
}
lang-cpp