This page has moved into the new guide: Ranger 8-Way › Arduino › Arduino IDE

Laser Ranger 8-ch × Arduino I2C (universal I2C edition)

Back to Laser Ranger 8-ch

The universal I2C edition of the Laser Ranger 8-ch speaks MBC I2C protocol v2: write one id, read 2 bytes = one int16 little-endian value; consecutive ids can be read in one go. This page has the value table, commands and wiring for this product; the shared rules (F0~F5 version & status, F8 re-address, EF reboot) are on the universal I2C protocol page.

SPIKEEV3Arduino I2CmBlockMATRIXmicro:bit

Tip

New to coding? Start with the Arduino examples; every sketch is copy-and-upload. The six-host reading of every value is in the reference.

Basics

Item Detail
I2C address 0x29(change with F8)
F0 device ID low byte 0x29、high byte 8
Model LASER8
Protocol F1 2
Speed 100 kHz (recommended) / 400 kHz; wait 2 ms after writing the id
Read 主機寫 [編號] → 等 2 ms → 讀 2×k byte(k≤16)= 從該編號起連續 k 個 int16 小端;空號回 -32768
Snapshot updated every 10 ms; a multi-value read is atomic

Wiring

Laser Ranger 8-ch × Arduino UNO
Sensor Arduino UNO / Nano Arduino R4 ESP32
SDA A4 SDA GPIO 21
SCL A5 SCL GPIO 22
5V 5V 5V 5V
GND GND GND GND

Works directly with 5 V hosts, no level shifter; two products with the same address cannot share a bus (re-address one with F8 first).

Value table

Each id returns one int16; click an id for the six-host reading and figure.

Id Value Range Meaning
01 nearest distance (cm) nearCm 0~120 cm smallest of the 8 distances, 0~120 cm (a failed sensor counts as 120); 120 = nothing around
02 nearest direction nearDir -8~+8 direction of the nearest object: −7 (left) to +7 (right), 0 = straight ahead, negative = left; ±8 = nothing around (keeps the last side)
03 farthest distance (cm) farCm 0~120 cm largest of the 8 distances, 0~120 cm (a failed sensor always reads 120 and looks 'farthest')
04 farthest direction farDir -7~+7 direction of the most open side: −7 to +7, 0 = straight ahead, negative = left; 0 when all 8 read 120
05 front distance (cm) frontCm 0~120 cm average of sensors 4 and 5 (both facing forward), 0~120 cm; EV3 mode 3 and SPIKE mode 6 carry the same thing in mm; 0x22 'C' is min(4, 5), not the average (see 10)
06 nearest distance (mm) nearMm 0~1200 mm smallest of the 8 mm readings, 0~1200 (failed sensor counts as 1200); 1200 = nothing around
07 init bitmap initOk 0~255 bit bit0 = sensor 1 (left) … bit7 = sensor 8; 1 = initialised at power-on (8 sensors take 24 s, no retry); 255 = all OK
08 fault bitmap fault 0~255 bit bit0 = sensor 1 … bit7 = sensor 8; 1 = currently failing (I2C error or no update for 200 ms); its distance reads 120 / 1200
09 front distance (mm) frontMm 0~1200 mm average of sensors 4 and 5 (the two facing forward), 0~1200 mm; 1200 = nothing ahead. cm version is 05
10~17 sensor 1~8 distance (cm) distCm 0~120 cm distance of each sensor 18, 0120 cm (1 = far left, 4 and 5 = front, 8 = far right, 30° apart); 120 = nothing that way / sensor failed; ids 10~17
18~1F sensor 1~8 distance (mm) distMm 0~1200 mm distance of each sensor 18, 01200 mm; 1200 = nothing that way / sensor failed; ids 18~1F

Status flags F3

bit Name Meaning
b0 ready (ready) 1 once all 8 sensors are initialised (24 s after power-on) and snapshots are running
b1 any sensor failed (fault) 1 if any sensor failed to initialise, has an I2C error or no update for 200 ms (08 ≠ 0 or 07 ≠ 255)

