Processor

【라즈베리파이】 wiringPi C 언어 : DC 모터(3V, 5V)

작성자 임베디드코리아 작성일26-02-17 18:04 조회89회 댓글0건
◆ DC 모터 제어를 ON, OFF 로 하는 코드와 PWM 방식으로 하는 코드를 추가한다.
◆ GPIO 제어 핀 넘버를 이용하며, 12V 전원은 릴레이를 통해 제어하면 된다.

--->>> 예제  DC_motor01.c  <<<---------------------------------
#include <signal.h> //Signal 사용 헤더파일
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h> //exit() 사용 헤더파일

#include <wiringPi.h>

#define DCMOTOR 23 // BCM_GPIO 13

void sig_handler(int signo); // SIGINT 사용 마지막 종료 함수

int main (void)
{
signal(SIGINT, (void *)sig_handler); //시그널 핸들러 함수 Ctrl+C

if (wiringPiSetup () == -1)
{
fprintf(stdout, "Unable to start wiringPi: %s\n", strerror(errno));
return 1 ;
}

pinMode (DCMOTOR, OUTPUT) ;

for (;;)
{
//printf("here - DCMOTOR on\n");
digitalWrite (DCMOTOR, 1) ; // On
}
return 0 ;
}

void sig_handler(int signo)
{
    printf("process stop\n");
digitalWrite (DCMOTOR, 0) ; // Off

exit(0);
}
---------------------------------------------------------------------------------
$ gcc  -o  DC_motor01  DC_motor01.c  -lwiringPi
$ sudo  DC_motor01

◆  DC Motor PWM 으로 속도를 제어한다.
◆  softPwmCreate(MOTORCONTROL, 0, 100);  코드에서 100을 10까지 변환하여 컴파일 후 실행하면 DC 모터 속도가 줄어든다.

--->>> 예제  DC-Motor_speed.c    <<<----------------------------------
#include <signal.h> //Signal 사용 헤더파일
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h> //exit() 사용 헤더파일
#include <wiringPi.h>
#include <softPwm.h>

#define MOTORCONTROL  23 //GPIO 13 - Motor

void sig_handler(int signo); // SIGINT 핸들러 함수

int main (void)
{
if(wiringPicheck()) printf("Fail\n");

signal(SIGINT, (void *)sig_handler);  // Ctrl+C

pinMode (MOTORCONTROL, OUTPUT);

softPwmCreate(MOTORCONTROL, 0, 100);

int i;
int delaytime = 100;
while(1)
{
printf("here \n");

softPwmWrite(MOTORCONTROL, 5);
delay(delaytime);

}
 
  return 0 ;
}

int wiringPicheck(void)
{
if (wiringPiSetup () == -1)
{
fprintf(stdout, "Unable to start wiringPi: %s\n", strerror(errno));
exit(1) ;
}
}

void Bpluspinmodeset(void)
{
pinMode (MOTORCONTROL, OUTPUT);

}

void sig_handler(int signo) // ctrl-c 로 종료시 실행되는 함수
{
    printf("process stop\n");
digitalWrite(MOTORCONTROL, 0);
exit(0);
}
---------------------------------------------------------------------------------
$ gcc  -o  DC-Motor_speed  DC-Motor_speed.c  -lwiringPi
$ sudo  ./DC-Motor_speed