加入星計(jì)劃,您可以享受以下權(quán)益:

  • 創(chuàng)作內(nèi)容快速變現(xiàn)
  • 行業(yè)影響力擴(kuò)散
  • 作品版權(quán)保護(hù)
  • 300W+ 專業(yè)用戶
  • 1.5W+ 優(yōu)質(zhì)創(chuàng)作者
  • 5000+ 長(zhǎng)期合作伙伴
立即加入
  • 正文
    • 驅(qū)動(dòng)單個(gè)VL53L0
    • 驅(qū)動(dòng)兩個(gè)VL53L0
  • 相關(guān)推薦
  • 電子產(chǎn)業(yè)圖譜
申請(qǐng)入駐 產(chǎn)業(yè)圖譜

ESP32驅(qū)動(dòng)GY-530 (VL53L0X) 全代碼

12/20 07:21
162
閱讀需 23 分鐘
加入交流群
掃碼加入
獲取工程師必備禮包
參與熱點(diǎn)資訊討論

GY-530是一個(gè)微小的基于940nm紅外進(jìn)行ToF測(cè)距的模塊,內(nèi)置VL53L10芯片,測(cè)量絕對(duì)范圍為2米,可用來(lái)用戶檢測(cè)、機(jī)器人避障、手勢(shì)識(shí)別、激光輔助自動(dòng)對(duì)焦等,工作電壓2.6-3.5V。網(wǎng)上用arduino來(lái)驅(qū)動(dòng)的代碼比較少,所以寫個(gè)博客記錄一下。

基于Arduino IDE,所有支持arduino編譯的MCU都可以用這個(gè)代碼

驅(qū)動(dòng)單個(gè)VL53L0

模塊的SDA、SCL對(duì)應(yīng)接到MCU的I2C引腳上就好了。

#include <Wire.h>

#define VL53L0X_REG_IDENTIFICATION_MODEL_ID         0xc0
#define VL53L0X_REG_IDENTIFICATION_REVISION_ID      0xc2
#define VL53L0X_REG_PRE_RANGE_CONFIG_VCSEL_PERIOD   0x50
#define VL53L0X_REG_FINAL_RANGE_CONFIG_VCSEL_PERIOD 0x70
#define VL53L0X_REG_SYSRANGE_START                  0x00
#define VL53L0X_REG_RESULT_INTERRUPT_STATUS         0x13
#define VL53L0X_REG_RESULT_RANGE_STATUS             0x14

//VL53L0的默認(rèn)I2C地址
#define address 0x29

byte gbuf[16];

void setup() {
  // put your setup code here, to run once:
  Wire.begin(25,26);        // 引腳25是SDA,26是SCL,可以自己設(shè)置其他引腳
  Serial.begin(115200);  // start serial for output
  Serial.println("VLX53LOX test started.");
}

void loop() {
//每50ms測(cè)一次距離值
  test();
  delay(50);
}

void test() {
  write_byte_data_at(VL53L0X_REG_SYSRANGE_START, 0x01);
  byte val = 0;
  int cnt = 0;
  while (cnt < 100) { // 1 second waiting time max
    delay(10);
    val = read_byte_data_at(VL53L0X_REG_RESULT_RANGE_STATUS);
    if (val & 0x01) break;
    cnt++;
  }
  read_block_data_at(0x14, 12);
  uint16_t dist = makeuint16(gbuf[11], gbuf[10]);
  Serial.print("distance ");
  Serial.println(dist);
}

uint16_t bswap(byte b[]) {
  // Big Endian unsigned short to little endian unsigned short
  uint16_t val = ((b[0] << 8) & b[1]);
  return val;
}

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) {
  // write data word at address and register
  Wire.beginTransmission(address);
  Wire.write(reg);
  Wire.write(data);
  Wire.endTransmission();
}

