登出中,將自動重新整理頁面
登入/註冊
新電子科技雜誌 Micro-electronics
熱門關鍵字:
  • CPO
  • 低軌衛星
  • 機器人
  • 先進封裝
  • Wi-Fi 8
  • GaN
  • 新電子科技雜誌 Micro-electronics
  • 首頁
  • 追新聞
    • 微軟發起跨產業倡議 提升AI透明度

      2026 年 11 月 20 日
    • JEDEC揭露DDR6技術藍圖 記憶體頻寬提升逾50%

      2026 年 11 月 19 日
    • 科林研發竹南技術中心動土 台灣研發實驗室空間將擴增9倍

      2026 年 11 月 18 日
    • Quix Quantum/光程研創共同開發光量子運算關鍵技術

      2026 年 11 月 18 日
    • 建立車用小晶片生態系 imec/ASRA聯盟提出互通框架文件

      2026 年 11 月 13 日
  • 懂市場
    • Starlink規模優勢已成 傳統衛星營運商另闢蹊徑

      2026 年 11 月 30 日
    • HBM4/HBM4E 如何重新定義半導體技術?AI算力競賽正從晶片走向系統整合

      2026 年 11 月 27 日
    • Weebit Nano行銷及業務開發副總裁Eran Briman:ReRAM化解AI資料搬移瓶頸

      2026 年 11 月 17 日
    • 從晶片分銷到AI生態系整合,安富利以設計服務打造邊緣/實體AI優勢

      2026 年 11 月 16 日
    • AI加速伺服器解耦 PCIe高速化迎戰訊號完整性極限

      2026 年 11 月 13 日
  • 學技術
    • ADI攜手NVIDIA擴充cuVSLAM感測融合能力 突破環境限制提升機器人定位

      2026 年 11 月 16 日
    • 掌握PDU與RPP運作狀態 遠端監控優化配電管理

      2026 年 11 月 13 日
    • 善用波特圖分析響應速度與穩定裕度 掌握電源控制迴路特性

      2026 年 11 月 13 日
    • 高密度模組降低電弧與間距設計負擔 48V汽車電源因應高壓設計

      2026 年 11 月 12 日
    • microLED研發與非PBS LCoS並行 全彩AR走向量產雙軌開發

      2026 年 11 月 12 日
  • 查圖表
    • Omdia:生成式AI需求激增 2026年全球AI軟體營收將突破2,000億美元

      2026 年 11 月 12 日
    • 2026年第3季智慧手機SoC出貨量年減21% 手機品牌轉向獲利優先

      2026 年 11 月 05 日
    • TrendForce:2027年CSP資本支出將破1.5兆美元

      2026 年 10 月 19 日
    • Counterpoint:3Q’26全球智慧型手機出貨量衰退20%

      2026 年 10 月 15 日
    • 先進封裝需求持續升溫 全球Foundry 2.0市場2Q營收年增25%

      2026 年 10 月 12 日
  • 探商情
    • 意法推出整合式功率元件MasterGaN7 降低空載功耗

      2026 年 11 月 27 日
    • ROHM推12A降壓DC-DC轉換器 整合QuiCur技術縮減電路

      2026 年 11 月 27 日
    • Littelfuse推出IXD704閘極驅動器 提升電源設計可靠性

      2026 年 11 月 27 日
    • 偉世通合資廠攜英飛凌 開發GaN BDS單級車載充電器

      2026 年 11 月 27 日
    • 德儀航太級晶片 驅動新一代衛星發展

      2026 年 11 月 26 日
  • 研討會
  • 找廠商
  • 各期雜誌
  • 白皮書
  • 影音專區
首頁 » 技術頻道 » Python開發者必學:用Arduino Cloud控制Raspberry Pi GPIO

Python開發者必學:用Arduino Cloud控制Raspberry Pi GPIO

2025/03/25 · 編譯:江宗諭

作為一位Python的開發者,您可能迫不及待想遠端控制並監控您的Raspberry Pi GPIO(通用輸入輸出)。那麼,這篇文章正符合您的需要!

