Model Train-related Notes Blog -- these are personal notes and musings on the subject of model train control, automation, electronics, or whatever I find interesting. I also have more posts in a blog dedicated to the maintenance of the Randall Museum Model Railroad.

2023-10-13 - SDB: Supporting two ToF Sensors

Category SDB

The Software Defined Blocks project uses an ESP32 with sensors to emulate block activations for a train model railroad.

The initial design calls to use two ToF sensors, one on each side of the piece of track to automate. That would allow the automation to precisely stop the train on either side.

The sensor I use is the Adafruit VL53L0X. It connects on the I2C bus at a specific 0x29 address. However, it is possible to change the address in software: one of the sensor’s pins named XSHUT is to “shutdown” the sensor. The procedure is to connect both sensors on the I2C bus, and have the two sensors’ XSHUT pins connected to different GPIO pins. This makes it possible to selectively turn them on at boot, changing their address one by one.

Example: https://github.com/adafruit/Adafruit_VL53L0X/blob/master/examples/vl53l0x_dual/vl53l0x_dual.ino

Pin out on the sensor: Vin, GND, SCL, SDA, GPIO, XSHUT

I2C Address: 0x29 → default I2C address at boot.

XSHUT is a “shutdown” (pulled high, pull down to shut down).

We’ll name our sensors ToF0 / ToF1 (instead of 1 & 2) because it’s more logical::

  • ToF0 ⇒ I2C 0x30, XSHUT0 on IO pin 18
  • ToF1 ⇒ I2C 0x31, XSHUT1 on IO pin 23

Init procedure (see setID() in the example):

  • Set both XSHUT pins to 0 (pull down = shutdown)         + Delay(10ms).
  • Set both XSHUT pins to 1 ⇒ this performs a reset         + Delay(10ms).
  • Set XSHUT1 to 0 (ToF0 awake, ToF1 shutdown)         + Delay(10ms).
  • lox0.begin(0x30)        ⇒ ToF0 becomes 0x30
  • Set XSHUT1 to 1 (ToF0 awake, ToF1 awake but still on 0x29)         + Delay(10ms).
  • lox1.begin(0x31)        ⇒ ToF1 becomes 0x31

What they call “lox” is really “l0x”, an Adafruit_VL53L0X() instance:

Adafruit_VL53L0X lox1 = Adafruit_VL53L0X();

Adafruit_VL53L0X lox2 = Adafruit_VL53L0X();

Support for this is implemented in the SDB ToF module: https://bitbucket.org/model-railroad/sdb/src/master/src/mod_tof.h


 Generated on 2024-12-03 by Rig4j 0.1-Exp-f2c0035