LoginSignup
1
1

More than 3 years have passed since last update.

【RaspberryPi】起動確認用LEDをpython、systemctlを使って制御

Posted at

概要

RaspberryPiの起動が完了するとLEDが点灯する機能を追加
※LEDは11ピン(GPIO17)に接続

実行環境

MCU:Raspberry Pi Zero W
OS:Raspberry Pi OS Buster

回路

回路図省略
電流制限抵抗は明るさを抑えるために2.2kΩを使用

実装

pythonプログラム

LEDの点灯制御をするためのpythonプログラム

$ sudo nano /home/pi/PowerLED.py

最終行にsleep()を入れるとpythonプログラムが終了してもLEDが点灯しっぱなしになる
綺麗な方法ではないが、バックグラウンドで動くプログラムを節約したいので暫定的にこれで実施

PowerLED.py
#!/usr/bin/python3

import RPi.GPIO as GPIO
import time

LED_PIN = 17

GPIO.setmode(GPIO.BCM)
GPIO.setup(LED_PIN, GPIO.OUT)

GPIO.output(LED_PIN, GPIO.HIGH)
time.sleep(3)

serviceファイル

systemctlで制御するためのserviceファイルを作成
パラメータは最低限にしているので、これより減らすと色々問題が起こるかも

$ sudo nano /lib/systemd/system/PowerLED.service

他サイトではshebangを使用・serviceファイルのExecStart行ではファイル名のみを記載する方法も紹介されていたが、上手くいかなかったのでserviceファイル上でpython3を指定(フルパスで指定しないとエラーになる)
その他のパラメータについては別記事に記載

PowerLED.service
[Unit]
Description = Power LED of Startup
After=multi-user.target

[Service]
ExecStart=/usr/bin/python3 /home/pi/App/PowerLED.py
Restart=on-failure
Type=simple

[Install]
WantedBy=multi-user.target

serviceの設定

動作確認

$ sudo systemctl daemon-reload
$ sudo systemctl start PowerLED
~LEDが点灯したらOK~

RaspberryPi起動時に自動起動するよう設定

$ sudo systemctl enable PowerLED
1
1
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
1
1