1
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?

Raspberry Pi5でC言語使ってLチカ

Last updated at Posted at 2024-08-23

Raspberry Pi5からGPIO接続について一部変更点があり、4で動いてたコードがそのまま使えないケースが発生しました。

動いたサンプルコード



#include 
#include 
#include 
#include 
#define CONSUMER "LED"
int main(int argc, char **argv)
{
const char *chipname = "gpiochip4";
struct gpiod_chip *chip;
struct gpiod_line *line;
int ret;
// GPIOチップを開く
chip = gpiod_chip_open_by_name(chipname);
if (!chip) {
    perror("gpiod_chip_open");
    exit(EXIT_FAILURE);
}

// GPIOラインを取得する (BCM 17)
line = gpiod_chip_get_line(chip, 17);  // ここが物理ピン11に対応
if (!line) {
    perror("gpiod_chip_get_line");
    gpiod_chip_close(chip);
    exit(EXIT_FAILURE);
}

// GPIOラインを出力としてリクエストする
ret = gpiod_line_request_output(line, CONSUMER, 0);
if (ret < 0) {
    perror("gpiod_line_request_output");
    gpiod_chip_close(chip);
    exit(EXIT_FAILURE);
}

// LEDの点滅
for (;;) {
    gpiod_line_set_value(line, 1); // LEDをオン
    printf("LED ON\n");
    usleep(100000);                // 100ms待機
    gpiod_line_set_value(line, 0); // LEDをオフ
    printf("LED OFF\n");
    usleep(100000);                // 100ms待機
}

// 解放処理
gpiod_chip_close(chip);
return EXIT_SUCCESS;

}

確認すべき点

Raspberry Piは、複数のGPIOチップがいくつかのGPIOピンをまとめて管理しています。刺そうと思っているピンに応じて*chipname = "gpiochip?"(?には適切なチップ番号を入れる。今回使用した17番ピンはチップ番号4で動きました。)を設定する必要があります。
1
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
1
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?