← Back to the archive
Open Source Advanced Tech SoftwareArduinoLibraryATtiny

Project

Arduino Uno R4 YDLidar G2 Library

Uno R4 Library for the YDLidar G2

Easy build
Completed Updated April 2026 Arduino Developers 6 clones GitHub ↗

YDLidarG2

Minimal YDLidar G2 driver for the Arduino Uno R4.

Wiring

G2 pinUno R4 pin
TX0 (Serial1 RX)
RX1 (Serial1 TX)
GNDGND
VCC motorExternal 5V (share GND)
VCC logic3.3V
DTRAny digital out (motor enable)

Don’t use Serial (USB) for the lidar — use Serial1 on pins 0/1.

Installation

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

Usage

#include "YDLidarG2.h"

YDLidarG2 lidar(Serial1, 3); // Serial1, motor-enable on pin 3

void setup() {
    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 serial + motor pin, call once in setup()
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 (out of 32 KB on Uno R4)
  • Multiple hits per degree bucket keep the closest/strongest reading
  • Checksum is computed but not enforced (known issue in reference libs too)