05 제어기 활용-GPIO

Download Report

Transcript 05 제어기 활용-GPIO

제어기 활용-GPIO
Ch.05
RaspberryPi
Sejin Oh
제어기 활용-GPIO
GPIO
 GPIO (General Purpose Input Output)
 마이크로프로세서가 주변장치와 통신하기 위해 범용으로 사용되는
입출력 포트
 입력과 출력을 마음대로 선택할 수 있고, 0과 1의 출력 신호를 임의
로 만들어줄 수 있는 구조를 가진다.
 GPIO 라이브러리
 라즈베리 파이의 GPIO를 제어할 수 있는 언어
• C, C++, C#, Python Perl, Ruby, Java등 매우 다양함
 각 언어에 따라 사용자들이 GPIO 라이브러리를 제작하여 공유하고
있다.
 wiringPi : 공개된 라이브러리 중 GPIO 제어 속도가 빠름
Raspberry Pi
2
제어기 활용-GPIO
GPIO
 Raspberry Pi b+ GPIO 핀 배치
Raspberry Pi
3
제어기 활용-GPIO
GPIO Library 설치
1. 라즈베리 파이의 업데이트 및 업그레이드 실시
$ sudo apt-get update
$ sudo apt-get upgrade
2. 소스 관리 툴인 git을 다운로드
$ sudo apt-get install git-core
3. Git을 이용하여 “Wiring Pi” 라이브러리를 다운로드
$ git clone git://git.drogon.net/wiringPi
Raspberry Pi
4
제어기 활용-GPIO
GPIO Library 설치
4. 빌드 및 설치 진행
$ cd wiringPi
$ ./build
5. 설치 확인
$ gpio -v
설치 확인 (예시)
Gpio version: 2.08
Copyright © 2012-2013 Gordon Henderson
This is free software with ABSOLUTELY NO WARRANTY
For details type: gpio –warranty
This Raspberry Pi is a reversion 2 board
Raspberry Pi
5
제어기 활용-GPIO
GPIO 출력 테스트
 GPIO의 출력을 이용하여 LED on/off 제어 테스트
 LED 회로를 구성하여 GPIO에 High 값을 출력해주면 LED는 켜짐
 GPIO에 Low 값을 출력하게 되면 LED는 꺼짐
 LED 2개, 저항 220Ω 2개, GPIO 23번 핀, 24번 핀 이용
Raspberry Pi
6
제어기 활용-GPIO
GPIO 출력 테스트
1. 작업 폴더 생성 및 이동
$ mkdir gpio
$ cd gpio
2. 프로그램 작성
$ sudo nano output.c
Raspberry Pi
7
제어기 활용-GPIO
GPIO 출력 테스트
#include <stdio.h>
#include <wiringPi.h>
while(1)
{
digitalWrite(LED1, 1);
digitalWrite(LED2, 1);
#define LED1 23
#define LED2 24
delay(500); // ms
int main(void)
{
if(wiringPiSetupGpio()==-1)
return 1;
digitalWrite(LED1, 0);
digitalWrite(LED2, 0);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
}
delay(500);
return 0;
}
Raspberry Pi
8
제어기 활용-GPIO
GPIO 출력 테스트
3. 프로그램 빌드
$ gcc –o output output.c -lwiringPi
4. 프로그램 실행
$ sudo ./output
Raspberry Pi
9
제어기 활용-GPIO
GPIO 출력 테스트
 python을 이용한 LED on/off 테스트
 $ sudo python
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.OUT)
GPIO.setup(24, GPIO.OUT)
while (True):
GPIO.output(23,
GPIO.output(24,
time.sleep(0.5)
GPIO.output(23,
GPIO.output(24,
time.sleep(0.5)
Raspberry Pi
True)
True)
False)
False)
10
제어기 활용-GPIO
GPIO 입력 테스트
 스위치 회로
 스위치의 on/off 신호에 따라 LED가 켜지고 꺼지는 실습
 GPIO 입력 테스트에 사용될 GPIO는 25번
 스위치에 10KΩ 연결
Raspberry Pi
11
제어기 활용-GPIO
GPIO 입력 테스트
 프로그램 작성