Shared registers

Id Name Meaning
F0 device ID low byte = factory I2C address (unchanged after re-addressing), high byte = hw[0] (channels / sensors / variant)
F1 protocol version 2 for this release (same number reported in SPIKE mode 8 v[1])
F2 firmware version major×100 + minor (e.g. 102 = 1.2)
F3 status flags b0 = ready; other bits per product (see flags)
F4 update counter +1 every 10 ms snapshot, wraps 0~255 (2.56 s per cycle, not usable for reboot detection; frozen = module busy)
F5 uptime seconds seconds since boot, saturates at 32767 (~9 h); a drop means the module rebooted
F8 change I2C address [F8][new 0x080x77][new], saved to EEPROM, takes effect after reboot; [F8][0x00][0xFF] = back to factory address
EF reboot [EF][0xA5]; mBlock sends ACK first, then reboots

Single-file helper functions

No library to install. Paste it in two parts: part ① (2 lines) at the top of the sketch, part ② at the bottom. #include <Wire.h> and MBC_NONE must appear before setup() uses them; pasting everything at the bottom will not compile.

① At the top of your sketch (before setup())

#include <Wire.h>

const int16_t MBC_NONE = -32768;   // no answer / undefined id

② At the bottom of your sketch (after loop())

// read k consecutive values (k ≤ 16) starting at id, int16 little-endian
bool mbcReadMany(uint8_t addr, uint8_t id, uint8_t k, int16_t *out) {
  Wire.beginTransmission(addr);
  Wire.write(id);
  if (Wire.endTransmission() != 0) return false;
  delay(2);                                        // always wait 2 ms (ESP32 products cannot clock-stretch)
  if (Wire.requestFrom(addr, (uint8_t)(k * 2)) != (uint8_t)(k * 2)) return false;
  for (uint8_t i = 0; i < k; i++) {
    uint8_t lo = Wire.read(), hi = Wire.read();
    out[i] = (int16_t)(lo | (hi << 8));
  }
  return true;
}

// read one value; MBC_NONE when nothing answers
int16_t mbcRead(uint8_t addr, uint8_t id) {
  int16_t v;
  return mbcReadMany(addr, id, 1, &v) ? v : MBC_NONE;
}

// write a command (0~3 parameters)
// with 2 parameters whose first is 0, write (uint8_t)0, e.g. mbcWrite(ADDR, 0xF8, (uint8_t)0x00, 0xFF); otherwise it clashes with the array form below and will not compile
void mbcWrite(uint8_t addr, uint8_t id, const uint8_t *args, uint8_t n) {
  Wire.beginTransmission(addr);
  Wire.write(id);
  for (uint8_t i = 0; i < n; i++) Wire.write(args[i]);
  Wire.endTransmission();
}
void mbcWrite(uint8_t addr, uint8_t id) { mbcWrite(addr, id, nullptr, 0); }
void mbcWrite(uint8_t addr, uint8_t id, uint8_t a) { mbcWrite(addr, id, &a, 1); }
void mbcWrite(uint8_t addr, uint8_t id, uint8_t a, uint8_t b) { uint8_t p[2] = {a, b}; mbcWrite(addr, id, p, 2); }
void mbcWrite(uint8_t addr, uint8_t id, uint8_t a, uint8_t b, uint8_t c) { uint8_t p[3] = {a, b, c}; mbcWrite(addr, id, p, 3); }

// wait until ready (F3 bit0 = 1), at most ms milliseconds
bool mbcWaitReady(uint8_t addr, unsigned long ms) {
  unsigned long t0 = millis();
  while (millis() - t0 < ms) {
    int16_t st = mbcRead(addr, 0xF3);
    if (st != MBC_NONE && (st & 1)) return true;
    delay(20);
  }
  return false;
}

Example: print the whole value table

// helper part ①
#include <Wire.h>

