notes/Resources/sensors/L04.md

57 lines
1.5 KiB
Markdown
Raw Permalink Normal View History

2024-02-05 13:38:33 +01:00
# L04 Underwater Sonar
![](Resources/sensors/L04-5.jpg)
[Datasheet](https://cdn-shop.adafruit.com/product-files/4664/4664_datasheet.pdf)
## Cable Descriptions
| Color | Name | Description | Remarks |
|------------|------------|-------------------|---------|
| red | VCC Power | input cable | 3.3V~5V |
| black | GND Power | ground cable | // |
| white | RX Trigger | input cable | (1) |
| yellow | TX UART | output cable | (1) |
## UART Data Frames
| Frame data | Instruction | Byte |
|--------------|---------------|--------|
| Frame header | Fixed to 0xFF | 1 byte |
| Data_H High | 8 bits of distance data | 1 byte
| Data_L Low | 8 bits of distance data | 1 byte
| SUM | Checksum of communication | 1 byte
```c
#include <SoftwareSerial.h>
unsigned char buffer_RTT[4] = {0};
uint8_t CS;
#define COM 0x55
SoftwareSerial mySerial(7, 8);
void setup() {
Serial.begin(115200);
mySerial.begin(115200);
}
void loop() {
mySerial.write(COM);
delay(100);
if(mySerial.available() > 0){
delay(4);
if(mySerial.read() == 0xff){
buffer_RTT[0] = 0xff;
for (int i=1; i<4; i++){
buffer_RTT[i] = mySerial.read();
}
CS = buffer_RTT[0] + buffer_RTT[1]+ buffer_RTT[2];
if(buffer_RTT[3] == CS) {
int distance = (buffer_RTT[1] << 8) + buffer_RTT[2];
Serial.print("Distance:");
Serial.print(distance);
Serial.println("mm");
}
}
}
}
```