[2022/10/02] First Start.
소나무 기운 , 전자제품 개발/생산
Raspberry Pi의 hardware PWM 사용하기 C,C++,Python
라즈베리파이에서는 하드웨어 PWM과 소프트웨어 PWM을 사용할 수 있습니다.여기서는 하드웨어 PWM을 사용하는 방법에 대해서 알아 보도록 할겠습니다.
Hardware PWM and software PWM are available on Raspberry Pi.Here we'll learn how to use hardware PWM.
하드웨어 PWM 사용할 수 있도록 설정하기, Enableing Hardware PWM
하드웨어를 사용할 수 있도록 설정하지 않은 상태에서 소프트웨어를 작성해서 사용하고자 하면 에러가 발생합니다.
Attempts to create and use software without hardware enabled result in an error.
Unhandled exception. System.ArgumentException: The chip number 0 is invalid or is not enabled.
이와 같은 에러메세지를 출력합니다. 이는 사용권한이 없다는 말입니다.
라즈베리파이에서 사용가능한 하드웨어 PWM포트는 아래 표와 같습니다.
PWM포트는 하나 또는 두개를 사용할 수 있습니다.
Outputs an error message like this. This means you don't have permission.
The available hardware PWM ports for Raspberry Pi are shown in the table below.
You can use one or two PWM ports.
/boot/config.txt파일을 수정해 주어야 합니다.
The /boot/config.txt file must be modified.
sudo nano /boot/config.txt
"dtoverlay=pwm" 부분을 찾아 수정합니다.
Locate and correct the "dtoverlay=pwm" part.
예를 들어서 PWM 1을 GPIO 18번으로 출력하고자 한다면
#dtoverlay=pwm부분을 dtoverlay=pwm,pin=18,func=4로 수정합니다.
For example, if you want to output PWM 1 to GPIO 18,
Modify the #dtoverlay=pwm part to dtoverlay=pwm, pin=18, func=4.
한개만 사용하는 경우 ( If only one is used ) :
PWM 번호 | GPIO | Function | Alt | dtoverlay |
PWM 0 | 12 | 4 | Alt 0 | dtoverlay=pwm,pin=12,func=4 |
PWM 0 | 18 | 2 | Alt 5 | dtoverlay=pwm,pin=18,func=2 |
PWM 1 | 13 | 4 | Alt 0 | dtoverlay=pwm,pin=12,func=4 |
PWM 1 | 18 | 2 | Alt 5 | dtoverlay=pwm,pin=18,func=2 |
두개 사용하는 경우 ( If you use two ) :
PWM 0 GPIO |
PWM 0 Function |
PWM 0 Alt |
PWM 1 GPIO |
PWM1 Function |
PWM1 Alt |
dtoverlay |
12 | 4 | Alt 0 | 13 | 4 | Alt0 | dtoverlay=pwm-2chan,pin=12,func=4,pin2=13,func2=4 |
18 | 2 | Alt 5 | 13 | 4 | Alt0 | dtoverlay=pwm-2chan,pin=18,func=2,pin2=13,func2=4 |
12 | 4 | Alt 0 | 19 | 2 | Alt 5 | dtoverlay=pwm-2chan,pin=12,func=4,pin2=19,func2=2 |
18 | 2 | Alt 5 | 19 | 2 | Alt 5 | dtoverlay=pwm-2chan,pin=18,func=2,pin2=19,func2=2 |
수정이 완료되면 Ctrl + X를 눌러 빠져나갑니다. 이때 저항할 것이 물으면 저항합니다.
When the modification is complete, press Ctrl + X to exit. At this time, if you ask what you want to resist, you will resist.
sudo reboot now
재부팅합니다.
Reboot.
관리자 권한으로 실행해 주면 됩니다.
You can run it with administrator privileges.
/sys/class/pwm/pwmchip0 이 생성된것을 볼 수 있습니다.
You can see that /sys/class/pwm/pwmchip0 was created.
PWM 포트 사용하기 C++, Using PWM Ports C++
C++로 된 간단한 예제입니다.
using System;
using System.Device.Pwm;
namespace TestTest
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello PWM!");
var pwm = PwmChannel.Create(0, 0, 400, 0.5);
pwm.Start();
Console.ReadKey();
}
}
}
PWM 포트 사용하기 C, Using PWM Ports C
C로 된 간단한 예제입니다.
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
const int PWM_pin = 1; /* GPIO 1 as per WiringPi, GPIO18 as per BCM */
int main (void)
{
int intensity ;
if (wiringPiSetup () == -1)
exit (1) ;
pinMode (PWM_pin, PWM_OUTPUT) ; /* set PWM pin as output */
while (1)
{
for (intensity = 0 ; intensity < 1024 ; ++intensity)
{
pwmWrite (PWM_pin, intensity) ; /* provide PWM value for duty cycle */
delay (1) ;
}
delay(1);
for (intensity = 1023 ; intensity >= 0 ; --intensity)
{
pwmWrite (PWM_pin, intensity) ;
delay (1) ;
}
delay(1);
}
}
PWM 포트 사용하기 C, Using PWM Ports C (Software PWM)
C로 된 간단한 예제입니다.
#include <wiringPi.h> /* include wiringPi library */
#include <stdio.h>
#include <softPwm.h> /* include header file for software PWM */
int main(){
int PWM_pin = 1; /* GPIO1 as per WiringPi,GPIO18 as per BCM */
int intensity;
wiringPiSetup(); /* initialize wiringPi setup */
pinMode(PWM_pin,OUTPUT); /* set GPIO as output */
softPwmCreate(PWM_pin,1,100); /* set PWM channel along with range*/
while (1)
{
for (intensity = 0; intensity < 101; intensity++)
{
softPwmWrite (PWM_pin, intensity); /* change the value of PWM */
delay (10) ;
}
delay(1);
for (intensity = 100; intensity >= 0; intensity--)
{
softPwmWrite (PWM_pin, intensity);
delay (10);
}
delay(1);
}
}
PWM 포트 사용하기 Python, Using PWM Ports Python
Python로 된 간단한 예제입니다.
import RPi.GPIO as GPIO
from time import sleep
ledpin = 12 # PWM pin connected to LED
GPIO.setwarnings(False) #disable warnings
GPIO.setmode(GPIO.BOARD) #set pin numbering system
GPIO.setup(ledpin,GPIO.OUT)
pi_pwm = GPIO.PWM(ledpin,1000) #create PWM instance with frequency
pi_pwm.start(0) #start PWM of required Duty Cycle
while True:
for duty in range(0,101,1):
pi_pwm.ChangeDutyCycle(duty) #provide duty cycle in the range 0-100
sleep(0.01)
sleep(0.5)
for duty in range(100,-1,-1):
pi_pwm.ChangeDutyCycle(duty)
sleep(0.01)
sleep(0.5)
마무리
PWM 신호는 듀티를 변경하며 사용합니다. LED의 밝기를 조절합니다. LCD의 Back light의 밝기를 조절하는데 사용합니다.모터의 구동신호등에도 사용됩니다.눈에 보이는 LED정도의 조절에는 사용이 가능합니다. 하지만 PWM신호를 평활하여 Analog Out 신호로 사용하는 것은 권장하지 않습니다. 아나로그 신로호 변환시 작은 Duty값에서의 선형성이 떨어집니다.
The PWM signal is used by changing the duty. Adjust the brightness of the LED. Use to adjust the brightness of the LCD's backlight.Also used for drive signals on the motor.It can be used to adjust the visible LED level. However, it is not recommended to smooth the PWM signal and use it as an Analog Out signal. The linearity of the small Duty value is reduced when converting the analog signal.
참고문헌
https://www.electronicwings.com/raspberry-pi/raspberry-pi-pwm-generation-using-python-and-c
https://github.com/dotnet/iot/blob/main/Documentation/raspi-pwm.md
틀린 부분이나 질문은 댓글 달아주세요.
즐거운 하루 보내세요. 감사합니다.
'Raspberry Pi (Linux, ubuntu)' 카테고리의 다른 글
라즈베리파이 디스크 사용량, 남은 용량 확인하기 (Raspberry Pi Disk Usage, Check Capacity Remaining ) (0) | 2022.10.04 |
---|---|
라즈베리파이 이어폰 젝 오디오 출력하기 (1) | 2022.09.26 |
To fix a black border in Raspberry Pi ( Raspberry Pi에서 화면 불일치를 수정하는 방법 ) (0) | 2022.09.14 |
To change permissions on folders and subfolders/files in Linux at once (하위폴더까지 한번에 권한 변경하기) (0) | 2022.09.13 |
Shutdown and Reboot from inside a C program (0) | 2022.09.12 |
댓글