本文是我們(編按:在此指 Arduino 團隊)先前介紹 《視覺化 Raspberry Pi 數據:輕鬆用 Arduino Cloud  掌握物聯網裝置》 的延伸。這次,我們將深入探討,如何使用Python 配置 Raspberry Pi的GPIO,這是許多物聯網專案的基礎步驟,通常被視為IoT應用當中的「 Hello World 」。無論是控制繼電器或監測數位輸入,學會管理GPIO都是相當重要的起手式。

然而,物聯網應用通常需要遠端存取,並透過儀表板即時監控裝置數據、查看歷史變化,甚至遠端操控裝置。

那麼,就讓我們深入了解如何實現這一切吧!

實體設置:點亮LED

在本文中,我們將展示一個簡單但完整的範例。我們會學習如何使用Arduino Cloud主控板,以遠端控制Raspberry Pi的數位GPIO。簡單來說,我們將學習如何:

  • 開關連接到Raspberry Pi的LED
  • 偵測連接到Raspberry Pi的按鈕是否被按下
  • 監控一個整數變數的即時數值及歷史數據

首先,讓我們按照下方的電路圖,將Raspberry Pi 、 LED及按鈕連接起來。

這是一個非常簡單的設置。現在一切都準備好了,就讓我們開始吧!

在Arduino Cloud建立裝置和Thing  

要將Raspberry Pi的數據傳送到Arduino Cloud,只需按照以下簡單步驟操作:

  1. 若您還沒有Arduino Cloud帳號,請先註冊。
  2. 將你的裝置新增為手動裝置。

 

請注意:請記下您的裝置ID及密鑰,稍後我們將會用到。

  1. 建立您的Thing,並加入變數。

在本文的範例中,我們使用了以下三個變數:

  • test_value:這是一個整數變數,我們將用它來顯示Raspberry Pi應用程式定期產生的數值,並在Arduino Cloud主控板上呈現。
  • button:這是一個布林變數,當按鈕被按下時,我們會將此資訊傳送到雲端。
  • led:這是一個布林變數,我們可透過Arduino Cloud主控板來開啟或關閉LED。
  1. 建立Arduino Cloud主控板以視覺化數據
  • 建立一個開關元件(Switch Widget,名稱:LED)及一個LED元件(LED Widget,名稱:LED),並將它們連結到led變數。
  • 建立一個圖表元件(Chart Widget,名稱:Value evolution)及一個數值元件(Value Widget,名稱:Value),並將它們連結到test_value變數。
  • 建立一個按鈕元件(Push Button,名稱:Push Button)及一個狀態元件(Status Widget,名稱:Button),並將它們連結到 button 變數。

透過這個主控板,您將能:

  • 使用開關元件開啟或關閉LED
  • 透過LED 元件查看LED的狀態
  • 透過數值元件,即時查看 test_value 變數的數值。
  • 透過圖表元件,觀察 test_value 變數的變化趨勢。
  • 當按鈕被按下時,透過Push Button和Button元件顯示按鈕的狀態。

請注意:您可在我們的文件指南中找到更詳細的操作說明。

針對您的物聯網裝置,運用Python在Arduino Cloud內撰寫程式 

現在是時候開發您的Python應用程式了。’

(編按:以下為Python程式碼)

#! /usr/bin/python3


import random
import gpiod
from gpiod.line import Direction, Value, Bias
from arduino_iot_cloud import ArduinoCloudClient
from credentials import DEVICE_ID, SECRET_KEY


LED=14      # GPIO14, Pin 8
BUTTON=15   # GPIO15, Pin 10


# For Raspberry PI 5, the chip is gpiochip4. Check for other RPI flavours.
chip = gpiod.Chip('/dev/gpiochip4')
req=chip.request_lines(consumer="rpi-acloud-gpio-basic",
     config= {
        LED    : gpiod.LineSettings(direction=Direction.OUTPUT),
        BUTTON : gpiod.LineSettings(direction=Direction.INPUT, bias=Bias.PULL_UP),
     })


