YDLidarG2
Minimal YDLidar G2 driver for the Arduino Uno R4.
Wiring
| G2 pin | Uno R4 pin |
|---|---|
| TX | 0 (Serial1 RX) |
| RX | 1 (Serial1 TX) |
| GND | GND |
| VCC motor | External 5V (share GND) |
| VCC logic | 3.3V |
| DTR | Any digital out (motor enable) |
Don’t use
Serial(USB) for the lidar — useSerial1on 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
| Method | Description |
|---|---|
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)