Advertisement
microrobotics

TF-Luna Range Sensor 0.2-8m

Mar 31st, 2023
703
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. The TF-Luna is a compact LiDAR range sensor that can measure distances from 0.2 to 8 meters. It uses UART communication to send distance and strength data. Here's an example of how to use the TF-Luna with an Arduino to read the distance and display it on the Serial Monitor:
  3.  
  4. To wire the TF-Luna sensor, connect its 5V pin (or 3.3V pin, depending on the sensor model) to the corresponding power pin on the Arduino and the GND pin to the ground. Connect the sensor's TX pin to the RX_PIN (pin 10) on the Arduino and the RX pin to the TX_PIN (pin 11) on the Arduino.
  5.  
  6. This code will continuously read the distance and signal strength from the TF-Luna sensor and print them on the Serial Monitor. The configureTFLuna() function is optional and can be used to change the sensor's configuration according to the datasheet.
  7. */
  8.  
  9. #include <SoftwareSerial.h>
  10.  
  11. const int RX_PIN = 10; // TF-Luna RX connected to Arduino pin 10
  12. const int TX_PIN = 11; // TF-Luna TX connected to Arduino pin 11
  13.  
  14. SoftwareSerial tfLunaSerial(RX_PIN, TX_PIN); // Declare a SoftwareSerial object
  15.  
  16. void setup() {
  17.   Serial.begin(9600);
  18.   tfLunaSerial.begin(115200); // TF-Luna default baud rate is 115200
  19.  
  20.   delay(500); // Wait for the sensor to initialize
  21.   configureTFLuna(); // Configure the TF-Luna sensor
  22. }
  23.  
  24. void loop() {
  25.   int distance = -1;
  26.   int strength = -1;
  27.  
  28.   if (readTFLunaData(distance, strength)) {
  29.     Serial.print("Distance: ");
  30.     Serial.print(distance);
  31.     Serial.print(" cm, Strength: ");
  32.     Serial.println(strength);
  33.   } else {
  34.     Serial.println("Failed to read data from TF-Luna");
  35.   }
  36.  
  37.   delay(500); // Wait 500ms between readings
  38. }
  39.  
  40. void configureTFLuna() {
  41.   // This function sends the command to configure the TF-Luna (optional)
  42.   // Check the datasheet for other possible configurations
  43.   byte configCommand[] = {0x5A, 0x05, 0x0A, 0x00, 0x00, 0x01, 0x60};
  44.   tfLunaSerial.write(configCommand, sizeof(configCommand));
  45.   delay(100);
  46. }
  47.  
  48. bool readTFLunaData(int &distance, int &strength) {
  49.   const int FRAME_SIZE = 9;
  50.   byte frame[FRAME_SIZE];
  51.  
  52.   // Look for the start of a valid frame
  53.   while (tfLunaSerial.available()) {
  54.     if (tfLunaSerial.read() == 0x59) {
  55.       if (tfLunaSerial.available() && tfLunaSerial.read() == 0x59) {
  56.         // Read the rest of the frame
  57.         tfLunaSerial.readBytes(frame, FRAME_SIZE);
  58.  
  59.         // Calculate checksum
  60.         byte checksum = 0;
  61.         for (int i = 0; i < 8; i++) {
  62.           checksum += frame[i];
  63.         }
  64.  
  65.         // Check if checksum matches
  66.         if (checksum == frame[8]) {
  67.           distance = frame[0] | (frame[1] << 8);
  68.           strength = frame[2] | (frame[3] << 8);
  69.           return true;
  70.         }
  71.       }
  72.     }
  73.   }
  74.  
  75.   return false;
  76. }
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement