用激光VL53L0X 6针模块同LCD1602-I2C为Arduino量度距离
喺呢個教學入面,我哋會學吓點樣用VL53L0X激光感測器量度距離,並且將結果顯示喺有I2C介面嘅LCD1602上面。呢個項目可以幫你得到以毫米或者厘米計嘅精確距離讀數,令佢成為任何Arduino項目嘅一個多功能配件。完成呢個教學之後,你就會有一個完全正常運作嘅距離量度系統,而且好容易設定同使用。

為咗達到呢個目的,我哋會用VL53L0X激光距離感測器,佢透過I2C同Arduino溝通。LCD1602顯示屏會清楚顯示量度到嘅距離。如果想清楚了解編程過程,請參考影片(影片00:00位置)。
硬件講解
呢個製作嘅主要零件係VL53L0X激光距離感測器同LCD1602顯示屏。VL53L0X用激光嚟準確量度距離,適合各種應用,包括機械人同自動化。佢用I2C通訊運作,可以好容易同Arduino整合。
LCD1602顯示屏係一個支援I2C嘅字元顯示屏,可以喺兩行上面顯示最多16個字元。佢簡化咗顯示數據嘅過程,因為佢比傳統LCD用少啲針腳。呢啲零件加埋一齊,就組成一個強大又易用嘅距離量度系統。
數據表細節
| 製造商 | STMicroelectronics |
|---|---|
| 零件編號 | VL53L0X |
| 邏輯/IO電壓 | 1.8 V 至 2.8 V |
| 供電電壓 | 2.6 V 至 3.5 V |
| 輸出電流(每通道) | … |
| 峰值電流(每通道) | … |
| PWM頻率指引 | … |
| 輸入邏輯閾值 | … |
| 電壓降 / RDS(on) / 飽和 | … |
| 熱限制 | … |
| 封裝 | … |
| 備註 / 變體 | … |
- 確保供電電壓正確(2.6 V 至 3.5 V)。
- 如有需要,喺SDA同SCL線上加拉高電阻。
- 將感測器遠離反光表面,避免錯誤讀數。
- 確認I2C地址(VL53L0X預設係0x29)。
- 如果喺高溫環境使用,要考慮散熱問題。
接線指示

