Arduino代碼同影片,用Sharp紅外線距離模組配TM1637顯示屏
喺呢個教學入面,我哋會探討點樣將Sharp紅外線距離模組同TM1637顯示器一齊用,嚟量度同視覺化顯示距離。呢個設定可以準確量度距離,並即時顯示喺TM1637模組上面。完成呢個指南之後,你就會有一個可以量度距離並顯示喺顯示器上面嘅可行項目。

對於呢個項目,你需要了解接線同埋驅動各組件之間互動嘅代碼。Sharp IR距離感應器會將量度到嘅距離傳俾Arduino,而Arduino就會更新TM1637顯示器。呢個教學會詳細講解接線指示,並提供對令一切順利運作嘅代碼嘅見解(喺影片00:00)。
硬件解說
呢個項目嘅主要組件係Sharp IR距離模組同TM1637顯示器。Sharp IR模組利用紅外線光嚟量度到物件嘅距離,透過計算發出嘅光反射返嚟所需嘅時間。佢提供一個模擬輸出,會根據量度到嘅距離而變化。
TM1637顯示器係一個四位7段顯示器,透過兩個引腳控制:一個時鐘引腳同一個數據輸入/輸出引腳。呢個顯示器可以輕鬆將Sharp IR模組嘅距離讀數視覺化,令佢對各種應用都好易用同實用。
數據表詳情
| 製造商 | Sharp |
|---|---|
| 零件編號 | GP2Y0A41SK0F |
| 邏輯/IO電壓 | 5 V |
| 供電電壓 | 4.5–15 V |
| 輸出電流(每通道) | 10 mA |
| 峰值電流(每通道) | 60 mA |
| PWM頻率指引 | 不適用 |
| 輸入邏輯閾值 | 0.8 V(低),2.0 V(高) |
| 電壓降 / RDS(on) / 飽和 | 不適用 |
| 熱限制 | –10 至 60 °C |
| 封裝 | 塑膠外殼 |
| 備註 / 變體 | 距離量度範圍:10–80 cm |
- 確保提供適當嘅電源(4.5–15 V),以免損壞感應器。
- 喺電源引腳附近使用去耦電容以保持穩定。
- 將感應器遠離直射陽光,以防止干擾。
- 校準感應器以獲得準確嘅距離讀數。
- 正確接線TM1637顯示器,以避免通訊錯誤。
- 如果喺較高電流下運作,請考慮加散熱片。
接線指示
要將Sharp IR距離模組同TM1637顯示器接線到Arduino,請跟隨以下步驟:
將Sharp IR模組嘅紅線連接到Arduino上嘅5V引腳。然後,將黑線連接到Arduino上嘅接地(GND)引腳。黃線係輸出,應該連接到Arduino上嘅模擬引腳A0。對於TM1637顯示器,將CLK引腳連接到數碼引腳2,將DIO引腳連接到Arduino嘅數碼引腳3。呢個設定令Arduino可以讀取Sharp IR模組嘅距離,並將數據發送到TM1637顯示器。
確保所有連接都穩固,以避免間歇性問題。如果你用跳線,請確保佢哋正確連接,避免鬆動。
代碼示例同講解
以下摘錄顯示顯示器同感應器點樣設定:
#define CLK 2 // 將CLK連接到引腳2
#define DIO 3 // 將DIO連接到引腳3
SharpIR SharpIR(A0, model); // 初始化Sharp IR感應器
喺呢個部分,我哋定義TM1637顯示器嘅引腳,並透過指定模擬引腳A0嚟初始化Sharp IR感應器。呢個對Arduino嚟講好重要,等佢知道從邊度讀取IR感應器嘅數據。
循環函數會獲取距離量度值並顯示出嚟:
int dis = SharpIR.distance(); // 獲取距離
display.showNumberDec(dis, false, 3, 1); // 顯示距離
喺呢度,距離會儲存喺變數dis入面,而TM1637顯示器會更新嚟顯示呢個數值。showNumberDec入面嘅參數控制數字嘅顯示方式,確保格式正確。
示範 / 預期效果
當你完成所有接線並上傳代碼之後,你應該會見到距離顯示喺TM1637上面。當你將物件移近或移遠感應器時,顯示器會即時更新。不過,要注意喺較遠距離時可能會有唔準確嘅情況,正如影片入面提到(喺影片12:00)。
常見嘅陷阱包括電源連接嘅極性反轉,以及感應器嘅浮動輸入,呢啲都可能導致讀數唔穩定。喺依賴佢做關鍵量度之前,請確保徹底測試你嘅設定。
影片時間戳
- 00:00 - 項目介紹
- 01:30 - 接線指示
- 03:00 - 代碼講解
- 05:30 - 距離量度示範
- 08:00 - 常見陷阱同疑難排解
/*
* Sharp IR (infrared) distance measurement module with TM1637 Display for Arduino
* Measures the distance in cm.
* Original library: https://github.com/guillaume-rico/SharpIR
* Original code for TM1637 from https://github.com/avishorp/TM1637
* Watch video instructions for this code: https://youtu.be/dnjY9n927HI
* Written by Ahmad Shamshiri on Feb 03, 2018 at 12:32
* in Ajax, Ontario, Canada. www.robojax.com
*
* Get this code and other Arduino codes from Robojax.com
Learn Arduino step by step in a structured course with all material, wiring diagrams, and libraries
all in one place.
If you found this tutorial helpful, please support me so I can continue creating
content like this.
or make a donation using PayPal http://robojax.com/L/?id=64
* * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.*
* This code has been downloaded from Robojax.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <Arduino.h>
#include <TM1637Display.h>
// Display Module connection pins (Digital Pins)
#define CLK 2 // connect CLK to pin 2
#define DIO 3 // connect DIO to pin 3
// The amount of time (in milliseconds) between display
#define TEST_DELAY 100
uint8_t blank[] = { 0x0, 0x0, 0x0, 0x0 };// blank screen
TM1637Display display(CLK, DIO);
///////////////////***** start of Sharp IR
#include <SharpIR.h>
#define IR A0 // define Sharp IR signal pin
#define model 430 // the model of the IR module
// Sharp IR code for Robojax.com
// ir: the pin where your sensor is attached
// model: an int that determines your sensor:
/*
* GP2Y0A02YK0F --> "20150"
GP2Y0A21YK --> "1080"
GP2Y0A710K0F --> "100500"
GP2YA41SK0F --> "430"
*/
SharpIR SharpIR(IR, model);
/////////////////////**** end of Sharp IR
void setup()
{
// Sharp IR with TM1637 Display code for Robojax.com
display.setBrightness(0x0f);// set the brightness at max
}
void loop()
{
// Sharp IR with TM1637 Display code for Robojax.com
display.setSegments(blank);// clear the screen
int dis=SharpIR.distance(); // this returns the distance to the object you're measuring
display.showNumberDec(dis, false, 3, 1);// display the distance
delay(TEST_DELAY);//delay between display
// Sharp IR with TM1637 Display code for Robojax.com
}
你可能需要嘅嘢
-
亞馬遜
資源與參考資料
-
外部Sharp IR GP2Y0A51SK0F datasheet (PDF)global.sharp
文件📁
Arduino 函式庫 (zip)
-
TM1637 Arduino Library
TM1637_library.zip1.36 MB -
Arduino library for sharp IR distance sensor
robojax-sharp_IR-master.zip
数据表 (pdf)
-
Sharp IR GP2Y0A21YK Infrared distance sensor datasheet
robojax_sharp_IR_GP2Y0A21YK_datasheet.pdf0.36 MB
Fritzing 文件
-
TM1637 Seven Segment module
TM1637.fzpz0.01 MB -
sharp ir sensor GP2Y0A02YK0F
sharp ir sensor GP2Y0A02YK0F .fzz
用户手册
-
TM1637顯示器手冊
robojax-TM1637_display_manual.pdf0.31 MB
其他文件
-
Sharp GP2Y0A41SK0F Infrared distance sensor datasheet
robojax_sharp_IR_GP2Y0A41SK0F_datasheet.pdf0.84 MB -
Sharp GP2Y0A710K0F Infrared distance sensor datashee
robojax_sharp_IR_GP2Y0A710K0F_datasheet.pdf0.33 MB