RPLIDAR C1 arduino C++ code needed

Trying to read data from C1 tof scanner. Can;t even connect to unit. I have tried the official RPLIDAR github code without success. RPLidar examples typically call for using a PWM pin to start motor. There is no PWM wire on the four wire C1 connector. Just 5v, gnd, Tx, Rx. I am using teensy 4.1 serial1. I have RPLidar a2 and can read that data without issue. There are no arduino code examples that I could find for the C1 TOF sensor. I have contacted RPLidar..maybe they will reply. Does anyone have a link or example that they want to share? Thx for any help!

C1 TOF sensor is different from A2 .... it don’t need a PWM motor pin ...... the motor is internal. It only uses 5V, GND, Tx, Rx and communicates via UART ..... That’s why A2 libraries won’t work.

You’ll need C1-specific SDK or example code from RPLIDAR to read distance data.

Thx for reply. I know, thats what am a searching for. I have reviewed sdk libraries for ROS and pi. Not what i need. And btw, you can open up the C1 case and hard wire a pwm control wire.

Install Driver for the USB cable

The USB cable converts UART to USB by using CP2102 chip. You need to install
the device driver for the chip. The driver can be found in the provided SDK
package or downloaded from Silicon Labs’ official website: Silicon Labs

Support is available... https://service.waveshare.com/

thx for reply, I have the USB chip interface. I do not want to convert for remote reading via desktop and some data interface GFX program. I want to use teensy Tx Rx directly for embedded device. So, I’m looking for a library which has ported the serial commands for C1 TOF to arduio C++ format.

Assuming "unit" is the LIDAR, and connect is from "teensy". Does the teensy show in your Device Manager?

Thx for reply. The teensy has a high speed ARM cortex m7 MCU which uses the arduino environment for programming (among others). The teensy 4.1 has at least 4+ dedicated serial(1+) ports and a dedicated USB serial port. Since I can;t find a dedicated RPLIDAR C1 library with control and read byte info, my next step will be to configure the teensy serial1 port as a USB hub and try to read the C1 LIDAR scanner using the RPLidar USB interface. I did not really want extra hardware, but…..let’s see what happens…

I have been on contact with the RPLidar team. they say they will port the ROS SDK to usable Arduino style code library. This will be cool if they do it because the C1 TOF scanner is a really nice compact unit with protected spinny parts! I will update this with code link if it gets done.

Well, the RPLidar team never replied again after first two emails. So, after quite a bit of searching, I found that a few months ago, a code was written based on the RP library, THX Robotto!:

so download and unpack Robotto’s lib to arduino lib, and try this code below. Make sure ALL ports are set to 460800 or you get garbage. Change Tx Rx pins to 16 and 17 (for wroom esp32 module) and make they are connected correctly to the C1. All functions including PWM control is by UART, so no need to wire up the C1 board's PWM pin…

/*
 * RPLidarC1 Arduino Library Example
 * 
 * This example demonstrates how to use the RPLidarC1 library with ESP32
 * to communicate with an RPLidar C1 laser scanner.
 * 
 * - RPLidar VCC (RED) -> 5V
 * - RPLidar GND (BLACK) -> GND  
 * - RPLidar TX (YELLOW) -> GPIO 33 (RX2)
 * - RPLidar RX (GREEN) -> GPIO 26 (TX2)
 * 
 * Note: RPLidar C1 uses UART-based motor control (no separate motor pin needed)
 * Make sure your ESP32 can provide enough current for the RPLidar!
 */

#include "RPLidarC1.h"


#define RXpin 16
#define TXpin 17

HardwareSerial C1UART(2);//use serial '2' port ESP32

// Create RPLidar instance using Serial2 (C1 uses UART motor control)
RPLidarC1 lidar(&C1UART);
RPLidarHealth health;

void setup() {
  Serial.begin(460800);
  delay(2000);

  Serial.println("RPLidarC1 Arduino Library Example");
  Serial.println("==================================");

  // Initialize the lidar
  Serial.println("Initializing RPLidar...");
  if (!lidar.begin(460800, 4000, RXpin, TXpin)) {
    Serial.println("Failed to initialize RPLidar!");
    Serial.println("Check connections and power supply.");
    while (1) {
      delay(1000);
    }
  }

  Serial.println("RPLidar initialized successfully!");

  // Check health status

  if (lidar.get_health(&health)) {
    lidar.print_health(&health);

    if (health.status != RPLIDAR_STATUS_OK) {
      Serial.println("Warning: RPLidar is not in good health!");
      if (health.status == RPLIDAR_STATUS_ERROR) {
        Serial.println("Attempting to reset...");
        lidar.reset();
        delay(2000);
      }
    }
  } else {
    Serial.println("Failed to get health status");
  }

  // Start scanning
  Serial.println("Starting scan...");
  if (lidar.start_scan()) {
    Serial.println("Scan started successfully");
  } else {
    Serial.println("Failed to start scan");
    while (1) {
      delay(1000);
    }
  }

  Serial.println("Reading measurements...");
  Serial.println("Format: Angle(deg) Distance(mm) Quality StartFlag");
  Serial.println("================================================");
}

bool run = true;

