Raspberry Pi GPIO Port (LED, Button) Control
네오즈 · 2019-09-26 · 4
# 1 Raspberry Pi GPIO
The figure below shows the names and functions of the Raspberry Pi GPIO 40-pin header.
[Image]
The expanded 40-pin header diagram shows two types of pin numbers and nomenclature to be used when writing control programs on both left and right sides. Looking at pin 8, it is labeled as 'TxD / GPIO 14 / GPIO 15'. Note that above 'GPIO 14' marked in black, 'BCM' is indicated, and above 'GPIO 15' marked in gray, 'wPi' is indicated.
Since one GPIO pin has two different names, it can be confusing. This phenomenon occurred because the GPIO pin naming standards were not consistent when creating libraries.
- BCM is the nomenclature that applies the physical pin number of the BCM283x chip, which is used by the default provided 'Python' library.
- wPi is the nomenclature that applies which number pin it is among the GPIO pins of the BCM283x chip, used in wiringPi, a C language library created and distributed by a person named Gordon.
- 'TxD' on GPIO header pin 8 means that in addition to the input/output function of GPIO, when using additional functions, the 'transmit' function of the serial communication port can be used.
# 2 Raspberry Pi GPIO Programming
## 2.0 wiringPi Library Installation
- Install the C language GPIO library "wiringPi". (The Python library is included in the default system image.)
First, enter the command below to fetch the source files from the manufacturer (Gordon)'s Github.
[Image]
("drogon" is not a typo. It is the manufacturer's own name "gordon" with the spelling order changed. It is not "dragon".)
After the Git command completes, if you run the 'ls' command, you can confirm that a "wiringPi" folder is created as shown below.
[Image]
If connection fails, use GitHub instead.
[Image]
Change the path to "wiringPi" as shown below and build with the "./build" command.
[Image]
When the build is complete, you can verify installation by running "gpio ?v", "gpio readall", etc.
[Image]
[Image]
**2.1 GPIO Output**
**1 GPIO Output Test Circuit Configuration**
[Image]
(1 Red LED, 1 Green LED each, 220Ω resistor 2 pieces)
Red Led: Connected to GPIO 23, Green Led: Connected to GPIO 24
### 2. GPIO Output Test Python Code Writing
Create a working folder, change the path, and run the "nano" editor.
[Image]
[Image]
Write the code below using the "nano" editor.
```
import RPi.GPIO as GPIO # RPi.GPIO에 정의된 기능을 GPIO명칭으로 사용
import time # time에 정의된 기능을 사용( time.sleep )
GPIO.setmode(GPIO.BCM) # GPIO 이름은 BCM 명칭 사용
GPIO.setup(23, GPIO.OUT) # GPIO 23 출력으로 설정
GPIO.setup(24, GPIO.OUT) # GPIO 24 출력으로 설정
print "GPIO Test~, press Ctrl+C to quit" # ""메세지 화면 출력
try: # try:행과 아래except KeyboardInterrupt:
while True: # 무한 반복문 - C언어의 while(1)에 해당
GPIO.output(23, True ) # GPIO 23에 HIGH 출력( 적색 LED 점등 )
GPIO.output(24, True ) # GPIO 24에 HIGH 출력( 녹색 LED 점등 )
time.sleep(0.5) # 0.5초 동안 대기
GPIO.output(23, False) # GPIO 23에 LOW 출력( 적색 LED 소등 )
GPIO.output(24, False) # GPIO 24에 LOW 출력( 녹색 LED 소등 )
time.sleep(0.5) # 0.5초 동안 대기
# 여기까지while TRUE: 반복구간( 들여쓰기로 구분 )
except KeyboardInterrupt: # Ctrl-C 입력 발생 시
GPIO.cleanup() # GPIO 관련설정 Clear
print "bye~" # 프로그램 종료 메세지 화면 출력
```
[Image]
Select "y" for the edit content save option.
[Image]
At the save filename input stage, since you will use the filename you entered when running the nano editor, press Enter as is.
[Image]
### 3. GPIO Output Test C Code Writing Using wiringPi Library
In the working folder created earlier, **edit "outEx01.c" using the "nano" editor**
[Image]
**Write and save the code below**
```
#include // stdio.h 파일 포함
#include // wiringPi.h 파일 포함
#define LED1 4 // 4번핀(GPIO 23) 대신 LED1사용을 위한 정의
#define LED2 5 // 5번핀(GPIO 24) 대신 LED2사용을 위한 정의
int main (void)
{
printf("Control GPIO by wiringPi\n"); // 메시지 화면 출력
wiringPiSetup(); // wiringPi 라이브러리 설정
pinMode(LED1, OUTPUT); // 4번핀(GPIO 23) 출력 설정
pinMode(LED2, OUTPUT); // 5번핀(GPIO 24) 출력 설정
while(1) // 무한 반복 구간
{
digitalWrite(LED1, 1); // 4번핀(GPIO 23) HIGH 출력( 적색 LED 점등 )
digitalWrite(LED2, 1); // 5번핀(GPIO 24) HIGH 출력( 녹색 LED 점등 )
delay(500); // 500ms (0.5초) 동안 대기
digitalWrite(LED1, 0); // 4번핀(GPIO 23) LOW 출력( 적색 LED 소등 )
digitalWrite(LED2, 0); // 5번핀(GPIO 24) LOW 출력( 녹색 LED 소등 )
delay(500); // 500ms (0.5초) 동안 대기
}
return 0;
}
```
**Compile the written code and generate executable code**
[Image]
- If an error occurs, modify the code by referring to the message on the screen and re-run it.
- In the above command, "outEx01" is the executable file name to be created, and "outEx01.c" is the source file to be compiled.
- "-l wiringPi" means to compile using the "wiringPi" library during compilation.
**Verify compilation results**
[Image]
- You can confirm that the "outEx01" executable file is created with the "ls" command.
**Run the code**
[Image]
- Verify that the red and green LEDs blink at 0.5-second intervals.
- Press Ctrl-C to terminate the running code.
**2.2 GPIO Input**
### 1. GPIO Input Test Circuit Configuration
[Image]
(1 Red LED, 1 Green LED each, 220Ω resistor 2 pieces, 10kΩ resistor 1 piece, SW 1 piece)
Red Led: Connected to GPIO 23, Green Led: Connected to GPIO 24, Button: Connected to GPIO 18
### 2. GPIO Input Test Python Code Writing
In the working folder created earlier, **edit "inEx01.py" using the "nano" editor**
[Image]
**Write and save the code below**
```
import RPi.GPIO as GPIO # RPi.GPIO에 정의된 기능을 GPIO 명칭으로 사용
GPIO.setmode( GPIO.BCM ) # GPIO 이름은 BCM 명칭 사용
GPIO.setup(23, GPIO.OUT) # GPIO 23 출력으로 설정
GPIO.setup(24, GPIO.OUT) # GPIO 24 출력으로 설정
GPIO.setup(18, GPIO.IN ) # GPIO 18 입력으로 설정
print "Press SW or input Ctrl+C to quit" # 메세지 화면 출력
try: # try:행과 아래 except KeyboardInterrupt:
# 이하는 생략가능( GPIO warnning 방지 )
while True: # 무한 반복문 - C언어의 while(1)에 해당
GPIO.output(23, False) # GPIO 23에 LOW 출력( 적색 LED 소등 )
GPIO.output(24, False) # GPIO 24에 LOW 출력( 녹색 LED 소등 )
while GPIO.input(18) == 0: # SW가 ON 인 동안 반복
GPIO.output(23, True) # GPIO 23에 HIGH 출력( 적색 LED 점등 )
GPIO.output(24, True) # GPIO 24에 HIGH 출력( 녹색 LED 점등 )
except KeyboardInterrupt: # Ctrl-C 입력 시
GPIO.cleanup() # GPIO 관련설정 Clear
print "bye~" # 프로그램 종료 메세지 화면 출력
```
Enter the command below to **run the written code**
[Image]
- Verify that the LED lights up while the switch is pressed. (Terminate with Ctrl-C)
### 3. GPIO Input Test C Code Writing Using wiringPi Library
**Edit "inEx01.c" using the "nano" editor**
[Image]
**Write and save the code below**
```
#include // stdio.h 파일 포함( printf() 사용을 위해 )
#include // wiringPi.h 파일 포함
#define SW 1 // 1번핀(GPIO 18) 대신 SW 사용을 위한 정의
#define LED1 4 // 4번핀(GPIO 23) 대신 LED1사용을 위한 정의
#define LED2 5 // 5번핀(GPIO 24) 대신 LED2사용을 위한 정의
int main (void)
{
printf("Control GPIO by wiringPi\n");
wiringPiSetup(); // wiringPi 라이브러리 설정( pinMode(), digitalWrite() 등의 사용을 위해 )
pinMode(SW , INPUT ); // 1번핀(GPIO 18) 입력 설정
pinMode(LED1, OUTPUT); // 4번핀(GPIO 23) 출력 설정
pinMode(LED2, OUTPUT); // 5번핀(GPIO 24) 출력 설정
while(1) // 무한 반복 구간
{
digitalWrite(LED1, 0); // 4번핀(GPIO 23) LOW 출력( 적색 LED 소등 )
digitalWrite(LED2, 0); // 5번핀(GPIO 24) LOW 출력( 녹색 LED 소등 )
while( digitalRead(SW) == 0 ) // 스위치가 ON인 동안 반복구간
{
digitalWrite(LED1, 1); // 4번핀(GPIO 23) HIGH 출력( 적색 LED 점등 )
digitalWrite(LED2, 1); // 5번핀(GPIO 24) HIGH 출력( 녹색 LED 점등 )
}
}
return 0; // int main()의 리턴값-의미는 없지만 문법상 필요
}
```
**Compile the written code and generate executable code**
[Image]
**Verify compilation results**
[Image]
**Run the code**
[Image]
- Verify that the LED lights up while the switch is pressed. (Terminate with Ctrl-C)
4
댓글 0개
등록된 댓글이 없습니다.