void write_word_data_at(byte reg, uint16_t data) {
  // write data word at address and register
  byte b0 = (data &0xFF);
  byte b1 = ((data >> 8) && 0xFF);
    
  Wire.beginTransmission(address);
  Wire.write(reg);
  Wire.write(b0);
  Wire.write(b1);
  Wire.endTransmission();
}

byte read_byte_data() {
  Wire.requestFrom(address, 1);
  while (Wire.available() < 1) delay(1);
  byte b = Wire.read();
  return b;
}

byte read_byte_data_at(byte reg) {
  //write_byte_data((byte)0x00);
  write_byte_data(reg);
  Wire.requestFrom(address, 1);
  while (Wire.available() < 1) delay(1);
  byte b = Wire.read();
  return b;
}

uint16_t read_word_data_at(byte reg) {
  write_byte_data(reg);
  Wire.requestFrom(address, 2);
  while (Wire.available() < 2) delay(1);
  gbuf[0] = Wire.read();
  gbuf[1] = Wire.read();
  return bswap(gbuf); 
}

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();
  }
}


uint16_t VL53L0X_decode_vcsel_period(short vcsel_period_reg) {
  // Converts the encoded VCSEL period register value into the real
  // period in PLL clocks
  uint16_t vcsel_period_pclks = (vcsel_period_reg + 1) << 1;
  return vcsel_period_pclks;
}

驅(qū)動(dòng)兩個(gè)VL53L0

如果買到相同的兩個(gè)模塊,它們的I2C地址默認(rèn)都是0x29,這時(shí)無(wú)法在同一條I2C總線通信。有一個(gè)方法是軟件修改I2C地址,但是每次斷點(diǎn)重啟,模塊的地址又變?yōu)?x29,而且每次改都要先斷開其中一個(gè),才能準(zhǔn)確修改另一個(gè)的地址,不然兩個(gè)地址會(huì)一起被改。所以最方便的方法,就是用兩條I2C總線,一般MCU都會(huì)有2個(gè)或以上的I2C資源。

//the original code by Ted Meyers
//posted here: https://groups.google.com/d/msg/diyrovers/lc7NUZYuJOg/ICPrYNJGBgAJ

#include <Wire.h>

#define VL53L0X_REG_IDENTIFICATION_MODEL_ID         0xc0
#define VL53L0X_REG_IDENTIFICATION_REVISION_ID      0xc2
#define VL53L0X_REG_PRE_RANGE_CONFIG_VCSEL_PERIOD   0x50
#define VL53L0X_REG_FINAL_RANGE_CONFIG_VCSEL_PERIOD 0x70
#define VL53L0X_REG_SYSRANGE_START                  0x00
#define VL53L0X_REG_RESULT_INTERRUPT_STATUS         0x13
#define VL53L0X_REG_RESULT_RANGE_STATUS             0x14
#define address 0x29

byte gbuf[16];
byte gbuf1[16];

void setup() {

  Wire.begin(25,26);        // join i2c bus (address optional for master)
  Wire1.begin(21,22);
  Serial.begin(115200);  // start serial for output
  Serial.println("VLX53LOX test started.");
}

void loop() {

  test();
  delay(100);
  
}

void test() {

  write_byte_data_at(VL53L0X_REG_SYSRANGE_START, 0x01);
  write_byte_data_at1(VL53L0X_REG_SYSRANGE_START, 0x01);

  byte val = 0;
  byte val1 = 0;
  
  int cnt = 0;
  int cnt1 = 0;
  
  while (cnt < 100) { // 1 second waiting time max
    delay(10);
    val = read_byte_data_at(VL53L0X_REG_RESULT_RANGE_STATUS);
    if (val & 0x01) break;
    cnt++;
  }

   while (cnt1 < 100) { // 1 second waiting time max
    delay(10);
    val1 = read_byte_data_at1(VL53L0X_REG_RESULT_RANGE_STATUS);
    if (val1 & 0x01) break;
    cnt1++;
  }

  read_block_data_at(0x14, 12);
  uint16_t dist1 = makeuint16(gbuf[11], gbuf[10]);
  read_block_data_at1(0x14, 12);
  uint16_t dist2 = makeuint16(gbuf1[11], gbuf1[10]);


    Serial.print("distance1:");
    Serial.print(dist1);
    Serial.print(" distance2:");   
    Serial.println(dist2);

}