# This function is executed every 1.0 seconds (as defined in the registration) and
# returns a random integer value between 0 and 100
def read_button(client):
  button = req.get_value(BUTTON)
  if button == Value.INACTIVE:
     return False
  else:
     return True


# This function is executed every 10.0 seconds (as defined in the registration) and
# returns a random integer value between 0 and 100
def read_value(client):
  return random.randint(0, 100)


# This function is executed each time the "led" variable changes
def on_led_changed(client, value):
  if value:
      req.set_value(LED, Value.ACTIVE)
  else:
      req.set_value(LED, Value.INACTIVE)
  print("LED change! Status is: ", value)




if __name__ == "__main__":
  # Create Arduino Cloud connection
  client = ArduinoCloudClient(device_id=DEVICE_ID, username=DEVICE_ID, password=SECRET_KEY)


  # Register the Arduino Cloud variables with the callback functions
  client.register("test_value", on_read=read_value, interval=10.0)
  client.register("button", on_read=read_button, interval=1.0)
  client.register("led", value=None, on_write=on_led_changed)
   # Start the client
  client.start()

您可在Project Hub找到更多關於這項專案的資訊,所有程式碼及更多細節也可在GitHub 儲存庫中找到。

免費開始使用Arduino Cloud 

將您的Raspberry Pi連接到Arduino Cloud非常容易。只需要建立一個免費帳號,便可開始使用。帳號隨時可用且免費,您還可探索進階功能以提升功能性。

所以,若您想用 Python 簡化 Raspberry Pi 應用程式的數據視覺化,不妨試試看 Arduino Cloud,並發揮它在專案中的全部潛力。

同時,敬請期待我們的 Raspberry Pi GPIO 基本控制教學系列的第三及第四部分。若您想進一步探索 Arduino Cloud ,請點擊這裡,謝謝。

(感謝Arduino.cc官網開放授權,使我們得以翻譯這篇文章,特此致謝!若讀者想要進一步閱讀原文,請點擊:閱讀原文,謝謝。)註:主圖及配圖都翻攝自arduino.cc網站,特此致謝!

(本文轉載自vMaker台灣自造者,原文連結:https://vmaker.tw/archives/73582)

Related posts:

  1. 打怪升級!創客必修「飛行器+手把」自造課 成就天空飛翔夢
  2. Arduino正式加入高通生態系!大廠資源挹注 深化開源與實作精神
  3. 樹莓派主要更新:鍵盤型電腦改款、作業系統升級
  4. 樹莓派主要更新: 5吋觸控顯示器、1TB固態硬碟

標籤:Arduino Cloud、Raspberry Pi

相關文章

打怪升級!創客必修「飛行器+手把」自造課 成就天空飛翔夢

2025/12/19

Arduino正式加入高通生態系!大廠資源挹注 深化開源與實作精神

2025/11/19

樹莓派主要更新:鍵盤型電腦改款、作業系統升級

2025/10/22

樹莓派主要更新: 5吋觸控顯示器、1TB固態硬碟

2025/09/25
關於新電子| About Microelectronics| 廣告委刊| 聯絡我們| 隱私權政策
城邦文化事業股份有限公司版權所有、轉載必究.Copyright(c) 2004 - 2026 Cite Publishing Ltd.
本網站使用 cookie 來改善您瀏覽網站時的體驗。 其中,分類為必要的 cookie 存儲在您的瀏覽器中,因為它們對於網站的基本功能的運行至關重要。 我們還使用第三方 cookie 來幫助我們分析和了解您如何使用本網站。 只有在您同意的情況下,這些 cookie 才會存儲在您的瀏覽器中。 您還可以選擇退出這些 cookie。 但選擇其中一些 cookie 可能會影響您的瀏覽體驗。
Cookie SettingsAccept All
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT





登入會員

本站程式甫於2022.5.5更新,
所有舊會員必須先點擊:
忘記密碼 進行密碼確認後,才能正常登入。

上述動作目的在於確保帳號安全性,造成不便懇請見諒。如您已重設過密碼,請忽略此訊息。

登入中...

還不是會員?點選這邊立即註冊