1. 작업 폴더로 이동
•
2.
$ sudo cd ~/gpio
pinMode(SW, INPUT);
프로그램 작성
•
$ sudo nano input.c
while(1)
{
digitalWrite(LED1, 0);
digitalWrite(LED2, 0);
#include <stdio.h>
#include <wiringPi.h>
#define LED 23 //gpio 23
#define LED 24 //gpio 24
#define SW 25 //gpio 25
if(digitalRead(SW) == 1)
{
digitalWrite(LED1, 1);
digitalWrite(LED2, 1);
delay(1000);
}
int main(void)
{
if(wiringPiSetupGpio() == -1)
return 1;
}
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
Raspberry Pi
12
}
return 0;
제어기 활용-GPIO
GPIO 입력 테스트
 프로그램 작성
3. 프로그램 빌드
•
4.
프로그램 실행
•
Raspberry Pi
$ sudo –o input input.c -lwiringPi
$ sudo ./input
13
제어기 활용-GPIO
Ultra Sonic Sensor
 초음파 센서
 우선 trigger 핀에서 10us 정도의 High 신호를 주면 초음파 센서는
40Khz 펄스를 자동으로 8번 발생시킴
 펄스 발생 후 echo 핀은 High로 되고 반사된 초음파가 감지될때
Low가 됨
 거리 측정