const int16_t MBC_NONE = -32768;   // no answer / undefined id

const uint8_t ADDR = 0x29;

void setup() {
  Serial.begin(115200);
  Wire.begin();
#if defined(WIRE_HAS_TIMEOUT)
  Wire.setWireTimeout(25000, true);   // AVR: never hang when the module is silent
#endif
  if (!mbcWaitReady(ADDR, 2000)) Serial.println("module not found: check wiring and address");
  Serial.print("ID 0x"); Serial.println(mbcRead(ADDR, 0xF0), HEX);   // low byte address, high byte hw[0]
  Serial.print("FW ");    Serial.println(mbcRead(ADDR, 0xF2));
}

void loop() {
  Serial.print("nearCm: "); Serial.println(mbcRead(ADDR, 0x01));   // nearest distance (cm)
  Serial.print("nearDir: "); Serial.println(mbcRead(ADDR, 0x02));   // nearest direction
  Serial.print("farCm: "); Serial.println(mbcRead(ADDR, 0x03));   // farthest distance (cm)
  Serial.print("farDir: "); Serial.println(mbcRead(ADDR, 0x04));   // farthest direction
  Serial.print("frontCm: "); Serial.println(mbcRead(ADDR, 0x05));   // front distance (cm)
  Serial.print("nearMm: "); Serial.println(mbcRead(ADDR, 0x06));   // nearest distance (mm)
  Serial.print("initOk: "); Serial.println(mbcRead(ADDR, 0x07));   // init bitmap
  Serial.print("fault: "); Serial.println(mbcRead(ADDR, 0x08));   // fault bitmap
  Serial.print("frontMm: "); Serial.println(mbcRead(ADDR, 0x09));   // front distance (mm)
  { int16_t a[8]; mbcReadMany(ADDR, 0x10, 8, a); Serial.print("distCm: "); for (uint8_t i = 0; i < 8; i++) { Serial.print(a[i]); Serial.print(' '); } Serial.println(); }
  { int16_t a[8]; mbcReadMany(ADDR, 0x18, 8, a); Serial.print("distMm: "); for (uint8_t i = 0; i < 8; i++) { Serial.print(a[i]); Serial.print(' '); } Serial.println(); }
  Serial.println();
  delay(200);
}

// paste part ② of the helper functions below

Sources and assumptions

Generated by _build/gen-reference.mjs from _build/protocol/tof8.json (single source of truth, transcribed from the 2026-10-21 protocol decisions).

  • 正前方 mm(EV3 模式 3、SPIKE 模式 6 v[1])在定案 §5 沒有獨立 I2C 編號 → 掛在 05 正前方 cm 底下,用 scale:mm 標示,不自己加編號。
  • 0x22(使用者 2026-10-21 最終定案):只用一個位址,R~K 由左到右 7 個 cm:第 1、2、3 顆、min(第 4, 第 5)、第 6、7、8 顆;11、12 不放資料;最近距離/方向由主機自己比較。此排法只用在 0x22,EV3/SPIKE不動。
  • SPIKE:0/5/7/9 鎖住,1、2、3、4、8 維持不動;模式 6 改 [最近 mm, 正前方 mm, 最近方向 −8+8](原本 = 第 27 顆兩兩合併);模式 8 v[0] 改 ID|hw[0]<<8。
  • 最空曠方向 04 範圍 −7~+7(maxDirection 永遠不會是 0/16,盤點 INO:1086-1088);最近方向 02 ±8 = 沒目標、沿用上次偏向側,開機預設 −8。
  • 沒有寫入命令;F8 換位址/EF 重開機用共用的。
  • 主對話 2026-10-21:補 09 正前方 mm(定案 §5 只有 05 正前方 cm,但 EV3 模式 3、SPIKE 模式 6 都送 mm,I2C/mBlock 應有同一個值)。
  • Pin table uses each board's common defaults (ESP32: change with Wire.begin(sda, scl)).
Email for a quoteBuying Guide