Intro: CircuitPython and TinyLiDAR: Simple Example
The MicroElectronicDesign tinyLiDAR is an ST VL53L0X based time-of-flight (ToF) ranging module with an i2c bus connection. The Adafruit microcontroller boards are easily connected to this sensor as they can speak the i2c protocol over their data pins.
The M0/M4 series have the advantage over other boards because they support a subset of Python which makes programming more accessible to a wider audience than C on an Arduino. This is a simple example using CircuitPython on a Gemma M0 board to read the distance values from a tinyLiDAR and indicate the distance by varying the brightness of the on-board RGB LED. The board must be the M0 version for python support.
Note: Adafruit also make various distance sensing boards including one based on the VL53L0X.
Step 1: CircuitPython Code
- Download lib/adafruit_dotstar.mpy and lib/adafruit_bus_device/i2c_device.mpy if you do not already have them. These are part of the optional library bundle, see first section of CircuitPython I2C for notes on how to install these. These files must go in the lib and lib/adafruit_bus_device directories on the Gemma M0.
- Download gemma-m0-tinylidar-simple.py, rename it to main.py and copy to the root directory of the Gemma M0.
The program writes output to the serial console as well as varying the brightness of the Gemma MO RGB LED in proportion to the distance read. The Gemma M0 led alternates between on and off for reads so will flash at approximately 5Hz and the default behaviour of the tinyLiDAR board is to flash its blue led per command at 10Hz.
The bus is driven at 100kHz which works well for this case. Like any bus, the communication speed communication speed is limited by the characteristics of the transmission line(s) so attention should be paid to length, capacitance and other factors.
Step 2: Connecting the TinyLiDAR to a Gemma M0
The picture shows an effective but inelegant temporary setup to connect the tinyLiDAR to a Gemma M0 board. The tinyLiDAR's “Grove” 4pin universal connector is being used via a Seeed Grove conversion cable, a spare header and crocodile clips to connect to the Gemma M0. Some separation between the pins on the header removes the risk of an inadvertent short. The connections are:
- Black: gnd to gnd
- Red: +V to 3Vo
- White: SDA to D0 (data)
- Yellow: SCL to D2 (clock)
The Adafruit boards have some pins which support the i2c protocol efficiently in hardware. In the case of the Gemma M0, these must be used, D0 for data and D2 for clock.
An i2c bus requires a pull-up resistor on the data and clock lines. In this case, the tinyLiDAR provides some 4.7K on-board resistors. If these have been removed from the circuit by cutting the lines then equivalents must be added to the circuit.
Step 3: Serial Output
The program writes output to the serial console showing the distance measured, here's an example screenshot of a terminal showing an object moving away from the sensor.