• echo 핀이 High에서 Low로 걸리는 시간을 측정
• 측정된 시간을 초음파의 속도(즉, 58)로 나누면 거리가 측정됨
Raspberry Pi
14
제어기 활용-GPIO
Ultra Sonic Sensor
Raspberry Pi
15
VCC
5V연결
GND
GND연결
Echo
GPIO 23 연결
Trigger
GPIO 24 연결
제어기 활용-GPIO
Ultra Sonic Sensor – ultrasonic.c
#include <stdio.h>
#include <wiringPi.h>
digitalWrite(TRIG,LOW);
#define TRIG 24
#define ECHO 23
while(digitalRead(ECHO) == LOW);
long startTime = micros();
while(digitalRead(ECHO) == HIGH);
long travelTime = micros() - startTime;
int main(void) {
int distance = 0;
int pulse = 0;
if(wiringPiSetupGpio() == -1)
return 1;
pinMode(TRIG,OUTPUT);
pinMode(ECHO,INPUT);
}
}
int distance = travelTime / 58;
printf("Distance: %d cm\n", distance);
delay(100);
for(;;){
digitalWrite(TRIG,LOW);
usleep(2);
digitalWrite(TRIG,HIGH);
usleep(20);
Raspberry Pi
16
제어기 활용-GPIO
Ultra Sonic Sensor
Raspberry Pi
17
제어기 활용-GPIO
온습도 센서
 온습도 센서(DHT11 Sensor)
Raspberry Pi
18
VCC
3V연결
GND
GND연결
Data
GPIO 4 연결
Resistance
10K
제어기 활용-GPIO
온습도 센서
 Raspberry Pi b+ GPIO 핀 배치
Raspberry Pi
19
제어기 활용-GPIO
온습도 센서 – C file
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define MAXTIMINGS 83
#define DHTPIN 7
void read_dht11_dat()
{
uint8_t laststate = HIGH ;
uint8_t counter = 0 ;
uint8_t j = 0, i ;
uint8_t flag = HIGH ;
uint8_t state = 0 ;
float f ;
int dht11_dat[5] = {0, } ;
uint8_t는 unsigned 8 bit 타입 변수
char, int가 word의 크기에 따라 달라지는 것을 방지하기 위함
Raspberry Pi
20
제어기 활용-GPIO
온습도 센서 – C File
dht11_dat[0] = dht11_dat[1] = dht11_dat[2] = dht11_dat[3] = dht11_dat[4] = 0 ;
pinMode(DHTPIN, OUTPUT) ;
digitalWrite(DHTPIN, LOW) ;
delay(18) ;
처음 신호선으로 LOW를 18ms 동안,
20~40us 동안 HIGH 신호를 보내주면
Start 신호
본소스에서는 중간값 30 사용
digitalWrite(DHTPIN, HIGH) ;
delayMicroseconds(30) ;
pinMode(DHTPIN, INPUT) ;
라즈베리 파이가 신호를 받아야하므로 INPUT
MAXTIMINGS 83
40개 데이터 비트 * 2(Low, High)
+ 3개 처음비트
for (i = 0; i < MAXTIMINGS; i++) {
counter = 0 ;
high->low/low->high 반대안되면
while ( digitalRead(DHTPIN) == laststate) {
카운트 증가 시키며 200us 대기
counter++ ;
delayMicroseconds(1) ;
if (counter == 200) break ;
200us 동안 변화 없을시 break
}
Raspberry Pi
21
제어기 활용-GPIO
온습도 센서 – C File
온습도 센서는 신호의 길이로 데이
터를 쓰기 때문에 counter가 20넘
어가면 1을 씀(or연산 사용)
laststate = digitalRead(DHTPIN) ;
if (counter == 200) break ;
}
//0~3비트 버리고 짝수 번째 비트인지 확인
if ((i >= 4) && (i % 2 == 0)) {
dht11_dat[ j / 8] <<= 1 ;
if (counter > 20) dht11_dat[ j / 8] |= 1 ;
j++ ;
}
// 0,1,2,3 더해서 패리티 비트 4와 비교
if (( j >= 40) && (dht11_dat[4] == ((dht11_dat[0] + dht11_dat[1] + dht11_dat[2] +
dht11_dat[3]) & 0xff))) {
printf("humidity = %d.%d %% Temperature = %d.%d *C \n", dht11_dat[0], dht11_dat[1],
dht11_dat[2], dht11_dat[3]) ;
}
체크썸 0xff
else printf("Data get failed\n") ;
}
Raspberry Pi
22
제어기 활용-GPIO
온습도 센서 – C File
int main(void)
{
printf("dht11 Raspberry pi\n") ;
if (wiringPiSetup() == -1) exit(1) ;
}
while (1) {
read_dht11_dat() ;
delay(1000) ;
}
return 0 ;
Raspberry Pi
23
제어기 활용-GPIO
온습도 센서 – 결과
Raspberry Pi
24
제어기 활용-GPIO
실습
 안전 종료를 위한 매크로 버튼
 page 165
 버튼과 스피커를 이용한 전자 피아노 건반
 page 170
 초음파 센서를 이용한 후방감지 시스템
 온/습도 센서를 이용한 디지털 온도계
Raspberry Pi
25
제어기 활용-GPIO
WebIOPi
 WebIOPi
 WebIOPi는 라즈베리 파이의 GPIO를 웹 브라우저에서 제어하기 위
해 만들어진 라즈베리 파이의 GPIO 제어 프레임워크.
 라즈베리 파이를 파이썬으로 GPIO를 제어하면서 동시에 서버로써
구동하여 원격 제어도 가능
 웹 브라우저에서는 HTTP REST API를 이용하여 서버에 원격 접속 및
GPIO를 제어할 수 있다.
Raspberry Pi
26
제어기 활용-GPIO
WebIOPi 설치
 webiopi 다운로드 및 설치
 $ wget http://webiopi.googlecode.com/files/WebIOPi-0.6.0.tar.gz
 $ tar xvzf WebIOPi-0.6.0.tar.gz
 $ cd WebIOPi-0.6.0
 $ sudo ./setup.sh
Raspberry Pi
27
제어기 활용-GPIO
WebIOPi 설치
 webiopi Patch
 $ wget http://sourceforge.net/projects/webiopi/files/WebIOPi0.7.1.tar.gz
 $ tar xvzf WebIOPi-0.7.1.tar.gz
 $ cd WebIOPi-0.7.1
 $ wget
https://raw.githubusercontent.com/doublebind/raspi/master/webio
pi-pi2bplus.patch
 $ patch -p1 -i webiopi-pi2bplus.patch
 $ sudo ./setup.sh
Raspberry Pi
28
제어기 활용-GPIO
WebIOPi 설치
 webiopi 시작
 $ sudo service webiopi start
 webiopi 종료
 $ sudo service webiopi stop
Raspberry Pi
29
제어기 활용-GPIO
WebIOPi 설치
 webiopi 를 이용한 gpio 제어
 데스크톱에서 자신의 ip주소로 접속하면 webiopi 메인화면 출력
 브라우저
• http://IP주소:8000
 인증
• id: webiopi
• pass: raspberry
Raspberry Pi
30
제어기 활용-GPIO
WebIOPi를 이용한 LED 제어
 LED 제어 예제 프로그램 다운로드
 $ git clone https://github.com/rasplay/traffic_light.git
 $ cd traffic_light
 $ sh setup.sh
 접속 후 예제 프로그램 확인
 브라우저에서 http://IP주소:8000 접속 확인
Raspberry Pi
31
제어기 활용-GPIO
WebIOPi를 이용한 LED 제어
 회로 구성
 LED 8개, 220Ω 8개 연결
 GPIO Pin 번호
• 10, 9, 11, 25, 7, 24, 8, 18번 핀에 LED 연결
Raspberry Pi
32
제어기 활용-GPIO
WebIOPi를 이용한 LED 제어 – 결과 확인
 결과확인
 브라우저 및 휴대폰을 이용한 결과확인
Raspberry Pi
33
Thank you
Raspberry Pi
34