1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

RaspberryPi5のGPIOをPodmanから操作

Posted at

経緯

  • MicroShiftからRaspberryPiのGPIOを操作する前実験として、PodmanからGPIOを操作した
  • 今回はLチカでなく、圧電スピーカーを鳴らした

環境

  • Raspberry Pi5
  • AlmaLinux 9.4
  • Podman version 4.9.4-rhel

手順

0. 事前準備

1. Raspberry Pi5回路準備

  • 接続:GPIO12 (PWM0) - 圧電スピーカー - Ground

RPI5 pinout
ref) https://www.hackatronic.com/raspberry-pi-5-pinout-specifications-pricing-a-complete-guide/

2. Podmanインストール・AlmaLinux設定

  1. Podmanインストール
    $ sudo dnf install -y podman
    
  2. gpiochipへアクセスできるようにPermissionを設定
    $ sudo chmod 666 /dev/gpiochip*
    
    • これですべてのユーザーがgpiochipにアクセスできるようになる
  3. 上の設定を再起動しても変わらないようにする
    $ sudo nano /etc/udev/rules.d/99-gpio.rules
    
    • 上のファイルに下記を追記
    KERNEL=="gpiochip*", MODE="0666"
    
  4. 再起動
    $ sudo reboot
    

3. プロジェクト用意

  • 下記のようにファイルを用意
    project
    ├─Containerfile
    └─chime.py
    
  • Containerfile
    FROM python:3.11-slim
    
    RUN pip install lgpio gpiozero
    
    WORKDIR /app
    
    COPY . .
    
    CMD ["python3", "chime.py"]
    
  • chime.py
    from gpiozero import PWMOutputDevice
    from time import sleep
    
    # PWM0 is on GPIO 12
    buzzer = PWMOutputDevice(pin=12, frequency=440)  # 440 Hz is the frequency of the A4 note
    
    try:
        print("Playing sound on buzzer...")
        while True:
            buzzer.value = 0.5  # 50% duty cycle for a continuous tone
            sleep(1)            # Play the tone for 1 second
            buzzer.off()        # Turn off the buzzer
            sleep(1)            # Wait for 1 second before playing again
    except KeyboardInterrupt:
        print("Program stopped")
    finally:
        buzzer.close()
    

3. コンテナビルド・起動

  1. コンテナビルド
    $ podman build -t gpiozero-sound .
    
  2. コンテナ起動
    $ podman run -it --privileged gpiozero-sound
    

ブザーが鳴ったら成功

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?