void loop() {
  RPLidarMeasurement measurement;

  // Get a single measurement
  if (run) {
    if (lidar.get_measurement(&measurement)) {
      // Print measurement data
      Serial.print("Angle: ");
      Serial.print(measurement.angle, 2);
      Serial.print("°  Distance: ");
      Serial.print(measurement.distance, 1);
      Serial.print("mm  Quality: ");
      Serial.print(measurement.quality);
      Serial.print("  Start: ");
      Serial.print(measurement.start_flag ? "Y" : "N");
      Serial.print("  Time: ");
      Serial.println(measurement.timestamp);

      // Optional: Filter out invalid measurements (C1 specific ranges)
      if (measurement.distance < 5.0 || measurement.distance > 12000.0) {
        Serial.println("  -> Invalid distance reading");
      }

      if (measurement.quality < 15) {  // C1 has different quality scaling
        Serial.println("  -> Low quality reading");
      }

    } else {
      Serial.println("Failed to get measurement");
      delay(10);
    }
  }

  static unsigned long last_health_check = 0;
  if (millis() - last_health_check > 30000) {
    last_health_check = millis();

    RPLidarHealth health;
    if (lidar.get_health(&health)) {
      if (health.status != RPLIDAR_STATUS_OK) {
        Serial.println("\nWarning: Health status changed!");
      }
      lidar.print_health(&health);
    }
  }

  if (millis() > 10000 && run) {
    run = false;
    Serial.println("Done after 10 seconds..");
    lidar.stop_scan();
    lidar.reset();
    // while(1) {delay(10);}
  }
  delay(1);  // Small delay to prevent overwhelming the serial output
}

Runs for ten seconds, check your serial monitor for output, should see this:

RPLidarC1 Arduino Library Example
==================================
Initializing RPLidar...
Resetting Lidar... TX: A5:40
Sent!
TX: A5:52
Got 7 bytes!
Angle: 165.50°  Distance: 4780.8mm  Quality: 21  Start: N  Time: 5254

Angle: 164.58°  Distance: 4804.3mm  Quality: 20  Start: N  Time: 5255

Angle: 163.40°  Distance: 4826.3mm  Quality: 21  Start: N  Time: 5256

Angle: 162.10°  Distance: 4848.8mm  Quality: 21  Start: N  Time: 5260

Angle: 161.02°  Distance: 4870.8mm  Quality: 20  Start: N  Time: 5261

Angle: 160.14°  Distance: 4891.8mm  Quality: 11  Start: N  Time: 5262
 

In case anyone is interested, here is the RPlidar C1 code parsed correctly for use on for example, a teensy 4.1. works well...you may have to invert LEFT RIGHT orientation...

#include <Arduino.h>

// Use Hardware Serial 1 on Teensy 4.1 (Pins 0 and 1)
#define LidarSerial Serial1

// Slamtec standard request byte markers
const uint8_t SYNC_BYTE = 0xA5;
const uint8_t START_SCAN = 0x20;

// Parser structure for the 5-byte data packet
uint8_t packetBuffer[5];
int packetIndex = 0;

// Function declarations
void sendStartScanCommand();

void setup() {
  // Initialize USB Serial Monitor
  Serial.begin(115200);
  while (!Serial && millis() < 3000); // Wait for Serial Monitor with timeout

  // C1 uses a strict 460800 baud rate over TTL UART
  LidarSerial.begin(460800);
  
  Serial.println("Teensy 4.1 Initializing RPLidar C1...");
  delay(1000); 

  // The C1 motor starts and scans automatically via UART commands
  sendStartScanCommand();
}

void loop() {
  while (LidarSerial.available() > 0) {
    uint8_t currentByte = LidarSerial.read();

    // Look for a valid 5-byte measurement packet frame
    if (packetIndex == 0) {
      // Byte 0 contains the inverse of the start bit in bit 1, and bit 0 is always 1
      // Valid data point synchronization checks
      bool bit0 = currentByte & 0x01;
      bool bit1 = (currentByte >> 1) & 0x01;
      
      if (bit0 != bit1) { // Check for proper framing sync
        packetBuffer[packetIndex++] = currentByte;
      }
    } 
    else if (packetIndex == 1) {
      // Byte 1 check: Bit 0 must always be 1 for a valid data point packet
      if (currentByte & 0x01) {
        packetBuffer[packetIndex++] = currentByte;
      } else {
        packetIndex = 0; // Invalid packet sequence, reset
      }
    } 
    else {
      packetBuffer[packetIndex++] = currentByte;

      // Full 5-byte structure successfully populated
      if (packetIndex >= 5) {
        // Parse Quality, Angle, and Distance
        uint8_t quality = packetBuffer[0] >> 2;
        
        // Extract 15-bit Angle data 
        uint16_t rawAngle = (packetBuffer[1] >> 1) | (packetBuffer[2] << 7);
        float angle = rawAngle / 64.0; // Scaled to degrees
        
        // Extract 16-bit Distance data
        uint16_t rawDistance = packetBuffer[3] | (packetBuffer[4] << 8);
        float distance = rawDistance / 4.0; // Scaled to millimeters

        // Output real-time coordinates if quality is reliable
        if (quality > 0 && distance > 0) {
          Serial.print("Angle: ");
          Serial.print(angle, 2);
          Serial.print("°\tDistance: ");
          Serial.print(distance, 1);
          Serial.print("mm\tQuality: ");
          Serial.println(quality);
        }

        packetIndex = 0; // Reset for next measurement frame
      }
    }
  }
}

// Forces the Lidar out of IDLE and into standard scanning mode
void sendStartScanCommand() {
  Serial.println("Sending SCAN request sequence to C1...");
  LidarSerial.write(SYNC_BYTE);
  LidarSerial.write(START_SCAN);
}