0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PC開発 + Raspberry Pi動作 using gpiozero

Last updated at Posted at 2025-06-30

概要

Raspberry PiでのGPIO制御はgpiozero(python)使うのが普通らしい(Raspberry Pi公式のUse GPIO from Python).そこでその準備.
またRaspberry Pi上でプログラム作るのはちょっと面倒(Raspberry Pi上にはUbuntu Server入れて軽量かつ制御に専念させているので).そこでデスクトップPC上でプログラム作成・Raspberry Pi上で動作,をするための話.

参考

PC開発 + Raspberry Pi動作

基本の流れはPC使ったRaspberry Pi開発手順通り.以下の説明はPC使ったRaspberry Pi開発手順の『前提』を使用.またbranchの活用をしている.

流れ

PCでの作業は『PC$』,Raspberry Piでの作業は『Pi$』と表記.

準備

  1. GitLab上でgpiozero用のブランチ作成
  2. PC$ git clone git@hogehoge.co.jp:user1/gpiozero-test.git
    • クローン先のディレクトリ: /home/user1/program/
    • コンピュータ名: home
  3. PC$ git switch -c develop-user1
    • 開発用ブランチの作成
  4. Pi$ git clone ssh://user1@home/home/user1/program/gpiozero-test
    • PCの開発用ブランチがcloneされる

gpiozeroのインストール

gpiozeroのパッケージはlgpioというパッケージも使用するみたいなので,こちらも合わせてインストール.
Raspberry Pi上でインストールしてPipfileをPC上にpushする.でpushする時にPC上の作業ブランチがdevelop-user1だとpushできなかった.そこでpushする時には一旦別のブランチ(mainブランチ)にしてpush,その後元に戻している.

  1. Pi$ cd gpiozero-test
    • Raspberry Pi上のプロジェクトディレクトリに移動
  2. Pi$ pipenv --python 3
  3. Pi$ pipenv install gpiozero lgpio
    • gpiozeroとlgpioのインストールでPipfile, Pipfile.lockができるのでPCにアップロード
  4. Pi$ git add .
  5. Pi$ git commit -a -m "なんかのメッセージ"
  6. PC$ git switch main
    • 一旦push先以外の別のブランチにしておく
  7. Pi$ git push
  8. PC$ git switch develop-user1

Raspberry Pi上での編集

gpiozeroのインストールと同じ流れ.

  1. Pi上で編集
  2. PC$ git switch main
    • 一旦push先以外の別のブランチにしておく
  3. Pi$ git push
  4. PC$ git switch develop-user1

PC上で編集(プログラム作成など)

PC上ではdevelop-user1ブランチで作業.

  1. PC上で編集
  2. PC$ git commit -a -m "なんかのメッセージ"
  3. Pi$ git pull

一段落ついてgithub, gitlabにアップロードする場合.最初はdevelop-user1ブランチから作業.

  1. git merge main
  2. git switch main
  3. git merge develop-user1
  4. git pushでgithub, gitlabにアップロード

ちょっとやってみる?

Raspberry PiでのLチカ実験の準備・回路作成の回路を作っているなら,PC上で編集(プログラム作成など)の『PC上で編集』のところで下記のプログラムを作成,実行してみると理解が進む.

led_test.py
from gpiozero import LED
from time import sleep

led = LED(21)

while True:
    led.on()
    sleep(1)
    led.off()
    sleep(1)
PC上での実行
led_test.pyの作成
PC$ git commit -a -m "led_test.pyつくったよ"
Raspberry Pi上での実行
Pi$ pipenv shell
Pi$ git pull origin develop-user1:main
Pi$ python3 led_test.py

led光るの確認.Ctrl-Cで止まる.
PC上でOKとする
PC$ git merge main
PC$ git switch main
PC$ git merge develop-user1
PC$ git push # githubやgitlabにpush
PC$ git switch develop-user1 # 次の編集に備える
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?