← Back to the archive
Open Source Advanced Tech SoftwareESP32LibraryATtiny

Project

ESP32 YDLidar G2 Library

ESP32 Library for the YDLidar G2

Easy build
Completed Updated April 2026 ESP32 Developers 17 clones GitHub ↗

YDLidarG2

Minimal YDLidar G2 driver for the ESP32-S3.

Wiring

G2 pinESP32-S3 pin
TX18 (Serial1 RX)
RX17 (Serial1 TX)
GNDGND
VCC motorExternal 5V (share GND)
VCC logic3.3V
DTRAny digital out (motor enable)

Unlike the Uno R4 version, you must call lidarSerial.begin() with your chosen RX/TX pins before calling lidar.begin().

Installation

Copy the YDLidarG2_ESP32S3/ folder into your Arduino libraries/ directory and restart the IDE.

Usage

#include "YDLidarG2.h"

#define LIDAR_RX_PIN    18
#define LIDAR_TX_PIN    17
#define LIDAR_MOTOR_PIN 16

HardwareSerial lidarSerial(1);
YDLidarG2 lidar(lidarSerial, LIDAR_MOTOR_PIN);

void setup() {
    lidarSerial.begin(LIDAR_BAUD, SERIAL_8N1, LIDAR_RX_PIN, LIDAR_TX_PIN);
    lidar.begin();
    lidar.startScan();
}

void loop() {
    if (lidar.update()) {                    // true once per full revolution
        uint16_t d = lidar.getDistance(90);  // mm at 90
        Serial.println(d);
    }
}

API

MethodDescription
begin()init motor pin + flush noise; call after lidarSerial.begin()
startScan()send scan command, returns false if no response
stop()stop scanning, kill motor
update()call every loop(); returns true on completed 360° scan
getDistance(deg)mm at degree [0–359], 0 = no return
getIntensity(deg)signal strength [0–1023] at degree
getValidBinCount()number of populated degree buckets last scan
getScanPoints(count)raw G2ScanPoint[] array
getHealth(health)device health, call before startScan()
getDeviceInfo(info)firmware/hardware version, call before startScan()

Notes

  • Baud rate: 230400
  • RAM usage: ~2.6 KB static (ESP32-S3 has 512 KB)
  • Multiple hits per degree bucket keep the closest/strongest reading
  • Checksum is computed but not enforced (known issue in reference libs too)