uint16_t bswap(byte b[]) {
  uint16_t val = ((b[0] << 8) & b[1]);
  return val;
}
uint16_t bswap1(byte b[]) {
  uint16_t val1 = ((b[0] << 8) & b[1]);
  return val1;
}

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_data1(byte data) {
  Wire1.beginTransmission(address);
  Wire1.write(data);
  Wire1.endTransmission();
}

void write_byte_data_at(byte reg, byte data) {
  // write data word at address and register
  Wire.beginTransmission(address);
  Wire.write(reg);
  Wire.write(data);
  Wire.endTransmission();
}
void write_byte_data_at1(byte reg, byte data) {
  // write data word at address and register
  Wire1.beginTransmission(address);
  Wire1.write(reg);
  Wire1.write(data);
  Wire1.endTransmission();
}

void write_word_data_at(byte reg, uint16_t data) {
  // write data word at address and register
  byte b0 = (data &0xFF);
  byte b1 = ((data >> 8) && 0xFF);
    
  Wire.beginTransmission(address);
  Wire.write(reg);
  Wire.write(b0);
  Wire.write(b1);
  Wire.endTransmission();
}
void write_word_data_at1(byte reg, uint16_t data) {
  // write data word at address and register
  byte b0 = (data &0xFF);
  byte b1 = ((data >> 8) && 0xFF);
    
  Wire1.beginTransmission(address);
  Wire1.write(reg);
  Wire1.write(b0);
  Wire1.write(b1);
  Wire1.endTransmission();
}

byte read_byte_data() {
  Wire.requestFrom(address, 1);
  while (Wire.available() < 1) delay(1);
  byte b = Wire.read();
  return b;
}
byte read_byte_data1() {
  Wire1.requestFrom(address, 1);
  while (Wire1.available() < 1) delay(1);
  byte b = Wire1.read();
  return b;
}

byte read_byte_data_at(byte reg) {
  //write_byte_data((byte)0x00);
  write_byte_data(reg);
  Wire.requestFrom(address, 1);
  while (Wire.available() < 1) delay(1);
  byte b = Wire.read();
  return b;
}
byte read_byte_data_at1(byte reg) {
  //write_byte_data((byte)0x00);
  write_byte_data1(reg);
  Wire1.requestFrom(address, 1);
  while (Wire1.available() < 1) delay(1);
  byte b = Wire1.read();
  return b;
}

uint16_t read_word_data_at(byte reg) {
  write_byte_data(reg);
  Wire.requestFrom(address, 2);
  while (Wire.available() < 2) delay(1);
  gbuf[0] = Wire.read();
  gbuf[1] = Wire.read();
  return bswap(gbuf); 
}
uint16_t read_word_data_at1(byte reg) {
  write_byte_data1(reg);
  Wire1.requestFrom(address, 2);
  while (Wire1.available() < 2) delay(1);
  gbuf1[0] = Wire1.read();
  gbuf1[1] = Wire1.read();
  return bswap1(gbuf1); 
}

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();
  }
}
void read_block_data_at1(byte reg, int sz) {
  int i = 0;
  write_byte_data1(reg);
  Wire1.requestFrom(address, sz);
  for (i=0; i<sz; i++) {
    while (Wire1.available() < 1) delay(1);
    gbuf1[i] = Wire1.read();
  }
}


uint16_t VL53L0X_decode_vcsel_period(short vcsel_period_reg) {
  uint16_t vcsel_period_pclks = (vcsel_period_reg + 1) << 1;
  return vcsel_period_pclks;
}

也可以用一個(gè)庫(kù) Adafruit_VL53L0X 來(lái)做。

相關(guān)推薦

電子產(chǎn)業(yè)圖譜