Post on 20-Jul-2020
Raspberry Pi 입출력디바이스II 1
제05강
입출력 디바이스 II
터치 스위치
자석 스위치
움직임감지센서
부저 모듈
LED Array
RGB LED
릴레이 모듈
초음파 센서(거리측정)
적외선 센서 및 리모콘(미작성)
Raspberry Pi 입출력디바이스II 2
터치스위치
* IM120710023 터치스위치
: 유형 ... 토글형 및 비토글형
: S(디지털 출력), V(5V), G(GND)
: 토글형 ... 터치시 이전 상태의 보 신호출력
: 비토글형 ... 터치시 Low 신호출력, 비터치시 High 신호 출력
Raspberry Pi 입출력디바이스II 3
터치스위치(계속)
[실습1] 터치스위치/LED 제어
: 비토글형 터치스위치를 터치중인동안 LED ON하는 프로그램
: 회로도
$ sudo nano touch_01.c
//=======================================
// touch_01.c
// direct touch sensor
//=======================================
#include <stdio.h>
Raspberry Pi 입출력디바이스II 4
#include <wiringPi.h>
#define P_TCH 1 // BCM_GPIO 18
#define P_LED 4 // BCM_GPIO 23
int main(void) {
printf("[P_TCH testing....]\n");
if(wiringPiSetup() == -1)
return 1;
pinMode(P_TCH, INPUT); // input
pinMode(P_LED, OUTPUT); // output
while(1) {
if(digitalRead(P_TCH)==HIGH) { // not touch
digitalWrite(P_LED, LOW);
printf(".");
}
else{ // touch
digitalWrite(P_LED, HIGH);
Raspberry Pi 입출력디바이스II 5
printf("\ntouched !!");
}
}
return 0;
}
$ make touch_01
$ ./touch_01
Raspberry Pi 입출력디바이스II 6
자석스위치
* 자석(리드)스위치
: 한쪽은 스위치이며, 다른 한쪽은 자석으로 구성
: 자석을 가까이하면 스위치 ON되는 형,
: 자석을 가까이하면 스위치 Off되는 형의 2유형
: 자석이 붙어있을 때 high신호 출력, 떨어졌을 때 low신호...
Raspberry Pi 입출력디바이스II 7
자석스위치(계속)
[실습2] 자석스위치/LED 제어
: 자석이 붙어있는 동안 LED를 ON하는 프로그램
$ sudo nano magnetic_01.c
//=======================================
// magnetic_01.c
// LED ON while magnetic ON
//=======================================
#include <stdio.h>
#include <wiringPi.h>
#define P_LED 1 // BCM_GPIO #18
#define P_MAG 5 // BCM_GPIO #24
int main(void) {
int status;
printf("[LED/MAG testing....]\n");
Raspberry Pi 입출력디바이스II 8
if(wiringPiSetup() == -1)
return 1;
pinMode(P_LED, OUTPUT);
pinMode(P_MAG, INPUT);
printf("P_LED ON while P_MAG ON.........\n");
status = 0;
while(1) {
status = digitalRead(P_MAG);
if(status==1) {
digitalWrite(P_LED, HIGH);
printf("ON.........\n");
}
else {
digitalWrite(P_LED, LOW);
printf("OFF.........\n");
}
delay(10);
}
Raspberry Pi 입출력디바이스II 9
return 0;
}
$ make magnetic_01
$ ./magnetic_01
Raspberry Pi 입출력디바이스II 10
움직임감지센서
* SEN0171 PIR 센서
: 외형
: 감지 거리 최대 7m 정도
: 3핀 인터페이스(빨강 Vcc, 검정 Gnd, 초록 감지신호선)
Raspberry Pi 입출력디바이스II 11
움직임감지센서(계속)
[실습3] 움직임감지센서 I
: SEN0171 PIR 센서 활용
: 적절히 회로 구현할 것
$ sudo nano motion_01.c
//=======================================
// motion_01.c
// LED ON while detecting motion
//=======================================
#include <stdio.h>
#include <wiringPi.h>
#define P_LED 1 // BCM_GPIO #18
#define P_MOT 5 // BCM_GPIO #24
int main(void) {
Raspberry Pi 입출력디바이스II 12
int status;
printf("[LED/MOT testing....]\n");
if(wiringPiSetup() == -1)
return 1;
pinMode(P_LED, OUTPUT);
pinMode(P_MOT, INPUT);
printf("P_LED ON while P_MOT ON.........\n");
status = 0;
while(1) {
status = digitalRead(P_MOT);
if(status==1) {
digitalWrite(P_LED, HIGH);
printf("ON.........\n");
}
else {
digitalWrite(P_LED, LOW);
printf("OFF.........\n");
Raspberry Pi 입출력디바이스II 13
}
delay(10);
}
return 0;
}
$ make motion_01
$ ./motion_01
Raspberry Pi 입출력디바이스II 14
부저 모듈
* 피에조 부저(SM-1212C)
: 외형
: 회로도
Raspberry Pi 입출력디바이스II 15
부저 모듈(계속)
* 음악연주를 위한 계이름에 따른 주파수
Raspberry Pi 입출력디바이스II 16
부저 모듈(계속)
* softTone.h 라이브러리
: 핀에 주파수 신호 출력과 관련한 함수
extern int softToneCreate(int pin);
: 주파수 신호를 출력하도록 핀 설정
extern void softToneWrite(int pin, int freq);
: 특정 주파수의 신호를 출력
extern void softToneStop(int pin);
: 주파수 신호의 출력을 정지
Raspberry Pi 입출력디바이스II 17
부저 모듈(계속)
[실습4] 부저모듈 제어 I
: 주파수를 가변하여 음 출력하는 프로그램
$ sudo nano buzer_01.c
//=======================================
// buzer_01.c
//
//=======================================
#include <stdio.h>
#include <wiringPi.h>
#include <softTone.h> // *
#define P_BUZ 1 // BCM_GPIO #13
int main(void) {
int freq;
Raspberry Pi 입출력디바이스II 18
if(wiringPiSetup() == -1)
return 1;
pinMode(P_BUZ, OUTPUT);
softToneCreate(P_BUZ);
freq = 262; // Do
while(1) {
softToneWrite(P_BUZ, freq); // 주파수 발생
delay(1000);
freq += 20;
}
softToneStop(P_BUZ); // 음 정지
delay(1000);
return 0;
}
Raspberry Pi 입출력디바이스II 19
$ make buzer_01
$ ./buzer_01
Raspberry Pi 입출력디바이스II 20
LED 어레이
[실습5] LED 어레이 제어
: 4개 LED에 이진수로 0~15까지 반복 표시하는 프로그램
(비트필드, 구조체 활용)
: 회로연결( 소스 참조 ), 도트매트릭스 모듈활용 가능
$ sudo nano ledAry_01.c
//=======================================
// ledAry_01.c
// LED Array(4 LED) control
//=======================================
#include <stdio.h>
#include <stdint.h>
#include <wiringPi.h>
#define P_LED_D0 26 // BCM_GPIO #12, LSB
Raspberry Pi 입출력디바이스II 21
#define P_LED_D1 27 // BCM_GPIO #16
#define P_LED_D2 28 // BCM_GPIO #20
#define P_LED_D3 29 // BCM_GPIO #21, MSB
struct leds {
unsigned int led0 : 1; // LSB
unsigned int led1 : 1;
unsigned int led2 : 1;
unsigned int led3 : 1; // MSB
unsigned int higher : 4; // higher nibble
};
union ucode {
uint8_t val;
struct leds pin; // bit-field
};
void setup(void) {
pinMode(P_LED_D0, OUTPUT);
pinMode(P_LED_D1, OUTPUT);
pinMode(P_LED_D2, OUTPUT);
pinMode(P_LED_D3, OUTPUT);
Raspberry Pi 입출력디바이스II 22
}
void out_data(union ucode acode) {
digitalWrite(P_LED_D0, acode.pin.led0);
digitalWrite(P_LED_D1, acode.pin.led1);
digitalWrite(P_LED_D2, acode.pin.led2);
digitalWrite(P_LED_D3, acode.pin.led3);
}
int main(void) {
union ucode acode;
int i;
printf("[LED Array testing....]\n");
if(wiringPiSetup() == -1)
return 1;
setup();
while(1) {
for(i=0; i<16; i++) {
Raspberry Pi 입출력디바이스II 23
acode.val = i;
out_data(acode);
printf("..... HEX : %0X, DEC : %2d \n",
acode.val, acode.val);
delay(1000);
}
}
return 0;
}
$ gcc –o ledAry_01 ledAry_01.c –lwiringPi
혹은, $ make ledAry_01
$ ./ledAry_01
Raspberry Pi 입출력디바이스II 24
RGB LED
* RGB LED
: 140C05 RGB LED 모듈
1) Common Anode 타입 : 다리가 긴 것이 +
2) Common Cathode 타입
Raspberry Pi 입출력디바이스II 25
RGB LED(계속)
[실습6] RGB LED 제어
: 몇가지 색상을 조합하여 표시하는 프로그램
: 각 LED를 ON하려면 Low 신호를 출력하여 함
$ sudo nano rgbLed_01.c
//=======================================
// rgbLed_01.c
// led ON (Low signal)
//=======================================
#include <stdio.h>
#include <wiringPi.h>
#define P_LED_R 0 // BCM_GPIO #17
#define P_LED_G 2 // BCM_GPIO #27
#define P_LED_B 3 // BCM_GPIO #22
Raspberry Pi 입출력디바이스II 26
int main(void) {
printf("[rgbLED testing....]\n");
if(wiringPiSetup() == -1)
return 1;
pinMode(P_LED_R, OUTPUT);
pinMode(P_LED_G, OUTPUT);
pinMode(P_LED_B, OUTPUT);
// all OFF
digitalWrite(P_LED_R, HIGH);
digitalWrite(P_LED_G, HIGH);
digitalWrite(P_LED_B, HIGH);
delay(3000);
while(1) {
printf("rLED ON.........\n");
digitalWrite(P_LED_R, LOW);
digitalWrite(P_LED_G, HIGH);
digitalWrite(P_LED_B, HIGH);
delay(1000);
Raspberry Pi 입출력디바이스II 27
printf("gLED ON.........\n");
digitalWrite(P_LED_R, HIGH);
digitalWrite(P_LED_G, LOW);
digitalWrite(P_LED_B, HIGH);
delay(1000);
printf("bLED ON.........\n");
digitalWrite(P_LED_R, HIGH);
digitalWrite(P_LED_G, HIGH);
digitalWrite(P_LED_B, LOW);
delay(1000);
printf("(r+g)LED ON.........\n");
digitalWrite(P_LED_R, LOW);
digitalWrite(P_LED_G, LOW);
digitalWrite(P_LED_B, HIGH);
delay(1000);
printf("(r+g+b)LED ON.........\n");
digitalWrite(P_LED_R, LOW);
digitalWrite(P_LED_G, LOW);
Raspberry Pi 입출력디바이스II 28
digitalWrite(P_LED_B, LOW);
delay(1000);
printf("all LED OFF.........\n");
digitalWrite(P_LED_R, HIGH);
digitalWrite(P_LED_G, HIGH);
digitalWrite(P_LED_B, HIGH);
delay(2000);
}
return 0;
}
$ gcc –o rgbLed_01 rgbLed_01.c –lwiringPi
혹은, $ make rgbLed_01
$ ./rgbLed_01
Raspberry Pi 입출력디바이스II 29
Relay 모듈
* 릴레이모듈 (DFR0017)
: 외양
NC(Normally Closed), COM(Common), NO(Normally Open)
: 유형
-구동전압에 따라 5V, 12V 등
-채널 개수에 따라 1CH, 2CH, 4CH, 8CH 등
: 실습 모듈 (5V 1CH 250ACV 10A)
-5V로구동, 고압회로는 250V.....
Raspberry Pi 입출력디바이스II 30
Relay 모듈(계속)
* 회로연결
-Normally Open 방식
: 고압회로단자를 COM, NO 단자에 연결
: 릴레이에 High 신호 출력으로 ON 상태
-Normally Closed 방식
: 고압회로단자를 NC, COM 단자에 연결
: 릴레이에 Low 신호 출력으로 ON 상태
Raspberry Pi 입출력디바이스II 31
Relay 모듈(계속)
[실습7] 릴레이 모듈 제어 I
: Normally Open 방식
(고압회로는 NO와 COM 단자에 연결)
: 릴레이모듈에 High신호 인가시 릴레이 ON하는 프로그램
$ sudo nano relay_01.c
//=======================================
// relay_01.c
// Normally Open (COM, NO) ..... ON at High signal
//=======================================
#include <stdio.h>
#include <wiringPi.h>
#define P_RLY 29 // BCM_GPIO #21
Raspberry Pi 입출력디바이스II 32
// funciton definition
void run(void) {
digitalWrite(P_RLY, HIGH);
printf("ON.........\n");
delay(5000);
digitalWrite(P_RLY, LOW);
printf("OFF.........\n");
delay(5000);
}
int main(void) {
printf("[Relay testing....Normally Open]\n");
if(wiringPiSetup() == -1)
return 1;
pinMode(P_RLY, OUTPUT);
while(1) {
run();
}
Raspberry Pi 입출력디바이스II 33
return 0;
}
$ make relay_01
$ ./relay_01
참고) Normally Closed 방식 (relay_02.c 소스 참조)
(고압회로는 NC와 COM 단자에 연결)
Raspberry Pi 입출력디바이스II 34
초음파센서 모듈
* 초음파센서 모듈 (HC-SR04)
: Ultrasonic Ranging Module(HC-SR04)
: 송신기가 초음파 전송한 후, 수신기는 그 반향된 신호를 수신
Raspberry Pi 입출력디바이스II 35
초음파센서 모듈(계속)
* 초음파센서 제어 절차
1) 10us TTL 신호로 Trigger
2) 송신기는 8개 40Hz 버스트 신호 발사(대기)
3) 수신기는 반향신호 수신
(High 레벨 신호의 지속시간 측정)
4) 지속시간과 음속 활용하여 거리 계산
5) 충분한 대기시간( >60ms )
Raspberry Pi 입출력디바이스II 36
초음파센서 모듈(계속)
* wiringPi.h 라이브러리
: 시간지연 함수들이 유용하게 사용될 수 있음
-시간지연함수
void delay(unsigned int howLong); // millisec
void delayMicroseconds(unsigned int howLong); // microsec
-단위시간 반환함수
unsigned int millis(void); // 현 시각을 millisec 단위로
unsigned int micros(void); // 현 시각을 microsec 단위로
Raspberry Pi 입출력디바이스II 37
초음파센서 모듈(계속)
[실습8] 초음파센서 제어
: 거리 측정하는 프로그램
: 회로연결(소스 참조) ... trig, echo 단자 사용
$ sudo nano usonic_01.c
//=======================================
// usonic_01.c
// HC-SR04 module
//=======================================
#include <stdio.h>
#include <wiringPi.h>
#define P_USO_TRIG 5 // Trigger Pulse, BCM_GPIO #24
#define P_USO_ECHO 4 // Echo Pulse, BCM_GPIO #23
int main(void) {
long start, end;
Raspberry Pi 입출력디바이스II 38
int duration;
float distance;
if(wiringPiSetup() == -1)
return 1;
pinMode(P_USO_TRIG, OUTPUT);
pinMode(P_USO_ECHO, INPUT);
printf("[UltraSonic testing....]\n");
while(1) {
// sending 2us signal
digitalWrite(P_USO_TRIG, LOW);
delayMicroseconds(2);
// sending 10microSec high signal
digitalWrite(P_USO_TRIG, HIGH);
delayMicroseconds(10); // 10us
digitalWrite(P_USO_TRIG, LOW);
// wait for burst signal, 8x40kHz=8x25us=200us
delayMicroseconds(200);
Raspberry Pi 입출력디바이스II 39
// receiving echo signal
while(digitalRead(P_USO_ECHO)==LOW) // wait until 1
;
start = micros(); // us unit
while(digitalRead(P_USO_ECHO)==HIGH) // wait until 0
;
end = micros();
// get High level duration(us)
duration = end - start;
// calculate the distance(cm)
distance = duration / 58.;
if(distance > 400) // invalid distance
printf("");
else {
printf("%d,%d duration:%d ... ", start, end, duration);
printf("Distance : %.2f cm\n", distance);
}
Raspberry Pi 입출력디바이스II 40
// delay for measurement cycle, > 60milliSec
delay(100);
}
}
$ make usonic_01
$ ./usonic_01
Raspberry Pi 입출력디바이스II 41
응용과제
[응용1] 부저모듈
: 동요 ‘학교종’연주하는 프로그램
$ sudo nano buzer_03.c
//=======================================
// buzer_03.c
// deangdeangdeang...
//=======================================
#include <stdio.h>
#include <wiringPi.h>
#include <softTone.h>
#define P_BUZ 1 // BCM_GPIO #13
#define Do 262
#define Re 294
Raspberry Pi 입출력디바이스II 42
#define Mi 330
#define Pa 349
#define Sol 392
#define Ra 440
#define Si 494
#define Do1 523
// 학교종이땡땡땡 음계 및 지속시간
int MusicData[] = {Sol, Sol, Ra, Ra, Sol, Sol, Mi,
Sol, Sol, Mi, Mi, Re,
Sol, Sol, Ra, Ra, Sol, Sol, Mi,
Sol, Mi, Re, Mi, Do};
int DelayTime[] = {400, 400, 400, 400, 400, 400, 800,
400, 400, 400, 400, 800,
400, 400, 400, 400, 400, 400, 800,
400, 400, 400, 400, 800};
int main(void) {
int i;
printf("[Music Play...]\n");
Raspberry Pi 입출력디바이스II 43
if(wiringPiSetup() == -1)
return 1;
pinMode(P_BUZ, OUTPUT);
softToneCreate(P_BUZ);
for(i=0; i<24; i++) {
softToneWrite(P_BUZ, MusicData[i]);
delay(DelayTime[i]);
}
softToneStop(P_BUZ); // 음 정지
delay(1000);
return 0;
}
$ make buzer_03
$ ./buzer_03
Raspberry Pi 입출력디바이스II 44
응용과제(계속)
[응용2] RGB LED 제어
: [실습6] 소스 참조
: PWM 방식으로 제어하도록 재구현
(256x256x256 가지 색상 표시토록)
[응용3] Relay 모듈
: Normally Closed 방식으로..
[응용4] 여러 모듈 활용
: 몇 개의 모듈을 조합한 응용과제 각자 정의하여 구현
Raspberry Pi 입출력디바이스II 45
Raspberry Pi 입출력디바이스II 46
이하 무시!!!!
Raspberry Pi 입출력디바이스II 47
움직임감지센서(계속)
* SEN0192 마이크로웨이브 센서
-
-감지 거리 최대 16m 정도
-벽을 통과하여 움직임 감지 가능
-3핀 인터페이스(빨강 Vcc, 검정 Gnd, 초록 감지신호선)
Raspberry Pi 입출력디바이스II 48
움직임감지센서(계속)
[실습2] 움직임감지센서 II
-SEN0192 마이크로웨이브 센서 활용
//=======================================
Raspberry Pi 입출력디바이스II 49
Relay 모듈(계속)
HK4100F-DC5V-SHG
Raspberry Pi 입출력디바이스II 50
적외선센서 및 리모콘
* VS1838B 적외선 센서
-거리
* 리모콘
* lirc 라이브러리