YDLidarG2
Minimal YDLidar G2 driver for the ESP32-S3.
Wiring
| G2 pin | ESP32-S3 pin |
|---|---|
| TX | 18 (Serial1 RX) |
| RX | 17 (Serial1 TX) |
| GND | GND |
| VCC motor | External 5V (share GND) |
| VCC logic | 3.3V |
| DTR | Any digital out (motor enable) |
Unlike the Uno R4 version, you must call
lidarSerial.begin()with your chosen RX/TX pins before callinglidar.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
| Method | Description |
|---|---|
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)