본문 바로가기
대외활동/시스템프로그래밍

0927 라즈베리파이 GPIO

by hoshi03 2024. 9. 27.

wiringpi 설치

wget \https://github.com/WiringPi/WiringPi/releases/download/3.10/wiringpi_3.10_arm64.deb
sudo apt install ./wiringpi_3.10_arm64.deb
sudo apt install wiringpi

gpio 18번 끄고키기

gpio -g mode 18 out
gpio -g write 18 0
gpio -g write 18 1

 

깜빡이게 만들기

 

#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>

int ledcontrol(int gpio){
    int i;
    pinMode(gpio, OUTPUT);

    for (int i = 0; i < 5000; i++)
    {
        digitalWrite(gpio, HIGH);
        delay(50);
        digitalWrite(gpio, LOW);
        delay(50);
    }
    return 0;    
}

int main(int argc, char **argv){
    int gno;

    if (argc < 2){
        printf("Usage : %s GPIO_NO\n", argv[0]);
        return -1;
    }

    gno = atoi(argv[1]);
    wiringPiSetup();
    ledcontrol(gno);
    return 0;
    
}

 

pwm으로 밝기 천천히 밝아지게 조절

#include <wiringPi.h>
#include <softPwm.h>
#include <stdio.h>
#include <stdlib.h>

int ledcontrol(int gpio)
{
    int i;
    pinMode(gpio, OUTPUT);
    softPwmCreate(gpio, 0, 255);

    for (int i = 0; i < 1000; i++)
    {
        softPwmWrite(gpio, i & 255);
        delay(10);
    }

    softPwmWrite(gpio, 0);
    return 0;
}

int main(int argc, char **argv)
{
    int gno;

    if (argc < 2)
    {
        printf("Usage : %s GPIO_NO\n", argv[0]);
        return -1;
    }

    gno = atoi(argv[1]);
    wiringPiSetup();
    ledcontrol(gno);
    return 0;
}

 

스위치로 불 키기 끄기

 

#include <wiringPi.h>
#include <softPwm.h>
#include <stdio.h>
#include <stdlib.h>

#define SW 5
#define LED 1

int switchcontrol()
{
    pinMode(SW, INPUT);
    pinMode(LED, OUTPUT);

    for (;;)
    {
        if (digitalRead(SW) == LOW)
        {
            digitalWrite(LED, HIGH);
        }

        else
        {
            if (digitalRead(SW) == HIGH)
            {
  
                digitalWrite(LED, LOW);
            }
        }
        delay(10);
    }
    return 0;
}

int main(int argc, char **argv)
{
    wiringPiSetup();
    switchcontrol();
    return 0;
}