> For the complete documentation index, see [llms.txt](https://s761111.gitbook.io/raspi-sensor/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://s761111.gitbook.io/raspi-sensor/tm1638-qi-led-shi-qi.md).

# TM1638七節LED顯示器

要把樹莓派感測的資料值顯示出來，便宜省錢的方案就是使用七節LED顯示器或是矩陣LED顯示器，這次先介紹TM1638七節LED顯示器，它有八個七節LED、八個LED以及八個按鈕，是很實用的零組件。

要使用它首先就要安裝它的Python Package rpi-TM1638，我fork原作者的程式並修改了一些小BUG及改用Python3的類別寫法，網站：<https://github.com/sunnyrainwss/rpi-TM1638>

安裝方式先遠端登入到樹莓派（或是使用realvnc直接使用遠端桌面），並使用底下的指令

```bash
sudo pip3 install git+https://github.com/sunnyrainwss/rpi-TM1638.git
```

{% embed url="<https://youtu.be/JeTXuu7b9tU>" %}

```python
from time import sleep
from rpi_TM1638 import TMBoards

# my GPIO settings (connected on GPIO19 and GPIO13 for DataIO and Clock; and on GPIO6 for the STB)
DIO = 19
CLK = 13
STB = 6,

# instanciante my TMboards
TM = TMBoards(DIO, CLK, STB, 0)

TM.clearDisplay()

# 逐一顯示上方八個LED燈
for i in range(8):
    TM.leds[i] = True  # turn on led 0 (1st led of the 1st board)
    sleep(0.5)
    TM.leds[i] = False

TM.segments[0] = '12.3'  # 從第0個開始顯示資料值
TM.segments[4] = '56.78'  # 從第4個開始顯示資料值

# 第3個7節LED逐一顯示
for i in range(8):
    TM.segments[3, i] = True
    sleep(0.5)
    TM.segments[3, i] = False

TM.clearDisplay()
TM.segments[0] = '00000000'

# 依照按鈕按下位址顯示1，沒按下顯示0
while True:
    a = TM.getData(0)
    print(a)
    seg = 0
    disp = ''
    for x in a:
        if x == 1 or x == 16:
            if x == 16:
                seg = 4
            disp += '1'
        else:
            disp += '0'
    TM.segments[seg] = disp
    sleep(0.5)
    TM.segments[seg] = '0000'
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://s761111.gitbook.io/raspi-sensor/tm1638-qi-led-shi-qi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