要接駁VL53L0X感測器同LCD1602顯示屏,首先連接電源同接地。將VL53L0X嘅VCC針腳連接到Arduino嘅5V針腳,然後將GND針腳連接到Arduino嘅接地。對於LCD1602,將佢嘅VCC連接到同一個5V,GND都連接到接地。
跟住,連接I2C通訊線。VL53L0X嘅SDA針腳應該連接到Arduino嘅A4針腳,而SCL針腳就接到A5。XSHUT針腳可以連接到Arduino嘅D12,但都可以唔接。對於LCD1602,確保SDA同SCL都連接到同VL53L0X感測器一樣嘅針腳。
程式碼示例同講解
VL53L0X sensor;
#include
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
Serial.begin(9600);
Wire.begin();
sensor.init();
sensor.setTimeout(500);
sensor.startContinuous();
lcd.begin();
lcd.backlight();
}
喺setup函數入面,我哋初始化串列通訊同I2C匯流排。sensor.init()初始化VL53L0X感測器,而lcd.begin()就初始化LCD顯示屏。顯示屏嘅背光會開着,方便睇清楚。
void loop() {
int distance = sensor.readRangeContinuousMillimeters();
Serial.print("Distance: ");
lcd.clear();
lcd.print("Robojax VL53L0X");
lcd.setCursor(0,1);
lcd.print("Dist.: ");
lcd.setCursor(7,1);
lcd.print(distance);
Serial.println();
delay(100);
}
喺loop入面,我哋用sensor.readRangeContinuousMillimeters()持續讀取距離,然後將佢打印到串列監視器。LCD會更新顯示距離讀數,提供即時反饋。100毫秒嘅延遲確保讀數唔會更新得太頻密,方便閱讀。
示範 / 預期效果
開機之後,系統會持續量度距離,並且將結果顯示喺LCD上面。你可以預期當你將物件移近或者移遠感測器嘅時候,距離數值會改變。如果感測器有任何問題,例如超時錯誤,適當嘅訊息會打印到串列監視器(影片03:15位置)。
/*
This is Arduino code to measure distance with VL53L0X and display it on LCD1602 with I2C module.
Distance is displayed in mm (millimeter) or cm (centimeter).
Original Laser Sensor source: https://github.com/adafruit/Adafruit_VL53L0X
Modified by Ahmad Shamshiri for RoboJax.com
Date modified: Jun 28, 2018 at 19:06 in Ajax, Ontario, Canada
Watch the video instruction for this code: https://youtu.be/t14ly7Y09oI
You can get this code from RoboJax.com.
Pin connection
VL53L0X Pin Arduino Pin
VCC 5V
GND GND
SDA A4 or SDA if available
SCL A5 or SCL if available
GPIO1 leave it unconnected
XSHUT D12 (digital 12 or pin 12)
LCD1602 Pin
*/
#include <Wire.h>
#include <VL53L0X.h>
VL53L0X sensor;
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int type = 1;// 1=mm , 2= cm
String unit;// variable for unit, mm or cm
void setup()
{
Serial.begin(9600);
Wire.begin();
sensor.init();
sensor.setTimeout(500);
// Start continuous back-to-back mode (take readings as
// fast as possible). To use continuous timed mode
// instead, provide a desired inter-measurement period in
// ms (e.g. sensor.startContinuous(100)).
sensor.startContinuous();
// initialize the LCD,
lcd.begin();
// Turn on the blacklight and print a message.
lcd.backlight();
}
void loop()
{
int distance =sensor.readRangeContinuousMillimeters();
//int distance =sensor.startContinuous(100);
//distance = distance;
Serial.print("Distance: ");
//start of loop Robojax code for LCD with I2C
lcd.clear();
lcd.print("Robojax VL53L0X");
lcd.setCursor (0,1); // go to start of 2nd line
lcd.print("Dist.: ");
lcd.setCursor (7,1); // go to start of 2nd line
if(type ==2){
float distanceCM = (float) (distance/10.0);
unit ="cm";
lcd.print(distanceCM);
Serial.print(distanceCM);
Serial.print(unit);
}else{
unit ="mm";
lcd.print(distance);
Serial.print(distance);
Serial.print(unit);
}
lcd.print(unit);
if (sensor.timeoutOccurred()) { Serial.print(" TIMEOUT"); }
Serial.println();
delay(100);
}
你可能需要嘅嘢
-
亞馬遜Purchase VL53L0X from Amazonamzn.to
-
易贝Purchase VL53l0x from eBayebay.us
-
阿里巴巴速卖通Purchase 1 to 10 pcs of VL53L0X from AliExpresss.click.aliexpress.com
-
邦购Purchase 1 to 10 pcs of VL53L0X from Banggoodbanggood.com
資源與參考資料
-
外部Adafruit download pagelearn.adafruit.com
-
外部Purchase 1 to 10 pcs of VL53L0X from AliExpresss.click.aliexpress.com
-
外部Purchase 1 to 10 pcs of VL53L0X from Banggoodbanggood.com
-
外部Purchase VL53l0x from eBayebay.us
-
外部VL53L0X datasheet (PDF)st.com
文件📁
Fritzing 文件
-
VL53L0X_Distance_sensor_Squares
VL53L0X_Distance_sensor_Squares.fzpz0.02 MB
其他文件
-
Adafruit VL53L0X 函式庫來自 Robojax
robojax-VL52L0X_Adafruit_VL53L0X-master.zip0.08 MB -
VL53L0X_Distance_sensor_purple
VL53L0X_Distance_sensor_purple.fzpz0.01 MB