Reads ToF chip GYVL53LOX distance information on BLYNK

I didn’t find anything about the ToF chip (GYVL53LOX) in the forum.Maybe someone should need it.

For ESP32 DEVKITV1 or other ESP32 boards.You only need to connect SDA, SCL, 5V, GND (purple square mark pin), or use any idle GPIO as 5V(blue square mark pin), set to high level (actual 3.3V), it can also drive 53LOX work.After the program runs, there will output a real-time distance value every second on the V0 virtual pin.

#define BLYNK_PRINT Serial
#include <Wire.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#define address 0x29
byte gbuf[16];
char auth[] = "your auth";
char ssid[] = "your ssid";
char pass[] = "your password";
BlynkTimer timer;

void sendSensor()
{
  test();
  uint16_t dist = makeuint16(gbuf[11], gbuf[10]);
  Blynk.virtualWrite(V0, dist);
}

void setup()
{
  Wire.begin();
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  timer.setInterval(1000L, sendSensor);
}

void loop()
{
  Blynk.run();
  timer.run();
}

void test() {
  write_byte_data_at(0x00, 0x01);
  read_block_data_at(0x14, 12);
  uint16_t dist = makeuint16(gbuf[11], gbuf[10]);
  Serial.println(dist);
}

uint16_t makeuint16(int lsb, int msb) {
  return ((msb & 0xFF) << 8) | (lsb & 0xFF);
}

void write_byte_data(byte data) {
  Wire.beginTransmission(address);
  Wire.write(data);
  Wire.endTransmission();
}

void write_byte_data_at(byte reg, byte data) {
  Wire.beginTransmission(address);
  Wire.write(reg);
  Wire.write(data);
  Wire.endTransmission();
}

void read_block_data_at(byte reg, int sz) {
  int i = 0;
  write_byte_data(reg);
  Wire.requestFrom(address, sz);
  for (i = 0; i < sz; i++) {
    while (Wire.available() < 1) delay(1);
    gbuf[i] = Wire.read();
  }
}
1 Like

Please edit your post to add triple backticks at the beginning and end of your code so that it displays correctly, and so that people can copy/paste it without certain characters becoming corrupted.
Triple backticks look like this:
```

It would also be helpful to some readers if you added information about what type of MCU this code is written for, how to connect the sensor to the MCU, and what widgets are required in the Blynk app.

Pete.

Ok, has already changed

1 Like